00001 <?php
00002 
00003 
00004 
00005 
00006 
00007 
00008 class XCacheCacheItem implements ICacheItem {
00009 
00010 
00011 
00012 
00013 
00014         protected $item_data;
00015         
00016 
00017 
00018 
00019 
00020 
00021         public function __construct($item_data) {
00022                 $this->item_data = $item_data;
00023         }
00024         
00025 
00026 
00027 
00028 
00029 
00030         public function get_creationdate() {
00031                 return $this->item_data['creationdate'];
00032         }       
00033         
00034 
00035 
00036 
00037 
00038 
00039         public function get_expirationdate() {
00040                 return $this->item_data['expirationdate'];
00041         }
00042         
00043 
00044 
00045 
00046 
00047 
00048         public function get_data() {
00049                 return $this->item_data['data'];
00050         }
00051         
00052 
00053 
00054 
00055 
00056 
00057         public function get_content_plain() {
00058                 $ret = $this->get_content_compressed();
00059                 if ($ret && function_exists('gzinflate')) {
00060                         $ret = gzinflate($ret);
00061                 }
00062                 return $ret;
00063         }
00064         
00065 
00066 
00067 
00068 
00069 
00070         public function get_content_compressed() {
00071                 return $this->item_data['content'];
00072         }       
00073 }
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 class CacheXCacheImpl implements ICachePersister {
00082 
00083 
00084 
00085         public function is_cached($cache_keys) {
00086                 $key = $this->flatten_keys($cache_keys);
00087                 return xcache_isset($key);
00088         }
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096         public function read($cache_keys) {
00097                 $ret = false;
00098                 $key = $this->flatten_keys($cache_keys);
00099                 if (xcache_isset($key)) {
00100                         $ret = new XCacheCacheItem(xcache_get($key));
00101                 }
00102                 return $ret;
00103         }
00104         
00105 
00106 
00107 
00108 
00109 
00110 
00111         public function store($cache_keys, $content, $cache_life_time, $data = '', $is_compressed = false) {
00112                 if (!$is_compressed) {
00113                         if (function_exists('gzdeflate')) {
00114                                 $content = gzdeflate($content, 9);
00115                         }
00116                 } 
00117                 $data = array(
00118                         'content' => $content,
00119                         'data' => $data,
00120                         'creationdate' => time(),
00121                         'expirationdate' => time() + $cache_life_time                   
00122                 );
00123                 $key = $this->flatten_keys($cache_keys);
00124                 xcache_set($key, $data, $cache_life_time);
00125         }
00126         
00127 
00128 
00129 
00130 
00131 
00132         public function clear($cache_keys = NULL) {
00133                 if (empty($cache_keys)) {
00134                         $this->do_clear_all();
00135                 }
00136                 else {
00137                         $this->do_clear($cache_keys);
00138                 }
00139         }
00140         
00141 
00142 
00143 
00144         protected function do_clear_all() {
00145                 xcache_unset_by_prefix($this->get_app_key());
00146         }
00147         
00148 
00149 
00150 
00151         protected function do_clear($cache_keys) {
00152                 $key = $this->flatten_keys($cache_keys);
00153                 xcache_unset($key);
00154                 xcache_unset_by_prefix($key . '_g$c');
00155         }
00156 
00157 
00158 
00159 
00160 
00161 
00162         protected function get_app_key() {
00163                 return 'g$c' . Config::get_url(Config::URL_DOMAIN) . '';
00164         }
00165 
00166 
00167 
00168 
00169         protected function preprocess_keys($cache_keys, $strip_empty = true) {
00170                 $cleaned = array($this->get_app_key());
00171                 if ($strip_empty) {
00172                         foreach(Arr::force($cache_keys, false) as $key) {
00173                                 if ($key || $key == '0') {
00174                                         $cleaned[] = $key;
00175                                 } 
00176                                 else {
00177                                         break;
00178                                 }
00179                         }               
00180                 }
00181                 else {
00182                         $cleaned = array_merge($cleaned, Arr::force($cache_keys, true));
00183                 }
00184                 return $cleaned;
00185         }
00186                 
00187 
00188 
00189 
00190 
00191 
00192         protected function flatten_keys($cache_keys) {
00193                 $cache_keys = $this->preprocess_keys($cache_keys);
00194                 $ret .= implode('_g$c', $cache_keys);
00195                 return $ret;            
00196         }
00197 
00198 
00199 
00200 
00201         public function remove_expired() {
00202                 
00203         }
00204         
00205 }