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