1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle CMS |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\Cache; |
9
|
|
|
/** |
10
|
|
|
* Abstract class that simplifies creating cache engines without namespaces support |
11
|
|
|
* |
12
|
|
|
* This class implements methods: |
13
|
|
|
* * `::get()` |
14
|
|
|
* * `::set()` |
15
|
|
|
* * `::del()` |
16
|
|
|
* * `::clean()` |
17
|
|
|
* |
18
|
|
|
* And requires to implement simple low-level `protected` proxy-methods: |
19
|
|
|
* * `::available_internal()` |
20
|
|
|
* * `::get_internal()` |
21
|
|
|
* * `::set_internal()` |
22
|
|
|
* * `::del_internal()` |
23
|
|
|
* * `::clean_internal()` |
24
|
|
|
* * `::increment_internal()` |
25
|
|
|
* |
26
|
|
|
* The main benefit is that namespace support is provided by this class and target class only needs to implement trivial low-level methods |
27
|
|
|
*/ |
28
|
|
|
abstract class _Abstract_with_namespace extends _Abstract { |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $root_versions_cache = []; |
33
|
|
|
/** |
34
|
|
|
* Whether current cache engine is available (might be `false` if necessary extension is not installed or something similar) |
35
|
|
|
* |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
abstract protected function available_internal (); |
39
|
|
|
/** |
40
|
|
|
* @abstract |
41
|
|
|
* |
42
|
|
|
* @param string $item |
43
|
|
|
* |
44
|
|
|
* @return bool|mixed |
45
|
|
|
*/ |
46
|
|
|
abstract protected function get_internal ($item); |
47
|
|
|
/** |
48
|
|
|
* @abstract |
49
|
|
|
* |
50
|
|
|
* @param string $item |
51
|
|
|
* @param mixed $data |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
abstract protected function set_internal ($item, $data); |
56
|
|
|
/** |
57
|
|
|
* @abstract |
58
|
|
|
* |
59
|
|
|
* @param string $item |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
abstract protected function del_internal ($item); |
64
|
|
|
/** |
65
|
|
|
* @abstract |
66
|
|
|
* |
67
|
|
|
* @param string $item |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
abstract protected function increment_internal ($item); |
72
|
|
|
/** |
73
|
|
|
* @abstract |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
abstract protected function clean_internal (); |
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
function get ($item) { |
82
|
|
|
if (!$this->available_internal()) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
return $this->get_internal( |
86
|
|
|
$this->namespaces_imitation($item) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
function set ($item, $data) { |
93
|
|
|
if (!$this->available_internal()) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
return $this->set_internal( |
97
|
|
|
$this->namespaces_imitation($item), |
98
|
|
|
$data |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
104
|
|
|
function del ($item) { |
105
|
|
|
if (!$this->available_internal()) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
$this->del_internal($this->namespaces_imitation($item)); |
109
|
|
|
$this->increment_internal('/'.DOMAIN."/$item"); |
110
|
|
|
unset($this->root_versions_cache['/'.DOMAIN."/$item"]); |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
/** |
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
116
|
|
|
function clean () { |
117
|
|
|
if (!$this->available_internal()) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
return $this->clean_internal(); |
121
|
|
|
} |
122
|
|
|
/** |
123
|
|
|
* Namespaces imitation |
124
|
|
|
* |
125
|
|
|
* Accepts item as parameter, returns item string that uses namespaces (needed for fast deletion of large branches of cache elements). |
126
|
|
|
* |
127
|
|
|
* @param $item |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected function namespaces_imitation ($item) { |
132
|
|
|
$exploded = explode('/', $item); |
133
|
|
|
$count = count($exploded); |
134
|
|
|
if ($count > 1) { |
135
|
|
|
$item_path = DOMAIN; |
136
|
|
|
--$count; |
137
|
|
|
/** @noinspection ForeachInvariantsInspection */ |
138
|
|
|
for ($i = 0; $i < $count; ++$i) { |
139
|
|
|
$item_path .= '/'.$exploded[$i]; |
140
|
|
|
if (!$i && isset($this->root_versions_cache["/$item_path"])) { |
141
|
|
|
$exploded[$i] .= '/'.$this->root_versions_cache["/$item_path"]; |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
$version = $this->get_internal("/$item_path"); |
145
|
|
|
if ($version === false) { |
146
|
|
|
$this->set_internal("/$item_path", 0); |
147
|
|
|
$version = 0; |
148
|
|
|
} |
149
|
|
|
$exploded[$i] .= "/$version"; |
150
|
|
|
if (!$i) { |
151
|
|
|
$this->root_versions_cache["/$item_path"] = $version; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
return DOMAIN.'/'.implode('/', $exploded); |
155
|
|
|
} |
156
|
|
|
return DOMAIN."/$item"; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|