PageRenderTime 749ms CodeModel.GetById 604ms app.highlight 11ms RepoModel.GetById 131ms app.codeStats 0ms

/New Folder/functions/include/library/Cache.class.php

https://github.com/bigbanton/Monline
PHP | 407 lines | 175 code | 218 blank | 14 comment | 15 complexity | fe490cbbd754dcbdb2f4e76f2665e49d MD5 | raw file
  1<?php
  2
  3/**
  4 * //License information must not be removed.
  5 * 
  6 * PHP version 5.4x
  7 * 
  8 * @Category ### Gripsell ###
  9 * @Package ### Advanced ###
 10 * @Architecture ### Secured  ###
 11 * @Copyright (c) 2013 {@URL http://www.gripsell.com Gripsell eApps & Technologies Private Limited}
 12 * @License EULA License http://www.gripsell.com
 13 * @Author $Author: gripsell $
 14 * @Version $Version: 5.3.3 $
 15 * @Last Revision $Date: 2013-21-05 00:00:00 +0530 (Tue, 21 May 2013) $
 16 */
 17
 18class FalseCache {
 19    public function __call($m, $v)
 20    {
 21    } 
 22} ;
 23
 24class Cache
 25
 26 {
 27    
 28    
 29    
 30    static private $mCache = null;
 31    
 32     static private $gCache = array();
 33    
 34     static private $mInstance = null;
 35    
 36     static private $mIndex = 0;
 37    
 38    
 39    
 40     static public function Instance()
 41    
 42    
 43    {
 44        
 45         if (!self::$mInstance) self::$mInstance = new Cache();
 46        
 47         } 
 48    
 49    
 50    
 51    static private function _CreateCacheInstance()
 52    
 53    
 54    {
 55        
 56         $ini = Config::Instance('php');
 57        settype($ini['memcache'], 'array');
 58        
 59         if (!class_exists('Memcache', false)) return new FalseCache();
 60        
 61         $cache_instance = new Memcache();
 62        
 63         foreach($ini['memcache'] AS $one)
 64        
 65         {
 66            
 67            $server = (string) $one;
 68            
 69             list($ip, $port, $weight) = explode(':', $server);
 70            
 71             if (!$ip || $port || $weight) continue;
 72            
 73             $cache_instance->addServer($ip
 74                
 75                 , $port
 76                
 77                 , true
 78                
 79                 , $weight
 80                
 81                 , 1
 82                
 83                 , 15
 84                
 85                 , true
 86                
 87                 , array('Cache', 'FailureCallback')
 88                
 89                );
 90            
 91             self::$mIndex++;
 92            
 93             } 
 94        
 95        return self::$mIndex ? $cache_instance : new FalseCache();
 96        
 97         } 
 98    
 99    
100    
101    private function __construct()
102    
103    
104    {
105        
106         self::$mCache = self::_CreateCacheInstance();
107        
108         } 
109    
110    
111    
112    static public function FailureCallback($ip, $port)
113    
114    
115    {
116        
117         self::$mIndex--;
118        if (self::$mIndex <= 0) self::$mCache = new FalseCache();
119        
120         } 
121    
122    
123    
124    static function Get($key)
125    
126    
127    {
128        
129         self::Instance();
130        
131         if (is_array($key)) {
132            
133            $v = array();
134            
135             foreach($key as $k) {
136                
137                $vv = self::Get($k);
138                
139                 if ($vv) {
140                    
141                    $v[$k] = $vv;
142                    
143                     } 
144                
145                } 
146            
147            return $v;
148            
149             } else {
150            
151            if (isset(self::$gCache[$key])) {
152                
153                return self::$gCache[$key];
154                
155                 } 
156            
157            $v = self::$mCache->get($key);
158            
159             if ($v) {
160                self::$gCache[$key] = $v;
161            } 
162            
163            return $v;
164            
165             } 
166        
167        } 
168    
169    
170    
171    
172    
173    static function Add($key, $var, $flag = 0, $expire = 0)
174    {
175        
176         self::Instance();
177        
178         self::$mCache->add($key, $var, $flag, $expire);
179        
180         self::$gCache[$key] = $var;
181        
182         } 
183    
184    
185    
186    
187    
188    static function Dec($key, $value = 1)
189    
190    
191    {
192        
193         self::Instance();
194        
195         return self::$mCache->decrement($key, $value);
196        
197         } 
198    
199    
200    
201    
202    
203    static function Inc($key, $value = 1)
204    
205    
206    {
207        
208         self::Instance();
209        
210         return self::$mCache->increment($key, $value);
211        
212         } 
213    
214    
215    
216    static function Replace($key, $var, $flag = 0, $expire = 0)
217    
218    
219    {
220        
221         self::Instance();
222        
223         return self::$mCache->replace($key, $var, $flag, $expire);
224        
225         } 
226    
227    
228    
229    
230    
231    static function Set($key, $var, $flag = 0, $expire = 0)
232    {
233        
234         self::Instance();
235        
236         self::$mCache->set($key, $var, $flag, $expire);
237        
238         self::$gCache[$key] = $var;
239        
240         return true;
241        
242         } 
243    
244    
245    
246    static function Del($key, $timeout = 0)
247    {
248        
249         self::Instance();
250        
251         if (is_array($key)) {
252            
253            foreach ($key as $k) {
254                
255                self::$mCache->delete($k, $timeout);
256                
257                 if (isset(self::$gCache[$k])) unset(self::$gCache[$k]);
258                
259                 } 
260            
261            } else {
262            
263            self::$mCache->delete($key, $timeout);
264            
265             if (isset(self::$gCache[$key])) unset(self::$gCache[$k]);
266            
267             } 
268        
269        return true;
270        
271         } 
272    
273    
274    
275    static function Flush()
276    
277    
278    {
279        
280         self::Instance();
281        
282         return self::$mCache->flush();
283        
284         } 
285    
286    
287    
288    static function GetFunctionKey($callback, $args = array())
289    
290    
291    {
292        
293         $args = ksort($args);
294        
295         $patt = "/(=>)\s*'(\d+)'/";
296        
297         $args_string = var_export($args, true);
298        
299         $args_string = preg_replace($patt, "\\1\\2", $args_string);
300        
301         $key = "[FUNC]:$callback($args_string)";
302        
303         return self::GenKey($key);
304        
305         } 
306    
307    
308    
309    static function GetStringKey($str = null)
310    {
311        
312         settype($str, 'array');
313        $str = var_export($str, true);
314        
315         $key = "[STR]:{$str}";
316        
317         return self::GenKey($key);
318        
319         } 
320    
321    
322    
323    static function GetObjectKey($tablename, $id)
324    
325    
326    {
327        
328         $key = "[OBJ]:$tablename($id)";
329        
330         return self::GenKey($key);
331        
332         } 
333    
334    
335    
336    static function GenKey($key)
337    {
338        
339         $hash = dirname(__FILE__);
340        
341         return md5($hash . $key);
342        
343         } 
344    
345    
346    
347    static function SetObject($tablename, $one)
348    {
349        
350         self::Instance();
351        
352         foreach($one AS $oone) {
353            
354            $k = self::GetObjectKey($tablename, $oone['id']);
355            
356             self::Set($k, $oone);
357            
358             } 
359        
360        return true;
361        
362         } 
363    
364    
365    
366    static function GetObject($tablename, $id)
367    {
368        
369         $single = ! is_array($id);
370        
371         settype($id, 'array');
372        
373         $k = array();
374        
375         foreach($id AS $oid) {
376            
377            $k[] = self::GetObjectKey($tablename, $oid);
378            
379             } 
380        
381        $r = Utility::AssColumn(self::Get($k), 'id');
382        
383         return $single ? array_pop($r) : $r;
384        
385         } 
386    
387    
388    
389    static function ClearObject($tablename, $id)
390    {
391        
392         settype($id, 'array');
393        
394         foreach($id AS $oid) {
395            
396            $key = self::GetObjectKey($tablename, $oid);
397            
398             self::Del($key);
399            
400             } 
401        
402        return true;
403        
404         } 
405    
406    } 
407