Source for file Cache.php

Documentation is available at Cache.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Caching
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Object.php';
  23. 23:  
  24. 24:  
  25. 25:  
  26. 26: /**
  27. 27:  * Implements the cache for a application.
  28. 28:  *
  29. 29:  * @author     David Grudl
  30. 30:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  31. 31:  * @package    Nette\Caching
  32. 32:  */
  33. 33: class Cache extends Object implements ArrayAccess
  34. 34: {
  35. 35:     /**#@+ dependency */
  36. 36:     const PRIORITY = 'priority';
  37. 37:     const EXPIRE = 'expire';
  38. 38:     const SLIDING = 'sliding';
  39. 39:     const TAGS = 'tags';
  40. 40:     const FILES = 'files';
  41. 41:     const ITEMS = 'items';
  42. 42:     const CONSTS = 'consts';
  43. 43:     const CALLBACKS = 'callbacks';
  44. 44:     const ALL = 'all';
  45. 45:     /**#@-*/
  46. 46:  
  47. 47:     /** @deprecated */
  48. 48:     const REFRESH = 'sliding';
  49. 49:  
  50. 50:     /** @ignore internal */
  51. 51:     const NAMESPACE_SEPARATOR "\x00";
  52. 52:  
  53. 53:     /** @var ICacheStorage */
  54. 54:     private $storage;
  55. 55:  
  56. 56:     /** @var string */
  57. 57:     private $namespace;
  58. 58:  
  59. 59:     /** @var string  last query cache */
  60. 60:     private $key;
  61. 61:  
  62. 62:     /** @var mixed  last query cache */
  63. 63:     private $data;
  64. 64:  
  65. 65:  
  66. 66:  
  67. 67:     public function __construct(ICacheStorage $storage$namespace NULL)
  68. 68:     {
  69. 69:         $this->storage $storage;
  70. 70:         $this->namespace = (string) $namespace;
  71. 71:  
  72. 72:         if (strpos($this->namespaceself::NAMESPACE_SEPARATOR!== FALSE{
  73. 73:             throw new InvalidArgumentException("Namespace name contains forbidden character.");
  74. 74:         }
  75. 75:     }
  76. 76:  
  77. 77:  
  78. 78:  
  79. 79:     /**
  80. 80:      * Returns cache storage.
  81. 81:      * @return ICacheStorage 
  82. 82:      */
  83. 83:     public function getStorage()
  84. 84:     {
  85. 85:         return $this->storage;
  86. 86:     }
  87. 87:  
  88. 88:  
  89. 89:  
  90. 90:     /**
  91. 91:      * Returns cache namespace.
  92. 92:      * @return string 
  93. 93:      */
  94. 94:     public function getNamespace()
  95. 95:     {
  96. 96:         return $this->namespace;
  97. 97:     }
  98. 98:  
  99. 99:  
  100. 100:  
  101. 101:     /**
  102. 102:      * Discards the internal cache.
  103. 103:      * @return void 
  104. 104:      */
  105. 105:     public function release()
  106. 106:     {
  107. 107:         $this->key $this->data NULL;
  108. 108:     }
  109. 109:  
  110. 110:  
  111. 111:  
  112. 112:     /**
  113. 113:      * Writes item into the cache.
  114. 114:      * Dependencies are:
  115. 115:      * - Cache::PRIORITY => (int) priority
  116. 116:      * - Cache::EXPIRE => (timestamp) expiration
  117. 117:      * - Cache::SLIDING => (bool) use sliding expiration?
  118. 118:      * - Cache::TAGS => (array) tags
  119. 119:      * - Cache::FILES => (array|string) file names
  120. 120:      * - Cache::ITEMS => (array|string) cache items
  121. 121:      * - Cache::CONSTS => (array|string) cache items
  122. 122:      *
  123. 123:      * @param  string key
  124. 124:      * @param  mixed  value
  125. 125:      * @param  array  dependencies
  126. 126:      * @return void 
  127. 127:      * @throws InvalidArgumentException
  128. 128:      */
  129. 129:     public function save($key$dataarray $dp NULL)
  130. 130:     {
  131. 131:         if (!is_string($key)) {
  132. 132:             throw new InvalidArgumentException("Cache key name must be string, " gettype($key." given.");
  133. 133:         }
  134. 134:  
  135. 135:         // convert expire into relative amount of seconds
  136. 136:         if (!empty($dp[Cache::EXPIRE])) {
  137. 137:             $expire $dp[Cache::EXPIRE];
  138. 138:             if (is_string($expire&& !is_numeric($expire)) {
  139. 139:                 $expire strtotime($expiretime();
  140. 140:             elseif ($expire Tools::YEAR{
  141. 141:                 $expire -= time();
  142. 142:             }
  143. 143:         }
  144. 144:  
  145. 145:         // convert FILES into CALLBACKS
  146. 146:         if (isset($dp[self::FILES])) {
  147. 147:             //clearstatcache();
  148. 148:             foreach ((array) $dp[self::FILESas $item{
  149. 149:                 $dp[self::CALLBACKS][array(array(__CLASS__'checkFile')$item@filemtime($item))// intentionally @
  150. 150:             }
  151. 151:             unset($dp[self::FILES]);
  152. 152:         }
  153. 153:  
  154. 154:         // add namespaces to items
  155. 155:         if (isset($dp[self::ITEMS])) {
  156. 156:             $dp[self::ITEMS= (array) $dp[self::ITEMS];
  157. 157:             foreach ($dp[self::ITEMSas $k => $item{
  158. 158:                 $dp[self::ITEMS][$k$this->namespace self::NAMESPACE_SEPARATOR $item;
  159. 159:             }
  160. 160:         }
  161. 161:  
  162. 162:         // convert CONSTS into CALLBACKS
  163. 163:         if (isset($dp[self::CONSTS])) {
  164. 164:             foreach ((array) $dp[self::CONSTSas $item{
  165. 165:                 $dp[self::CALLBACKS][array(array(__CLASS__'checkConst')$itemconstant($item));
  166. 166:             }
  167. 167:             unset($dp[self::CONSTS]);
  168. 168:         }
  169. 169:  
  170. 170:         $this->key = NULL;
  171. 171:         $this->storage->write(
  172. 172:             $this->namespace self::NAMESPACE_SEPARATOR $key,
  173. 173:             $data,
  174. 174:             (array) $dp
  175. 175:         );
  176. 176:     }
  177. 177:  
  178. 178:  
  179. 179:  
  180. 180:     /**
  181. 181:      * Removes items from the cache by conditions.
  182. 182:      * Conditions are:
  183. 183:      * - Cache::PRIORITY => (int) priority
  184. 184:      * - Cache::TAGS => (array) tags
  185. 185:      * - Cache::ALL => TRUE
  186. 186:      *
  187. 187:      * @param  array 
  188. 188:      * @return void 
  189. 189:      */
  190. 190:     public function clean(array $conds NULL)
  191. 191:     {
  192. 192:         $this->storage->clean((array) $conds);
  193. 193:     
  194. 194:  
  195. 195:  
  196. 196:  
  197. 197:     /********************* interface \ArrayAccess ****************d*g**/
  198. 198:  
  199. 199:  
  200. 200:  
  201. 201:     /**
  202. 202:      * Inserts (replaces) item into the cache (\ArrayAccess implementation).
  203. 203:      * @param  string key
  204. 204:      * @param  mixed 
  205. 205:      * @return void 
  206. 206:      * @throws InvalidArgumentException
  207. 207:      */
  208. 208:     public function offsetSet($key$data)
  209. 209:     {
  210. 210:         if (!is_string($key)) // prevents NULL
  211. 211:             throw new InvalidArgumentException("Cache key name must be string, " gettype($key." given.");
  212. 212:         }
  213. 213:  
  214. 214:         $this->key = $this->data NULL;
  215. 215:         if ($data === NULL{
  216. 216:             $this->storage->remove($this->namespace self::NAMESPACE_SEPARATOR $key);
  217. 217:         else {
  218. 218:             $this->storage->write($this->namespace self::NAMESPACE_SEPARATOR $key$dataarray());
  219. 219:         }
  220. 220:     }
  221. 221:  
  222. 222:  
  223. 223:  
  224. 224:     /**
  225. 225:      * Retrieves the specified item from the cache or NULL if the key is not found (\ArrayAccess implementation).
  226. 226:      * @param  string key
  227. 227:      * @return mixed|NULL
  228. 228:      * @throws InvalidArgumentException
  229. 229:      */
  230. 230:     public function offsetGet($key)
  231. 231:     {
  232. 232:         if (!is_string($key)) {
  233. 233:             throw new InvalidArgumentException("Cache key name must be string, " gettype($key." given.");
  234. 234:         }
  235. 235:  
  236. 236:         if ($this->key === $key{
  237. 237:             return $this->data;
  238. 238:         }
  239. 239:         $this->key = $key;
  240. 240:         $this->data $this->storage->read($this->namespace self::NAMESPACE_SEPARATOR $key);
  241. 241:         return $this->data;
  242. 242:     }
  243. 243:  
  244. 244:  
  245. 245:  
  246. 246:     /**
  247. 247:      * Exists item in cache? (\ArrayAccess implementation).
  248. 248:      * @param  string key
  249. 249:      * @return bool 
  250. 250:      * @throws InvalidArgumentException
  251. 251:      */
  252. 252:     public function offsetExists($key)
  253. 253:     {
  254. 254:         if (!is_string($key)) {
  255. 255:             throw new InvalidArgumentException("Cache key name must be string, " gettype($key." given.");
  256. 256:         }
  257. 257:  
  258. 258:         $this->key = $key;
  259. 259:         $this->data $this->storage->read($this->namespace self::NAMESPACE_SEPARATOR $key);
  260. 260:         return $this->data !== NULL;
  261. 261:     }
  262. 262:  
  263. 263:  
  264. 264:  
  265. 265:     /**
  266. 266:      * Removes the specified item from the cache.
  267. 267:      * @param  string key
  268. 268:      * @return void 
  269. 269:      * @throws InvalidArgumentException
  270. 270:      */
  271. 271:     public function offsetUnset($key)
  272. 272:     {
  273. 273:         if (!is_string($key)) {
  274. 274:             throw new InvalidArgumentException("Cache key name must be string, " gettype($key." given.");
  275. 275:         }
  276. 276:  
  277. 277:         $this->key = $this->data NULL;
  278. 278:         $this->storage->remove($this->namespace self::NAMESPACE_SEPARATOR $key);
  279. 279:     }
  280. 280:  
  281. 281:  
  282. 282:  
  283. 283:     /********************* dependency checkers ****************d*g**/
  284. 284:  
  285. 285:  
  286. 286:  
  287. 287:     /**
  288. 288:      * Checks CALLBACKS dependencies.
  289. 289:      * @param  array 
  290. 290:      * @return bool 
  291. 291:      */
  292. 292:     public static function checkCallbacks($callbacks)
  293. 293:     {
  294. 294:         foreach ($callbacks as $callback{
  295. 295:             $func array_shift($callback);
  296. 296:             if (!call_user_func_array($func$callback)) {
  297. 297:                 return FALSE;
  298. 298:             }
  299. 299:         }
  300. 300:         return TRUE;
  301. 301:     }
  302. 302:  
  303. 303:  
  304. 304:  
  305. 305:     /**
  306. 306:      * Checks CONSTS dependency.
  307. 307:      * @param  string 
  308. 308:      * @param  mixed 
  309. 309:      * @return bool 
  310. 310:      */
  311. 311:     private static function checkConst($const$value)
  312. 312:     {
  313. 313:         return defined($const&& constant($const=== $value;
  314. 314:     }
  315. 315:  
  316. 316:  
  317. 317:  
  318. 318:     /**
  319. 319:      * Checks FILES dependency.
  320. 320:      * @param  string 
  321. 321:      * @param  int 
  322. 322:      * @return bool 
  323. 323:      */
  324. 324:     private static function checkFile($file$time)
  325. 325:     {
  326. 326:         return @filemtime($file== $time// intentionally @
  327. 327:     }
  328. 328: