| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace voku\cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * AdapterApc: a APC-Cache adapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * http://php.net/manual/de/book.apc.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package   voku\cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class AdapterApc implements iAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |    * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |   public $installed = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |    * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |   public $debug = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |    * __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 9 |  |   public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 9 |  |     if (function_exists('apc_store') === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |       $this->installed = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 9 |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |    * cacheInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |    * Retrieves cached information from APC's data store | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |    * @param string  $type    - If $type is "user", information about the user cache will be returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |    * @param boolean $limited - If $limited is TRUE, the return value will exclude the individual list of cache entries. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |    *                         This is useful when trying to optimize calls for statistics gathering. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |    * @return array of cached data (and meta-data) or FALSE on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |   public function cacheInfo($type = '', $limited = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     return apc_cache_info($type, $limited); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |   /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |    * apc_cache_exists (fallback function for old apc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |    * * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |    * Cache a variable in the data-store | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |    * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |    * @param mixed  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |    * @return Boolean - Returns TRUE on success or FALSE on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |   public function set($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     return apc_store($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |    * Cache a variable in the data-store with ttl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |    * @param string $key  - Store the variable using this name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |    * @param string $data - The variable to store. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |    * @param string $ttl  - Time To Live; store var in the cache for ttl seconds. "0" for no ttl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |    * @return boolean - Returns TRUE on success or FALSE on failure. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |   public function setExpired($key, $data, $ttl) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |   { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     return apc_store($key, $data, $ttl); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |    * get stored value in APC from key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |    * @param string $key - The key used to store the value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |    * @return boolean - The stored variable or array of variables on success; FALSE on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |   public function get($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     if ($this->exists($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |       return apc_fetch($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |       return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |    * Checks if APC key exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |    * @param mixed $key - A string, or an array of strings, that contain keys. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |    * @return mixed - Returns TRUE if the key exists, otherwise FALSE Or if an array was passed to keys, then an array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |    *               is returned that contains all existing keys, or an empty array if none exist. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |   public function exists($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     if (function_exists('apc_exists')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |       return apc_exists($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |       return $this->apc_cache_exists($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |    * check if apc-cache exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |    * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |    * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |   public function apc_cache_exists($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     return (bool)apc_fetch($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |    * Removes a stored variable from the cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |    * @param string $key - The key used to store the value (with apc_store()). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |    * @return boolean - Returns TRUE on success or FALSE on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |   public function remove($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     return apc_delete($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |    * Removes the cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |    * @return boolean - Returns TRUE on success or FALSE on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |   public function removeAll() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     return apc_clear_cache('system') && apc_clear_cache('user'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |    * Clears the APC cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |    * @param string $type - If $type is "user", the user cache will be cleared; otherwise, the system cache (cached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |    *                     files) will be cleared. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |    * @return boolean - Returns TRUE on success or FALSE on failure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |   public function cacheClear($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     return apc_clear_cache($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |    * check if cache is installed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |    * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |   public function installed() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     return $this->installed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 175 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |  |