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 https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Caching
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../Object.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * Implements the cache for a application.
  29. 29:  *
  30. 30:  * @author     David Grudl
  31. 31:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  32. 32:  * @package    Nette\Caching
  33. 33:  */
  34. 34: class Cache extends Object implements ArrayAccess
  35. 35: {
  36. 36:     /**#@+ dependency */
  37. 37:     const PRIORITY = 'priority';
  38. 38:     const EXPIRE = 'expire';
  39. 39:     const REFRESH = 'refresh';
  40. 40:     const TAGS = 'tags';
  41. 41:     const FILES = 'files';
  42. 42:     const ITEMS = 'items';
  43. 43:     const CONSTS = 'consts';
  44. 44:     const ALL = 'all';
  45. 45:     /**#@-*/
  46. 46:  
  47. 47:     /** @var ICacheStorage */
  48. 48:     private $storage;
  49. 49:  
  50. 50:     /** @var string */
  51. 51:     private $namespace;
  52. 52:  
  53. 53:     /** @var string  last query cache */
  54. 54:     private $key;
  55. 55:  
  56. 56:     /** @var mixed  last query cache */
  57. 57:     private $data;
  58. 58:  
  59. 59:  
  60. 60:  
  61. 61:     public function __construct(ICacheStorage $storage$namespace NULL)
  62. 62:     {
  63. 63:         $this->storage $storage;
  64. 64:         $this->namespace $namespace == NULL '' $namespace "\x00";
  65. 65:     }
  66. 66:  
  67. 67:  
  68. 68:  
  69. 69:     /**
  70. 70:      * Returns cache storage.
  71. 71:      * @return ICacheStorage 
  72. 72:      */
  73. 73:     public function getStorage()
  74. 74:     {
  75. 75:         return $this->storage;
  76. 76:     }
  77. 77:  
  78. 78:  
  79. 79:  
  80. 80:     /**
  81. 81:      * Returns cache namespace.
  82. 82:      * @return string 
  83. 83:      */
  84. 84:     public function getNamespace()
  85. 85:     {
  86. 86:         return $this->namespace;
  87. 87:     }
  88. 88:  
  89. 89:  
  90. 90:  
  91. 91:     /**
  92. 92:      * Discards the internal cache.
  93. 93:      * @return void 
  94. 94:      */
  95. 95:     public function release()
  96. 96:     {
  97. 97:         $this->key $this->data NULL;
  98. 98:     }
  99. 99:  
  100. 100:  
  101. 101:  
  102. 102:     /**
  103. 103:      * Writes item into the cache.
  104. 104:      * Dependencies are:
  105. 105:      *       priority => (int) priority
  106. 106:      *       expire => (timestamp) expiration
  107. 107:      *       refresh => (bool) use sliding expiration?
  108. 108:      *       tags => (array) tags
  109. 109:      *       files => (array|string) file names
  110. 110:      *       items => (array|string) cache items
  111. 111:      *       consts => (array|string) cache items
  112. 112:      *
  113. 113:      * @param  string key
  114. 114:      * @param  mixed 
  115. 115:      * @param  array 
  116. 116:      * @return void 
  117. 117:      * @throws InvalidArgumentException
  118. 118:      */
  119. 119:     public function save($key$dataarray $dependencies NULL)
  120. 120:     {
  121. 121:         if (!is_string($key)) {
  122. 122:             throw new InvalidArgumentException("Cache key name must be string, " gettype($key." given.");
  123. 123:         }
  124. 124:  
  125. 125:         $this->key NULL;
  126. 126:  
  127. 127:         $this->adjust($dependencies);
  128. 128:  
  129. 129:         $this->storage->write(
  130. 130:             $this->namespace $key,
  131. 131:             $data,
  132. 132:             $dependencies
  133. 133:         );
  134. 134:     }
  135. 135:  
  136. 136:  
  137. 137:  
  138. 138:     /**
  139. 139:      * Removes items from the cache by conditions.
  140. 140:      * @param  array 
  141. 141:      * @return void 
  142. 142:      */
  143. 143:     public function clean(array $conds NULL)
  144. 144:     {
  145. 145:         $this->adjust($conds);
  146. 146:         $this->storage->clean($conds);
  147. 147:     
  148. 148:  
  149. 149:  
  150. 150:  
  151. 151:     /**
  152. 152:      * Update dependencies array.
  153. 153:      * @param  array 
  154. 154:      * @return void 
  155. 155:      */
  156. 156:     private function adjust($arr)
  157. 157:     {
  158. 158:         if ($arr === NULL{
  159. 159:             $arr array();
  160. 160:         }
  161. 161:  
  162. 162:         if (isset($arr[self::TAGS])) {
  163. 163:             $arr[self::TAGS= (array) $arr[self::TAGS];
  164. 164:             foreach ($arr[self::TAGSas $key => $value{
  165. 165:                 $arr[self::TAGS][$key$this->namespace $value;
  166. 166:             }
  167. 167:         }
  168. 168:  
  169. 169:         if (isset($arr[self::ITEMS])) {
  170. 170:             $arr[self::ITEMS= (array) $arr[self::ITEMS];
  171. 171:             foreach ($arr[self::ITEMSas $key => $value{
  172. 172:                 $arr[self::ITEMS][$key$this->namespace $value;
  173. 173:             }
  174. 174:         }
  175. 175:     }
  176. 176:  
  177. 177:  
  178. 178:  
  179. 179:     /********************* interface \ArrayAccess ****************d*g**/
  180. 180:  
  181. 181:  
  182. 182:  
  183. 183:     /**
  184. 184:      * Inserts (replaces) item into the cache (\ArrayAccess implementation).
  185. 185:      * @param  string key
  186. 186:      * @param  mixed 
  187. 187:      * @return void 
  188. 188:      * @throws InvalidArgumentException
  189. 189:      */
  190. 190:     public function offsetSet($key$data)
  191. 191:     {
  192. 192:         if (!is_string($key)) // prevents NULL
  193. 193:             throw new InvalidArgumentException("Cache key name must be string, " gettype($key." given.");
  194. 194:         }
  195. 195:  
  196. 196:         $this->key = $this->data NULL;
  197. 197:         if ($data === NULL{
  198. 198:             $this->storage->remove($this->namespace $key);
  199. 199:         else {
  200. 200:             $this->storage->write($this->namespace $key$dataarray());
  201. 201:         }
  202. 202:     }
  203. 203:  
  204. 204:  
  205. 205:  
  206. 206:     /**
  207. 207:      * Retrieves the specified item from the cache or NULL if the key is not found (\ArrayAccess implementation).
  208. 208:      * @param  string key
  209. 209:      * @return mixed|NULL
  210. 210:      * @throws InvalidArgumentException
  211. 211:      */
  212. 212:     public function offsetGet($key)
  213. 213:     {
  214. 214:         if (!is_string($key)) {
  215. 215:             throw new InvalidArgumentException("Cache key name must be string, " gettype($key." given.");
  216. 216:         }
  217. 217:  
  218. 218:         if ($this->key === $key{
  219. 219:             return $this->data;
  220. 220:         }
  221. 221:         $this->key = $key;
  222. 222:         $this->data $this->storage->read($this->namespace $key);
  223. 223:         return $this->data;
  224. 224:     }
  225. 225:  
  226. 226:  
  227. 227:  
  228. 228:     /**
  229. 229:      * Exists item in cache? (\ArrayAccess implementation).
  230. 230:      * @param  string key
  231. 231:      * @return bool 
  232. 232:      * @throws InvalidArgumentException
  233. 233:      */
  234. 234:     public function offsetExists($key)
  235. 235:     {
  236. 236:         if (!is_string($key)) {
  237. 237:             throw new InvalidArgumentException("Cache key name must be string, " gettype($key." given.");
  238. 238:         }
  239. 239:  
  240. 240:         $this->key = $key;
  241. 241:         $this->data $this->storage->read($this->namespace $key);
  242. 242:         return $this->data !== NULL;
  243. 243:     }
  244. 244:  
  245. 245:  
  246. 246:  
  247. 247:     /**
  248. 248:      * Removes the specified item from the cache.
  249. 249:      * @param  string key
  250. 250:      * @return void 
  251. 251:      * @throws InvalidArgumentException
  252. 252:      */
  253. 253:     public function offsetUnset($key)
  254. 254:     {
  255. 255:         if (!is_string($key)) {
  256. 256:             throw new InvalidArgumentException("Cache key name must be string, " gettype($key." given.");
  257. 257:         }
  258. 258:  
  259. 259:         $this->key = $this->data NULL;
  260. 260:         $this->storage->remove($this->namespace $key);
  261. 261:     }
  262. 262: