1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Caching;
9:
10: use Nette,
11: Nette\Utils\Callback;
12:
13:
14: 15: 16: 17: 18: 19: 20: 21:
22: class Cache extends Nette\Object implements \ArrayAccess
23: {
24:
25: const PRIORITY = 'priority',
26: EXPIRATION = 'expire',
27: EXPIRE = 'expire',
28: SLIDING = 'sliding',
29: TAGS = 'tags',
30: FILES = 'files',
31: ITEMS = 'items',
32: CONSTS = 'consts',
33: CALLBACKS = 'callbacks',
34: ALL = 'all';
35:
36:
37: const NAMESPACE_SEPARATOR = "\x00";
38:
39:
40: private $storage;
41:
42:
43: private $namespace;
44:
45:
46: private $key;
47:
48:
49: private $data;
50:
51:
52: public function __construct(IStorage $storage, $namespace = NULL)
53: {
54: $this->storage = $storage;
55: $this->namespace = $namespace . self::NAMESPACE_SEPARATOR;
56: }
57:
58:
59: 60: 61: 62:
63: public function getStorage()
64: {
65: return $this->storage;
66: }
67:
68:
69: 70: 71: 72:
73: public function getNamespace()
74: {
75: return (string) substr($this->namespace, 0, -1);
76: }
77:
78:
79: 80: 81: 82: 83:
84: public function derive($namespace)
85: {
86: $derived = new static($this->storage, $this->namespace . $namespace);
87: return $derived;
88: }
89:
90:
91: 92: 93: 94: 95: 96:
97: public function load($key, $fallback = NULL)
98: {
99: $data = $this->storage->read($this->generateKey($key));
100: if ($data === NULL && $fallback) {
101: return $this->save($key, Callback::closure($fallback));
102: }
103: return $data;
104: }
105:
106:
107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123:
124: public function save($key, $data, array $dependencies = NULL)
125: {
126: $this->release();
127: $key = $this->generateKey($key);
128:
129: if ($data instanceof Nette\Callback || $data instanceof \Closure) {
130: $this->storage->lock($key);
131: $data = call_user_func_array($data, array(& $dependencies));
132: }
133:
134: if ($data === NULL) {
135: $this->storage->remove($key);
136: } else {
137: $this->storage->write($key, $data, $this->completeDependencies($dependencies, $data));
138: return $data;
139: }
140: }
141:
142:
143: private function completeDependencies($dp, $data)
144: {
145:
146: if (isset($dp[Cache::EXPIRATION])) {
147: $dp[Cache::EXPIRATION] = Nette\Utils\DateTime::from($dp[Cache::EXPIRATION])->format('U') - time();
148: }
149:
150:
151: if (isset($dp[self::FILES])) {
152: foreach (array_unique((array) $dp[self::FILES]) as $item) {
153: $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkFile'), $item, @filemtime($item));
154: }
155: unset($dp[self::FILES]);
156: }
157:
158:
159: if (isset($dp[self::ITEMS])) {
160: $dp[self::ITEMS] = array_unique(array_map(array($this, 'generateKey'), (array) $dp[self::ITEMS]));
161: }
162:
163:
164: if (isset($dp[self::CONSTS])) {
165: foreach (array_unique((array) $dp[self::CONSTS]) as $item) {
166: $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkConst'), $item, constant($item));
167: }
168: unset($dp[self::CONSTS]);
169: }
170:
171: if (!is_array($dp)) {
172: $dp = array();
173: }
174: return $dp;
175: }
176:
177:
178: 179: 180: 181: 182:
183: public function remove($key)
184: {
185: $this->save($key, NULL);
186: }
187:
188:
189: 190: 191: 192: 193: 194: 195: 196:
197: public function clean(array $conditions = NULL)
198: {
199: $this->release();
200: $this->storage->clean((array) $conditions);
201: }
202:
203:
204: 205: 206: 207: 208:
209: public function call($function)
210: {
211: $key = func_get_args();
212: $key[0] = Callback::toReflection($function);
213: return $this->load($key, function() use ($function, $key) {
214: return Callback::invokeArgs($function, array_slice($key, 1));
215: });
216: }
217:
218:
219: 220: 221: 222: 223: 224:
225: public function wrap($function, array $dependencies = NULL)
226: {
227: $cache = $this;
228: return function() use ($cache, $function, $dependencies) {
229: $key = array(Callback::toReflection($function), func_get_args());
230: $data = $cache->load($key);
231: if ($data === NULL) {
232: $data = $cache->save($key, Callback::invokeArgs($function, $key[1]), $dependencies);
233: }
234: return $data;
235: };
236: }
237:
238:
239: 240: 241: 242: 243:
244: public function start($key)
245: {
246: $data = $this->load($key);
247: if ($data === NULL) {
248: return new OutputHelper($this, $key);
249: }
250: echo $data;
251: }
252:
253:
254: 255: 256: 257: 258: 259:
260: protected function generateKey($key)
261: {
262: return $this->namespace . md5(is_scalar($key) ? $key : serialize($key));
263: }
264:
265:
266:
267:
268:
269: 270: 271:
272: public function offsetSet($key, $data)
273: {
274: $this->save($key, $data);
275: }
276:
277:
278: 279: 280:
281: public function offsetGet($key)
282: {
283: $key = is_scalar($key) ? (string) $key : serialize($key);
284: if ($this->key !== $key) {
285: $this->key = $key;
286: $this->data = $this->load($key);
287: }
288: return $this->data;
289: }
290:
291:
292: 293: 294:
295: public function offsetExists($key)
296: {
297: $this->release();
298: return $this->offsetGet($key) !== NULL;
299: }
300:
301:
302: 303: 304:
305: public function offsetUnset($key)
306: {
307: $this->save($key, NULL);
308: }
309:
310:
311: 312: 313:
314: public function release()
315: {
316: $this->key = $this->data = NULL;
317: }
318:
319:
320:
321:
322:
323: 324: 325: 326: 327:
328: public static function checkCallbacks($callbacks)
329: {
330: foreach ($callbacks as $callback) {
331: if (!call_user_func_array(array_shift($callback), $callback)) {
332: return FALSE;
333: }
334: }
335: return TRUE;
336: }
337:
338:
339: 340: 341: 342: 343: 344:
345: private static function checkConst($const, $value)
346: {
347: return defined($const) && constant($const) === $value;
348: }
349:
350:
351: 352: 353: 354: 355: 356:
357: private static function checkFile($file, $time)
358: {
359: return @filemtime($file) == $time;
360: }
361:
362: }
363: