-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFmapi_cache.php
365 lines (249 loc) · 7.79 KB
/
Fmapi_cache.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
* FileMaker-Data-API-PHP-Wrapper
* Data Caching extension
* Sheldon Lendrum
*/
class Fmapi_cache {
/**
* Supported caching methods:
* - 'file'
* - 'mongodb'
*/
public $cache_type = 'file';
/**
* cache path, only used for file based storage.
* eg: '../cache/';
*/
public $cache_path = '';
/**
* How many hours to cache the result for.
*/
public $expires = 4;
/**
* Force cache expiration and refresh the result
*/
public $cache_bust = FALSE;
/**
* Query to create cache reference.
*/
public $query = '';
/**
* Result to cache
*/
public $result = '';
/**
* MongoDB Database connection string
* only required for Mongo connections.
*/
protected $mongodb_dsn = '';
public function __construct()
{
}
public function type($cache_type = NULL)
{
$this->cache_type = $cache_type;
return $this;
}
public function path($cache_path = NULL)
{
$this->cache_path = $cache_path;
return $this;
}
public function expires($expires = NULL)
{
$this->expires = $expires;
return $this;
}
public function bust($cache_bust = NULL)
{
$this->cache_bust = $cache_bust;
return $this;
}
public function query($query = NULL)
{
$this->query = $query;
return $this;
}
public function result($result = NULL)
{
$this->result = $result;
return $this;
}
/**
* Set your Mongo connection dsn string.
* * eg: 'mongodb://user:[email protected]:27017/?ssl=true&replicaSet=replica&authSource=admin'
*/
public function dsn($mongodb_dsn = NULL)
{
$this->mongodb_dsn = $mongodb_dsn;
return $this;
}
/**
* Store a cached result, or 401 (404) result.
*/
public function store()
{
if(empty($this->expires)) return FALSE;
if(empty($this->query)) return FALSE;
if(empty($this->result)) return FALSE;
if($this->cache_type == 'file') {
if(empty($this->cache_path)) {
if(!is_dir($this->cache_path)) {
$cache = new Fmapi_file_cache();
$cache->store($this->expires, $this->query, $this->result);
}
}
}
if($this->cache_type == 'mongodb') {
if(!empty($this->mongodb_dsn)) {
$cache = new Fmapi_mongodb_cache($this->mongodb_dsn);
$cache->store($this->expires, $this->query, $this->result);
}
}
/**
* Reset cache object.
*/
$this->expires = 4;
$this->query = '';
$this->result = '';
$this->cache_bust = FALSE;
return TRUE;
}
/**
* Get for an existing, non-expired cached result.
*/
public function retrieve()
{
if(empty($this->query)) return FALSE;
if($this->cache_type == 'file') {
if(empty($this->cache_path)) {
if(!is_dir($this->cache_path)) {
$cache = new Fmapi_file_cache($this->cache_path);
return $cache->retrieve($this->query, $this->cache_bust);
}
}
}
if($this->cache_type == 'mongodb') {
if(!empty($this->mongodb_dsn)) {
$cache = new Fmapi_mongodb_cache($this->mongodb_dsn);
return $cache->retrieve($this->query, $this->cache_bust);
}
}
return FALSE;
}
}
/**
* File based storage.
*/
class Fmapi_file_cache {
public $cache_path;
public function __construct($cache_path = NULL)
{
$this->cache_path = $cache_path;
}
public function store($cache_hours = NULL, $cache_object = NULL, $result = NULL)
{
if(empty($this->cache_path)) return FALSE;
if(!is_dir($this->cache_path)) return FALSE;
$cache_json = json_encode($cache_object);
$cache_md5 = md5($cache_json) .'.cache';
if(file_exists($this->cache_path . $cache_md5)) {
unlink($this->cache_path . $cache_md5);
}
$cache_object['hash'] = $cache_md5;
$cache_object['data'] = $result;
$cache_object['expires'] = strtotime('+ '. $cache_hours .' hours');
/**
* Cache a no records found result
* But only for a shorter time.
*/
if(!empty($result['messages'][0]) and $result['messages'][0]['code'] == '401') {
$cache_object['expires'] = strtotime('+ 30 minutes');
}
file_put_contents($this->cache_path . $cache_md5, json_encode($cache_object));
}
public function retrieve($cache_object = NULL, $cache_bust = FALSE)
{
if(empty($this->cache_path)) return FALSE;
$cache_json = json_encode($cache_object);
$cache_md5 = md5($cache_json) .'.cache';
if(!file_exists($this->cache_path . $cache_md5)) {
return FALSE;
}
if($cache_bust === TRUE) {
unlink($this->cache_path . $cache_md5);
return FALSE;
}
$cache = file_get_contents($this->cache_path . $cache_md5);
$cache = json_decode($cache);
if(time() > $cache->expires) {
unlink($this->cache_path . $cache_md5);
return FALSE;
}
$cache->data = json_encode($cache->data);
$cache->data = json_decode($cache->data, TRUE);
$result = [];
if(is_array($cache->data)) {
foreach($cache->data as $index => $object) {
$result[$index] = is_array($object) ? (object)$object : $object;
}
}
return $result;
}
}
/**
* Mongodb Cache extension
* You must set up your Mongo DB Connection
*/
class Fmapi_mongodb_cache {
public $mongodb;
public $collection = 'cache';
public function __construct($mongodb_dsn = NULL)
{
$this->mongodb = new MongoDB\Client($mongodb_dsn);
}
public function store($cache_hours = NULL, $cache_object = NULL, $result = NULL)
{
$cache_json = json_encode($cache_object);
$cache_md5 = md5($cache_json);
$this->mongodb->{$this->collection}->deleteMany(['hash' => $cache_md5]);
$cache_object['hash'] = $cache_md5;
$cache_object['data'] = $result;
$cache_object['expires'] = strtotime('+ '. $cache_hours .' hours');
/**
* Cache a no records found result
* But only for a shorter time.
*/
if(!empty($result['messages'][0]) and $result['messages'][0]['code'] == '401') {
$cache_object['expires'] = strtotime('+ 30 minutes');
}
$this->mongodb->insert($this->collection, $cache_object);
}
public function retrieve($cache_object = NULL, $cache_bust = FALSE)
{
$cache_json = json_encode($cache_object);
$cache_md5 = md5($cache_json);
$cache = $this->mongodb->findOne($this->collection, ['hash' => $cache_md5]);
if(empty($cache)) {
return FALSE;
}
if($cache_bust === TRUE) {
$this->mongodb->{$this->collection}->deleteOne($cache_object);
return FALSE;
}
if(time() > $cache->expires) {
$this->mongodb->{$this->collection}->deleteOne($cache_object);
return FALSE;
}
$cache->data = json_encode($cache->data);
$cache->data = json_decode($cache->data, TRUE);
$result = [];
if(is_array($cache->data)) {
foreach($cache->data as $index => $object) {
$result[$index] = is_array($object) ? (object)$object : $object;
}
}
return $result;
}
}