|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00038 defined('MOODLE_INTERNAL') || die(); 00039 00046 class eaccelerator { 00047 00053 function eaccelerator() { 00054 global $CFG; 00055 if ( function_exists('eaccelerator_get')) { 00056 $this->mode = 'eaccelerator'; 00057 } elseif (function_exists('mmcache_get')) { 00058 $this->mode = 'mmcache'; 00059 } else { 00060 debugging("\$CFG->eaccelerator is set to true but the required functions are not available. You need to have either eaccelerator or turckmmcache extensions installed, compiled with the shmem keys option enabled."); 00061 } 00062 00063 $this->prefix = $CFG->dbname .'|' . $CFG->prefix . '|'; 00064 } 00065 00072 function status() { 00073 if (isset($this->mode)) { 00074 return true; 00075 } 00076 return false; 00077 } 00078 00087 function set($key, $value, $ttl=0) { 00088 $set = $this->mode . '_put'; 00089 $unlock = $this->mode . '_unlock'; 00090 00091 // we may have acquired a lock via getforfill 00092 // release if it exists 00093 @$unlock($this->prefix . $key . '_forfill'); 00094 00095 return $set($this->prefix . $key, serialize($value), $ttl); 00096 } 00097 00104 function get($key) { 00105 $fn = $this->mode . '_get'; 00106 $rec = $fn($this->prefix . $key); 00107 if (is_null($rec)) { 00108 return false; 00109 } 00110 return unserialize($rec); 00111 } 00112 00119 function delete($key) { 00120 $fn = $this->mode . '_rm'; 00121 return $fn($this->prefix . $key); 00122 } 00123 00149 function getforfill ($key) { 00150 $get = $this->mode . '_get'; 00151 $lock = $this->mode . '_lock'; 00152 00153 $rec = $get($this->prefix . $key); 00154 if (!is_null($rec)) { 00155 return unserialize($rec); 00156 } 00157 if ($lock($this->prefix . $key . '_forfill')) { 00158 // we obtained the _forfill lock 00159 // our caller will compute and set the value 00160 return false; 00161 } 00162 // someone else has the lock 00163 // "block" till we can get the value 00164 // actually, loop .05s waiting for it 00165 for ($n=0;$n<5;$n++) { 00166 usleep(10000); 00167 $rec = $get($this->prefix . $key); 00168 if (!is_null($rec)) { 00169 return unserialize($rec); 00170 } 00171 } 00172 return false; 00173 } 00174 00183 function releaseforfill ($key) { 00184 $unlock = $this->mode . '_unlock'; 00185 return $unlock($this->prefix . $key . '_forfill'); 00186 } 00187 } 00188 00189 ?>