1 | <?php |
||
15 | class Cache { |
||
16 | use Singleton; |
||
17 | /** |
||
18 | * Cache state |
||
19 | * |
||
20 | * @var bool |
||
21 | */ |
||
22 | protected $state = true; |
||
23 | /** |
||
24 | * Initialization state |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected $init = false; |
||
28 | /** |
||
29 | * Name of cache engine |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $engine; |
||
33 | /** |
||
34 | * Instance of cache engine object |
||
35 | * |
||
36 | * @var Cache\_Abstract |
||
37 | */ |
||
38 | protected $engine_instance; |
||
39 | /** |
||
40 | * Initialization, creating cache engine instance |
||
41 | */ |
||
42 | 42 | protected function construct () { |
|
49 | /** |
||
50 | * Returns instance for simplified work with cache, when using common prefix |
||
51 | * |
||
52 | * @param string $prefix |
||
53 | * |
||
54 | * @return Prefix |
||
55 | */ |
||
56 | 26 | static function prefix ($prefix) { |
|
57 | 26 | return new Prefix($prefix); |
|
58 | } |
||
59 | /** |
||
60 | * Get item from cache |
||
61 | * |
||
62 | * If item not found and $callback parameter specified - closure must return value for item. This value will be set for current item, and returned. |
||
63 | * |
||
64 | * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
||
65 | * @param callable|null $callback |
||
66 | * |
||
67 | * @return false|mixed Returns item on success of <b>false</b> on failure |
||
68 | */ |
||
69 | 42 | function get ($item, $callback = null) { |
|
70 | 42 | if (!$this->state) { |
|
71 | 10 | return is_callable($callback) ? $callback() : false; |
|
72 | } |
||
73 | 42 | $item = trim($item, '/'); |
|
74 | 42 | $data = $this->engine_instance->get($item); |
|
75 | 42 | if ($data === false && is_callable($callback)) { |
|
76 | 24 | $data = $callback(); |
|
77 | 24 | if ($data !== false) { |
|
78 | 24 | $this->set($item, $data); |
|
79 | } |
||
80 | } |
||
81 | 42 | return $data; |
|
82 | } |
||
83 | /** |
||
84 | * Put or change data of cache item |
||
85 | * |
||
86 | * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
||
87 | * @param mixed $data |
||
88 | * |
||
89 | * @return bool |
||
90 | */ |
||
91 | 26 | function set ($item, $data) { |
|
92 | 26 | if (is_object($this->engine_instance)) { |
|
93 | 26 | $this->engine_instance->del($item); |
|
94 | } |
||
95 | 26 | if (!$this->state) { |
|
96 | return true; |
||
97 | } |
||
98 | 26 | $item = trim($item, '/'); |
|
99 | 26 | return $this->engine_instance->set($item, $data); |
|
100 | } |
||
101 | /** |
||
102 | * Delete item from cache |
||
103 | * |
||
104 | * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | 22 | function del ($item) { |
|
109 | 22 | if (empty($item)) { |
|
110 | return false; |
||
111 | } |
||
112 | /** |
||
113 | * Cache cleaning instead of removing when root specified |
||
114 | */ |
||
115 | 22 | if ($item == '/') { |
|
116 | return $this->clean(); |
||
117 | } |
||
118 | 22 | if (is_object($this->engine_instance)) { |
|
119 | 22 | $item = trim($item, '/'); |
|
120 | 22 | return $this->engine_instance->del($item); |
|
121 | } else { |
||
122 | return false; |
||
123 | } |
||
124 | } |
||
125 | /** |
||
126 | * Clean cache by deleting all items |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | 10 | function clean () { |
|
131 | 10 | if (is_object($this->engine_instance)) { |
|
132 | 10 | return $this->engine_instance->clean(); |
|
133 | } else { |
||
134 | return false; |
||
135 | } |
||
136 | } |
||
137 | /** |
||
138 | * Cache state enabled/disabled |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | 10 | function cache_state () { |
|
143 | 10 | return $this->state; |
|
144 | } |
||
145 | /** |
||
146 | * Disable cache |
||
147 | */ |
||
148 | 10 | function disable () { |
|
151 | /** |
||
152 | * Get item from cache |
||
153 | * |
||
154 | * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
||
155 | * |
||
156 | * @return false|mixed Returns item on success of <b>false</b> on failure |
||
157 | */ |
||
158 | 40 | function __get ($item) { |
|
161 | /** |
||
162 | * Put or change data of cache item |
||
163 | * |
||
164 | * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
||
165 | * @param mixed $data |
||
166 | */ |
||
167 | 14 | function __set ($item, $data) { |
|
170 | /** |
||
171 | * Delete item from cache |
||
172 | * |
||
173 | * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i> |
||
174 | */ |
||
175 | 14 | function __unset ($item) { |
|
178 | } |
||
179 |