-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternalCache.php
More file actions
318 lines (300 loc) · 10.8 KB
/
ExternalCache.php
File metadata and controls
318 lines (300 loc) · 10.8 KB
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
<?php
/** ---------------------------------------------------------------------
* app/lib/Cache/ExternalCache.php : provides caching using external facilities
* ----------------------------------------------------------------------
* CollectiveAccess
* Open-source collections management software
* ----------------------------------------------------------------------
*
* Software by Whirl-i-Gig (http://www.whirl-i-gig.com)
* Copyright 2014-2018 Whirl-i-Gig
*
* For more information visit http://www.CollectiveAccess.org
*
* This program is free software; you may redistribute it and/or modify it under
* the terms of the provided license as published by Whirl-i-Gig
*
* CollectiveAccess is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTIES whatsoever, including any implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* This source code is free and modifiable under the terms of
* GNU General Public License. (http://www.gnu.org/copyleft/gpl.html). See
* the "license.txt" file for details, or visit the CollectiveAccess web site at
* http://www.CollectiveAccess.org
*
* @package CollectiveAccess
* @subpackage Cache
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License version 3
*
* ----------------------------------------------------------------------
*/
require_once(__CA_LIB_DIR__."/Cache/MemoryCache.php");
class ExternalCache {
# ------------------------------------------------
/**
*
*/
private static $cache;
/**
*
*/
private static $invalidation_method = null;
# ------------------------------------------------
/**
* Initialize cache for given namespace if necessary
* Namespace declaration is optionale
* @throws ExternalCacheInvalidParameterException
*/
private static function init() {
if(self::cacheExists()) {
return true;
} else {
if(self::$cache = self::getCacheObject()) {
return true;
} else {
return false;
}
}
}
# ------------------------------------------------
/**
* Does the cache object exist?
* @return bool
*/
private static function cacheExists() {
return (isset(self::$cache) && (self::$cache instanceof Stash\Pool));
}
# ------------------------------------------------
/**
* Set cache invalidation mode. See http://www.stashphp.com/Invalidation.html for a description
* of each method. The default is Invalidation::PRECOMPUTE which is the best choice for most.
* most situations. Invalidation::NONE is required for testing with short TTL's to avoid
* side effects from "cache stampede" protection.
* @return bool
*/
public static function setInvalidationMode($pn_method) {
if (!in_array($pn_method, [Stash\Invalidation::NONE, Stash\Invalidation::PRECOMPUTE, Stash\Invalidation::OLD, Stash\Invalidation::VALUE, Stash\Invalidation::SLEEP])) { return false; }
self::$invalidation_method = $pn_method;
return true;
}
# ------------------------------------------------
/**
*
* @return string
*/
private static function filterKey($ps_key) {
return str_replace("/", "_", $ps_key);
}
# ------------------------------------------------
private static function checkParameters($ps_namespace, $ps_key) {
if(!is_string($ps_namespace)) {
throw new ExternalCacheInvalidParameterException('Namespace has to be a string');
}
if(!preg_match("/^[A-Za-z0-9_]+$/", $ps_namespace)) {
throw new ExternalCacheInvalidParameterException('Caching namespace must only contain alphanumeric characters, dashes and underscores');
}
if(!$ps_key) {
throw new ExternalCacheInvalidParameterException('Key cannot be empty');
}
}
# ------------------------------------------------
/**
* Get cache object from static object property
* @return Doctrine\Common\Cache\CacheProvider
*/
private static function getCache() {
if(isset(self::$cache)) {
return self::$cache;
} else {
return null;
}
}
# ------------------------------------------------
/**
* Get a cache item
* @param string $ps_key
* @param string $ps_namespace
* @return mixed
* @throws ExternalCacheInvalidParameterException
*/
public static function fetch($ps_key, $ps_namespace='default') {
if(!self::init()) { return false; }
$ps_key = self::filterKey($ps_key);
self::checkParameters($ps_namespace, $ps_key);
$item = self::getCache()->getItem(self::makeKey($ps_key, $ps_namespace));
if(!is_null(self::$invalidation_method)) { $item->setInvalidationMethod(self::$invalidation_method); }
return $item->isMiss() ? null : $item->get();
}
# ------------------------------------------------
/**
* Puts data into the cache. Overwrites existing items!
* @param string $ps_key
* @param mixed $pm_data
* @param string $ps_namespace
* @param int $pn_ttl
* @return bool
* @throws ExternalCacheInvalidParameterException
*/
public static function save($ps_key, $pm_data, $ps_namespace='default', $pn_ttl=null) {
if(!self::init()) { return false; }
$ps_key = self::filterKey($ps_key);
self::checkParameters($ps_namespace, $ps_key);
if(!defined('__CA_CACHE_TTL__')) {
define('__CA_CACHE_TTL__', 3600);
}
$pool = self::getCache();
$item = $pool->getItem(self::makeKey($ps_key, $ps_namespace));
$item->expiresAfter((!is_null($pn_ttl) ? $pn_ttl : __CA_CACHE_TTL__));
$item->set($pm_data);
$pool->save($item);
return true;
}
# ------------------------------------------------
/**
* Does a given cache key exist?
* @param string $ps_key
* @param string $ps_namespace
* @return bool
* @throws ExternalCacheInvalidParameterException
*/
public static function contains($ps_key, $ps_namespace='default') {
if(!self::init()) { return false; }
$ps_key = self::filterKey($ps_key);
self::checkParameters($ps_namespace, $ps_key);
$item = self::getCache()->getItem(self::makeKey($ps_key, $ps_namespace));
if(!is_null(self::$invalidation_method)) { $item->setInvalidationMethod(self::$invalidation_method); }
return !$item->isMiss();
}
# ------------------------------------------------
/**
* Remove a given key from cache
* @param string $ps_key
* @param string $ps_namespace
* @return bool
* @throws ExternalCacheInvalidParameterException
*/
public static function delete($ps_key, $ps_namespace='default') {
if(!self::init()) { return false; }
$ps_key = self::filterKey($ps_key);
self::checkParameters($ps_namespace, $ps_key);
self::getCache()->deleteItem(self::makeKey($ps_key, $ps_namespace));
return true;
}
# ------------------------------------------------
/**
* Flush cache
* @throws MemoryCacheInvalidParameterException
*/
public static function flush($ps_namespace=null) {
try {
if(!self::init()) { return false; }
if ($ps_namespace) {
self::getCache()->deleteItem($z=substr(__CA_APP_TYPE__, 0, 4).'/'.$ps_namespace);
} else {
self::getCache()->clear();
}
} catch(UnexpectedValueException $e) {
// happens during the installer pre tasks when we just purge everything in app/tmp without asking.
// At that point we have existing objects in self::$cache that can't deal with that.
// We do nothing here because the directory is re-created automatically the next time someone
// tries to access the cache.
}
}
# ------------------------------------------------
# Helpers
# ------------------------------------------------
private static function getCacheObject() {
if(!defined('__CA_CACHE_BACKEND__')) {
define('__CA_CACHE_BACKEND__', 'file');
}
switch(__CA_CACHE_BACKEND__) {
case 'memcached':
return self::getMemcachedObject();
case 'redis':
return self::getRedisObject();
case 'apc':
return self::getApcObject();
case 'sqlite':
return self::getSqliteObject();
case 'file':
default:
return self::getFileCacheObject();
}
}
# ------------------------------------------------
private static function getCacheDirectory() {
$vs_cache_base_dir = (defined('__CA_CACHE_FILEPATH__') ? __CA_CACHE_FILEPATH__ : __CA_APP_DIR__.DIRECTORY_SEPARATOR.'tmp');
$vs_cache_dir = $vs_cache_base_dir.DIRECTORY_SEPARATOR.__CA_APP_NAME__.'Cache';
if(!file_exists($vs_cache_dir)) { @mkdir($vs_cache_dir); }
return $vs_cache_dir;
}
# ------------------------------------------------
private static function getFileCacheObject(){
try {
$driver = new Stash\Driver\FileSystem([
'path' => ExternalCache::getCacheDirectory(),
'dirSplit' => 2
]);
return new Stash\Pool($driver);
} catch (InvalidArgumentException $e) {
// carry on ... but no caching :(
return null;
}
}
# ------------------------------------------------
private static function getMemcachedObject(){
if(!defined('__CA_MEMCACHED_HOST__')) {
define('__CA_MEMCACHED_HOST__', 'localhost');
}
if(!defined('__CA_MEMCACHED_PORT__')) {
define('__CA_MEMCACHED_PORT__', 11211);
}
$driver = new Stash\Driver\Memcache(['servers' => [__CA_MEMCACHED_HOST__, __CA_MEMCACHED_PORT__, 'prefix_key' => ExternalCache::makeCacheName(), 'serializer' => 'json']]);
return new Stash\Pool($driver);
}
# ------------------------------------------------
private static function getRedisObject(){
if(!defined('__CA_REDIS_HOST__')) {
define('__CA_REDIS_HOST__', 'localhost');
}
if(!defined('__CA_REDIS_PORT__')) {
define('__CA_REDIS_PORT__', 6379);
}
if(!defined('__CA_REDIS_DB__')) {
define('__CA_REDIS_DB__', 0);
}
$options = array('servers' => [[__CA_REDIS_HOST__, __CA_REDIS_PORT__]], 'database' => __CA_REDIS_DB__);
#! Uses password if defined, this is the modification of the original code
if(defined('__CA_REDIS_PASSWORD__') && __CA_REDIS_PASSWORD__) {
$options['password'] = __CA_REDIS_PASSWORD__;
}
$driver = new Stash\Driver\Redis($options);
return new Stash\Pool($driver);
}
# ------------------------------------------------
private static function getSqliteObject(){
$driver = new Stash\Driver\Sqlite([
'path' => ExternalCache::getCacheDirectory(),
'nesting' => 0
]);
return new Stash\Pool($driver);
}
# ------------------------------------------------
private static function getApcObject(){
$driver = new Stash\Driver\Apc(['ttl' => __CA_CACHE_TTL__, 'namespace' => ExternalCache::makeCacheName()]);
return new Stash\Pool($driver);
}
# ------------------------------------------------
private static function makeKey($ps_key, $ps_namespace) {
if(!defined('__CA_APP_TYPE__')) { define('__CA_APP_TYPE__', 'PROVIDENCE'); }
return substr(__CA_APP_TYPE__, 0, 4).'/'.$ps_namespace.'/'.$ps_key; // only use the first four chars of app type for compactness
}
# ------------------------------------------------
private static function makeCacheName() {
if(!defined('__CA_APP_TYPE__')) { define('__CA_APP_TYPE__', 'PROVIDENCE'); }
return substr(__CA_APP_TYPE__, 0, 4).'_'.__CA_APP_NAME__; // only use the first four chars of app type for compactness
}
# ------------------------------------------------
}
class ExternalCacheInvalidParameterException extends Exception {}