-
Notifications
You must be signed in to change notification settings - Fork 1
/
bxcache.php
270 lines (244 loc) · 9.57 KB
/
bxcache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* @author Etienne Bagnoud <[email protected]>
* @license MIT
* @copyright 2023 Etienne Bagnoud
* @todo Still in heavy developpment, not ready for usage as is.
*
* Manage caching of external bexio data into two caches :
* - Short time cache to avoid hiting ratelimit (put,get,delete)
* - Long time cache in case bexio is down (store,load,remove)
*/
class BexioCache {
protected $cache; // short time cache
protected $path; // long time cache
protected $duration = 30;
function __construct (Memcached $memcache, string $path, Int $duration = 30) {
$this->cache = $memcache;
$this->path = realpath($path);
$this->duration = $duration;
if (!is_writable($this->path)) {
throw new Exception ('Cache is not writable');
}
}
function content_hash (string $content):string {
return hash('xxh3', $content);
}
function ref_hash (string $reference):string {
return hash('xxh3', $reference) . '.' . hash('crc32c', $reference);
}
function ref_to_path (string $reference):string {
$base = $this->ref_hash($reference);
$dir = $this->path . '/' . substr($base, 0, 2) . '/' . substr($base, 2, 2) . '/' . $base . '/';
if (!is_dir($dir)) {
@mkdir($dir, 0770, true);
}
return $dir;
}
function cmp_content (string $reference, string $content) {
$dirname = $this->ref_to_path($reference);
if (!is_file($dirname . '/hash')) { return false; }
$hash = $this->content_hash($content);
$storedHash = file_get_contents($dirname . '/hash');
if ($storedHash === $hash) { return true; }
return false;
}
/**
* When set into collection, this will be for iterating only so performance
* penalty is not a concern when reading back. This is to be used in some
* specific emergency case where slowness is not a problem.
*/
function set_to_collection (string $reference, string $path):bool {
$parts = explode('/', $reference);
$collection = array_shift($parts);
$item = array_shift($parts);
if (str_starts_with($item, '#')) { return true; } // query or listing, don't structure into collection/item
if (!is_dir($this->path . '/' . $collection)) {
mkdir($this->path . '/' . $collection);
}
$file = basename($path);
if (!is_link($this->path . '/' . $collection . '/' . $file)) {
return symlink($path, $this->path . '/' . $collection . '/' . $file);
}
return true;
}
function get_age (string $reference):int {
$dirname = $this->ref_to_path($reference);
if (!is_file($dirname . '/hash')) { return -1; }
$mtime = filemtime($dirname . '/hash');
if ($mtime === false) { return -1; }
return time() - $mtime;
}
function iterate_collection (string $collection):Generator {
$dh = opendir($this->path . '/' . $collection);
if (!$dh) { return; }
while(($file = readdir($dh)) !== false) {
if (is_file($this->path . '/' . $collection . '/' . $file . '/deleted')) { continue; }
if (!is_file($this->path . '/' . $collection . '/' . $file . '/content')) { continue; }
yield file_get_contents($this->path . '/' . $collection . '/' . $file . '/content');
}
closedir($dh);
}
function store (string $reference, string $content):bool {
$dirname = $this->ref_to_path($reference);
$this->set_to_collection($reference, $dirname);
if (file_put_contents($dirname . '/content', $content)) {
return file_put_contents($dirname . '/hash', $this->content_hash($content)) !== false;
}
return false;
}
function load (string $reference):string|false {
$dirname = $this->ref_to_path($reference);
return file_get_contents($dirname . '/content');
}
function remove (string $reference):bool {
$dirname = $this->ref_to_path($reference);
return file_put_contents($dirname . '/deleted', strval(time())) !== false;
}
function put (string $reference, string $content):bool {
$reference = $this->ref_hash($reference);
return $this->cache->set($reference, $content, $this->duration);
}
function get (string $reference):string|false {
$reference = $this->ref_hash($reference);
return $this->cache->get($reference);
}
function delete (string $reference):bool {
$reference = $this->ref_hash($reference);
return $this->cache->delete($reference);
}
}
/* Trait to compose a class where you can search in cache like it would be the
* actual Bexio API.
*/
trait BexioJSONCache {
protected $bxcache;
function read_cache (string $reference):Array {
if ($this->bxcache->get_age($reference) <= -1) { return [0, false]; }
$content = $this->bxcache->load($reference);
if ($content === false) { return [0, false]; }
[$count, $content] = explode("\n", $content, 2);
return [intval($count), $content];
}
function search_cache ($collection, $reference):Generator {
if ($this->bxcache->get_age($reference) <= -1) { return; }
$content = $this->bxcache->load($reference);
if ($content === false) { return; }
[$count, $content] = explode("\n", $content, 2);
$content = json_decode($content);
foreach($content as $id) {
$object = $this->read_cache($collection . '/' . $id);
if ($object[0] <= 0) { continue; }
if ($object[0] > 1) {
$items = json_decode($object[1]);
foreach($items as $item) { yield json_encode($item); }
continue;
}
yield $object[1];
}
}
function _cmp_value ($op, $v1, $v2 = '') {
$strv1 = strval($v1);
$strv2 = strval($v2);
switch ($op) {
case '=':
case 'equal':
return strcasecmp($strv1, $strv2) === 0;
case '!=':
case 'not_equal':
return strcasecmp($strv1, $strv2) !== 0;
case '>':
case 'greater_than':
return strcasecmp($strv1, $strv2) === 1;
case '<':
case 'less_than':
return strcasecmp($strv1, $strv2) === -1;
case '>=':
case 'greater_equal':
$x = strcasecmp($strv1, $strv2);
return $x >= 0;
case '<=':
case 'less_equal':
$x = strcasecmp($strv1, $strv2);
return $x <= 0;
case 'like':
return stristr($strv1, $strv2) !== false;
case 'not_like':
return stristr($strv1, $strv2) === false;
case 'is_null':
return is_null($v1);
case 'not_null':
return !is_null($v1);
case 'in':
$v2 = json_decode($v2);
foreach ($v2 as $item) {
if ($this->_cmp_value($strv1, strval($item))) {
return true;
}
}
return false;
case 'not_in':
$v2 = json_decode($v2);
foreach ($v2 as $item) {
if ($this->_cmp_value($strv1, strval($item))) {
return false;
}
}
return true;
}
}
/**
* Search cache a bit like it would be done on real server
*/
function query_cache (string $collection, array $query, bool $use_or = false):Generator {
foreach($this->bxcache->iterate_collection($collection) as $object) {
[$count, $content] = explode("\n", $object);
if ($count <= 0) { continue; }
if ($count > 1) {
$items = json_decode($content);
foreach($items as $item) {
$filter = $use_or;
foreach ($query as $q) {
if ($use_or) {
if ($this->_cmp_value($q['criteria'], $item->{$q['field']}, $q['value'])) {
$filter = false;
break;
}
continue;
}
if (!$this->_cmp_value($q['criteria'], $item->{$q['field']}, $q['value'])) {
$filter = true;
break;
}
}
if ($filter) { continue; }
yield json_encode($item);
}
continue;
}
$object = json_decode($content);
$filter = $use_or;
foreach ($query as $q) {
if ($use_or) {
if ($this->_cmp_value($q['criteria'], $object->{$q['field']}, $q['value'])) {
$filter = false;
break;
}
continue;
}
if (!$this->_cmp_value($q['criteria'], $object->{$q['field']}, $q['value'])) {
$filter = true;
break;
}
}
if ($filter) { continue; }
yield $content;
}
}
function store_cache (string $reference, string $content, int $items) {
$content = $items . "\n" . $content;
if (!$this->bxcache->cmp_content($reference, $content)) {
$this->bxcache->store($reference, $content);
}
}
}