|
1 | 1 | <?php |
2 | | - |
3 | 2 | namespace ActiveRecord; |
4 | | - |
5 | 3 | use Closure; |
6 | 4 |
|
7 | 5 | /** |
|
12 | 10 | */ |
13 | 11 | class Cache |
14 | 12 | { |
15 | | - public static $adapter = null; |
16 | | - public static $options = []; |
17 | | - |
18 | | - /** |
19 | | - * Initializes the cache. |
20 | | - * |
21 | | - * With the $options array it's possible to define: |
22 | | - * - expiration of the key, (time in seconds) |
23 | | - * - a namespace for the key |
24 | | - * |
25 | | - * this last one is useful in the case two applications use |
26 | | - * a shared key/store (for instance a shared Memcached db) |
27 | | - * |
28 | | - * Ex: |
29 | | - * $cfg_ar = ActiveRecord\Config::instance(); |
30 | | - * $cfg_ar->set_cache('memcache://localhost:11211',array('namespace' => 'my_cool_app', |
31 | | - * 'expire' => 120 |
32 | | - * )); |
33 | | - * |
34 | | - * In the example above all the keys expire after 120 seconds, and the |
35 | | - * all get a postfix 'my_cool_app'. |
36 | | - * |
37 | | - * (Note: expiring needs to be implemented in your cache store.) |
38 | | - * |
39 | | - * @param string $url URL to your cache server |
40 | | - * @param array $options Specify additional options |
41 | | - */ |
42 | | - public static function initialize($url, $options=[]) |
43 | | - { |
44 | | - if ($url) { |
45 | | - $url = parse_url($url); |
46 | | - $file = ucwords(Inflector::instance()->camelize($url['scheme'])); |
47 | | - $class = "ActiveRecord\\$file"; |
48 | | - require_once __DIR__ . "/cache/$file.php"; |
49 | | - static::$adapter = new $class($url); |
50 | | - } else { |
51 | | - static::$adapter = null; |
52 | | - } |
53 | | - |
54 | | - static::$options = array_merge(['expire' => 30, 'namespace' => ''], $options); |
55 | | - } |
56 | | - |
57 | | - public static function flush() |
58 | | - { |
59 | | - if (static::$adapter) { |
60 | | - static::$adapter->flush(); |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - /** |
65 | | - * Attempt to retrieve a value from cache using a key. If the value is not found, then the closure method |
66 | | - * will be invoked, and the result will be stored in cache using that key. |
67 | | - * |
68 | | - * @param $key |
69 | | - * @param $closure |
70 | | - * @param $expire in seconds |
71 | | - * |
72 | | - * @return mixed |
73 | | - */ |
74 | | - public static function get($key, $closure, $expire=null) |
75 | | - { |
76 | | - if (!static::$adapter) { |
77 | | - return $closure(); |
78 | | - } |
79 | | - |
80 | | - if (is_null($expire)) { |
81 | | - $expire = static::$options['expire']; |
82 | | - } |
83 | | - |
84 | | - $key = static::get_namespace() . $key; |
85 | | - |
86 | | - if (!($value = static::$adapter->read($key))) { |
87 | | - static::$adapter->write($key, ($value = $closure()), $expire); |
88 | | - } |
89 | | - |
90 | | - return $value; |
91 | | - } |
92 | | - |
93 | | - public static function set($key, $var, $expire=null) |
94 | | - { |
95 | | - if (!static::$adapter) { |
96 | | - return; |
97 | | - } |
98 | | - |
99 | | - if (is_null($expire)) { |
100 | | - $expire = static::$options['expire']; |
101 | | - } |
102 | | - |
103 | | - $key = static::get_namespace() . $key; |
104 | | - |
105 | | - return static::$adapter->write($key, $var, $expire); |
106 | | - } |
107 | | - |
108 | | - public static function delete($key) |
109 | | - { |
110 | | - if (!static::$adapter) { |
111 | | - return; |
112 | | - } |
113 | | - |
114 | | - $key = static::get_namespace() . $key; |
115 | | - |
116 | | - return static::$adapter->delete($key); |
117 | | - } |
118 | | - |
119 | | - private static function get_namespace() |
120 | | - { |
121 | | - return (isset(static::$options['namespace']) && strlen(static::$options['namespace']) > 0) ? (static::$options['namespace'] . '::') : ''; |
122 | | - } |
| 13 | + static $adapter = null; |
| 14 | + static $options = array(); |
| 15 | + |
| 16 | + /** |
| 17 | + * Initializes the cache. |
| 18 | + * |
| 19 | + * With the $options array it's possible to define: |
| 20 | + * - expiration of the key, (time in seconds) |
| 21 | + * - a namespace for the key |
| 22 | + * |
| 23 | + * this last one is useful in the case two applications use |
| 24 | + * a shared key/store (for instance a shared Memcached db) |
| 25 | + * |
| 26 | + * Ex: |
| 27 | + * $cfg_ar = ActiveRecord\Config::instance(); |
| 28 | + * $cfg_ar->set_cache('memcache://localhost:11211',array('namespace' => 'my_cool_app', |
| 29 | + * 'expire' => 120 |
| 30 | + * )); |
| 31 | + * |
| 32 | + * In the example above all the keys expire after 120 seconds, and the |
| 33 | + * all get a postfix 'my_cool_app'. |
| 34 | + * |
| 35 | + * (Note: expiring needs to be implemented in your cache store.) |
| 36 | + * |
| 37 | + * @param string $url URL to your cache server |
| 38 | + * @param array $options Specify additional options |
| 39 | + */ |
| 40 | + public static function initialize($url, $options=array()) |
| 41 | + { |
| 42 | + if ($url) |
| 43 | + { |
| 44 | + $url = parse_url($url); |
| 45 | + $file = ucwords(Inflector::instance()->camelize($url['scheme'])); |
| 46 | + $class = "ActiveRecord\\$file"; |
| 47 | + require_once __DIR__ . "/cache/$file.php"; |
| 48 | + static::$adapter = new $class($url); |
| 49 | + } |
| 50 | + else |
| 51 | + static::$adapter = null; |
| 52 | + |
| 53 | + static::$options = array_merge(array('expire' => 30, 'namespace' => ''),$options); |
| 54 | + } |
| 55 | + |
| 56 | + public static function flush() |
| 57 | + { |
| 58 | + if (static::$adapter) |
| 59 | + static::$adapter->flush(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Attempt to retrieve a value from cache using a key. If the value is not found, then the closure method |
| 64 | + * will be invoked, and the result will be stored in cache using that key. |
| 65 | + * @param $key |
| 66 | + * @param $closure |
| 67 | + * @param $expire in seconds |
| 68 | + * @return mixed |
| 69 | + */ |
| 70 | + public static function get($key, $closure, $expire=null) |
| 71 | + { |
| 72 | + if (!static::$adapter) |
| 73 | + return $closure(); |
| 74 | + |
| 75 | + if (is_null($expire)) |
| 76 | + { |
| 77 | + $expire = static::$options['expire']; |
| 78 | + } |
| 79 | + |
| 80 | + $key = static::get_namespace() . $key; |
| 81 | + |
| 82 | + if (!($value = static::$adapter->read($key))) |
| 83 | + static::$adapter->write($key, ($value = $closure()), $expire); |
| 84 | + |
| 85 | + return $value; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param $key |
| 90 | + * @param $var |
| 91 | + * @param null $expire |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + public static function set($key, $var, $expire=null) |
| 95 | + { |
| 96 | + if (!static::$adapter) |
| 97 | + return; |
| 98 | + |
| 99 | + if (is_null($expire)) |
| 100 | + { |
| 101 | + $expire = static::$options['expire']; |
| 102 | + } |
| 103 | + |
| 104 | + $key = static::get_namespace() . $key; |
| 105 | + return static::$adapter->write($key, $var, $expire); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param $key |
| 110 | + * @return void |
| 111 | + */ |
| 112 | + public static function delete($key) |
| 113 | + { |
| 114 | + if (!static::$adapter) |
| 115 | + return; |
| 116 | + |
| 117 | + $key = static::get_namespace() . $key; |
| 118 | + return static::$adapter->delete($key); |
| 119 | + } |
| 120 | + |
| 121 | + private static function get_namespace() |
| 122 | + { |
| 123 | + return (isset(static::$options['namespace']) && strlen(static::$options['namespace']) > 0) ? (static::$options['namespace'] . "::") : ""; |
| 124 | + } |
123 | 125 | } |
0 commit comments