use standard SplObserver
[m6w6/pq-gateway] / lib / pq / Gateway / Table / StaticCache.php
1 <?php
2
3 namespace pq\Gateway\Table;
4
5 class StaticCache implements CacheInterface
6 {
7 protected static $cache = array();
8
9 /**
10 * @inheritdoc
11 * @param string $key
12 * @param bool $exists
13 * @return mixed
14 */
15 function get($key, &$exists = null) {
16 if (($exists = array_key_exists($key, static::$cache))) {
17 list($ttl, $data) = static::$cache[$key];
18 if ($ttl && $ttl < time()) {
19 unset(static::$cache[$key]);
20 $exists = false;
21 return null;
22 }
23 return $data;
24 }
25 }
26
27 /**
28 * @inheritdoc
29 * @param string $key
30 * @param mixed $val
31 * @param int $ttl
32 * @return \pq\Gateway\Table\StaticCache
33 */
34 function set($key, $val, $ttl = 0) {
35 static::$cache[$key] = array(
36 $ttl ? $ttl + time() : 0,
37 $val
38 );
39 return $this;
40 }
41
42 /**
43 * @inheritdoc
44 * @param string $key
45 * @return \pq\Gateway\Table\StaticCache
46 */
47 function del($key) {
48 unset(static::$cache[$key]);
49 return $this;
50 }
51 }