00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class MemcacheEngine extends CacheEngine {
00033
00034
00035
00036
00037
00038
00039 var $__Memcache = null;
00040
00041
00042
00043
00044
00045
00046
00047
00048 var $settings = array();
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 function init($settings = array()) {
00060 if (!class_exists('Memcache')) {
00061 return false;
00062 }
00063 parent::init(array_merge(array(
00064 'engine'=> 'Memcache', 'prefix' => Inflector::slug(APP_DIR) . '_', 'servers' => array('127.0.0.1'), 'compress'=> false
00065 ), $settings)
00066 );
00067
00068 if ($this->settings['compress']) {
00069 $this->settings['compress'] = MEMCACHE_COMPRESSED;
00070 }
00071 if (!is_array($this->settings['servers'])) {
00072 $this->settings['servers'] = array($this->settings['servers']);
00073 }
00074 if (!isset($this->__Memcache)) {
00075 $return = false;
00076 $this->__Memcache =& new Memcache();
00077 foreach ($this->settings['servers'] as $server) {
00078 $parts = explode(':', $server);
00079 $host = $parts[0];
00080 $port = 11211;
00081 if (isset($parts[1])) {
00082 $port = $parts[1];
00083 }
00084 if ($this->__Memcache->addServer($host, $port)) {
00085 $return = true;
00086 }
00087 }
00088 return $return;
00089 }
00090 return true;
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 function write($key, &$value, $duration) {
00102 $expires = time() + $duration;
00103 $this->__Memcache->set($key.'_expires', $expires, $this->settings['compress'], $expires);
00104 return $this->__Memcache->set($key, $value, $this->settings['compress'], $expires);
00105 }
00106
00107
00108
00109
00110
00111
00112
00113 function read($key) {
00114 $time = time();
00115 $cachetime = intval($this->__Memcache->get($key.'_expires'));
00116 if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
00117 return false;
00118 }
00119 return $this->__Memcache->get($key);
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 function delete($key) {
00129 return $this->__Memcache->delete($key);
00130 }
00131
00132
00133
00134
00135
00136
00137 function clear() {
00138 return $this->__Memcache->flush();
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148 function connect($host, $port = 11211) {
00149 if ($this->__Memcache->getServerStatus($host, $port) === 0) {
00150 if ($this->__Memcache->connect($host, $port)) {
00151 return true;
00152 }
00153 return false;
00154 }
00155 return true;
00156 }
00157 }
00158 ?>