-
Notifications
You must be signed in to change notification settings - Fork 7
/
lda-cache.class.php
301 lines (237 loc) · 8.52 KB
/
lda-cache.class.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
<?php
require_once 'lda.inc.php';
class LinkedDataApiCachedResponse
{
var $eTag;
var $generatedTime;
var $lastModified;
var $mimetype;
var $nChunks;
var $body;
var $cacheable;
function serve()
{
header("Content-type: {$this->mimetype}");
header("Last-Modified: {$this->lastModified}");
header("ETag: {$this->eTag}");
header("x-served-from-cache: true");
if ($this->nChunks == 1){
echo $this->body;
}
else{
foreach ($this->body as $split){
echo $split;
}
}
}
}
class LinkedDataApiCache
{
var $connection = false;
var $_options = array();
function __construct($options=array()){
$this->_options = $options;
$this->connection = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
}
public function load($id, $doNotTestCacheValidity = FALSE, $doNotUnserialize = FALSE) {
// return false; // DISABLE
if(!$this->connection) $this->connection = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
if(@$tmp = $this->connection->get($id)){
if (is_array($tmp)) {
logDebug("Returned valid response from cache for id: ".$id);
return $tmp[0];
}
}
return false;
}
function getLifetime($specificLifetime){
if($specificLifetime){
return PUELIA_CACHE_AGE;
}
}
public function save($data, $id, $tags = array(), $specificLifetime = false, $priority=0){
// return false; // DISABLE
$lifetime = $this->getLifetime($specificLifetime);
if(!$this->connection) $this->connection = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
if (isset($this->_options['compression']) AND $this->_options['compression']) {
$flag = MEMCACHE_COMPRESSED;
} else {
$flag = 0;
}
if ($this->test($id)) {
// because set and replace seems to have different behaviour
@$result = $this->connection->replace($id, array($data, time(), $lifetime), $flag, $lifetime);
} else {
@$result = $this->connection->set($id, array($data, time(), $lifetime), $flag, $lifetime);
}
return $result;
}
public function remove($id){
if(!$this->connection) $this->connection = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
// return false; // DISABLE
return $this->connection->delete($id);
}
public function test($id){
if(@$tmp = $this->connection->get($id)){
if (is_array($tmp)) {
return $tmp[1];
}
}
// return false;
}
public static function hasCachedResponse(LinkedDataApiRequest $request)
{
// return false; // DISABLE
if(!function_exists("memcache_connect")) return false;
$acceptableTypes = self::getAcceptableTypes($request);
$uri = $request->getOrderedUriWithoutApiKeys();
foreach ($acceptableTypes as $mimetype)
{
$key = LinkedDataApiCache::cacheKey($uri, $mimetype);
$mc = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
$cachedObject = $mc->get($key);
if ($cachedObject)
{
if ($cachedObject->nChunks > 1){
$cachedObject->body = array();
for ($i=0; $i<$cachedObject->nChunks; $i++){
$splitKey = LinkedDataApiCache::cacheIndexedKey($uri, $mimetype, $i);
$cachedObject->body[$i] = $mc->get($splitKey);
}
logDebug("Found a cached response > 1 MB for $mimetype under key $key ; Aggregating response from ".$cachedObject->nChunks." chunks");
}
else{
logDebug("Found a cached response for $mimetype under key $key");
}
return $cachedObject;
}
logDebug("No cached response for $mimetype under key $key");
}
logDebug('No suitable cached responses found');
return false;
}
private static function getAcceptableTypes($request){
global $outputFormats;
$extension = $request->getFormatExtension();
if ($extension!==false AND $extension!='ico'){
$acceptableTypes = $outputFormats[$extension]['mimetypes'];
}
else {
$formatParam = $request->getParam('_format');
if ($formatParam!=null){
$acceptableTypes = $outputFormats[$formatParam]['mimetypes'];
}
else{
$acceptableTypes = $request->getAcceptTypes();
}
}
return $acceptableTypes;
}
public static function cacheResponse(LinkedDataApiRequest $request, LinkedDataApiResponse $response)
{
// return false; // DISABLE
if(!function_exists("memcache_connect")) return false;
$cacheableResponse = new LinkedDataApiCachedResponse();
$cacheableResponse->eTag = $response->eTag;
$cacheableResponse->generatedTime = $response->generatedTime;
$cacheableResponse->lastModified = $response->lastModified;
$cacheableResponse->mimetype = $response->mimetype;
$key = LinkedDataApiCache::cacheKey($request->getOrderedUriWithoutApiKeys(), $cacheableResponse->mimetype);
$responseLength = strlen($response->body);
if ($responseLength>MAX_RESPONSE){
logDebug('Response larger than '.MAX_RESPONSE.', will not cache');
return;
}
if ($responseLength > MEMCACHED_LIMIT){
$cacheableResponse->nChunks = (int)(($responseLength+MEMCACHED_LIMIT)/MEMCACHED_LIMIT);
$splits = str_split($response->body, MEMCACHED_LIMIT);
$cacheableResponse->body = false;
$mc = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
$mc->add($key, $cacheableResponse, false, PUELIA_CACHE_AGE);
logDebug('Main key: '.$key);
for ($i=0; $i<$cacheableResponse->nChunks; $i++){
$splitKey = LinkedDataApiCache::cacheIndexedKey($request->getOrderedUriWithoutApiKeys(), $cacheableResponse->mimetype, $i);
$mc->add($splitKey, $splits[$i], false, PUELIA_CACHE_AGE);
logDebug('Split key: '.$splitKey);
}
logDebug('Caching Response > 1MB as '.$key.' with mimetype '.$cacheableResponse->mimetype."; writing ".$cacheableResponse->nChunks." chunks");
}
else{
$cacheableResponse->nChunks = 1;
$cacheableResponse->body = $response->body;
$mc = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
$mc->add($key, $cacheableResponse, false, PUELIA_CACHE_AGE);
logDebug('Caching Response as '.$key.' with mimetype '.$cacheableResponse->mimetype);
}
}
public static function cacheURI($uri){
// return false; // DISABLE
if(!function_exists("memcache_connect"))
return false;
logDebug('Caching '.$uri);
$key = LinkedDataApiCache::cacheKey($uri, '');
$mc = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
$mc->add($key, true, false );
return $key;
}
public static function hasCachedUri($uri){
// return false; // DISABLE
// logDebug("Looking in memcache for $uri");
if(!function_exists("memcache_connect"))
return false;
$mc = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
$key = LinkedDataApiCache::cacheKey($uri, '');
$cachedObject = $mc->get($key);
if ($cachedObject)
{
return $cachedObject;
}
logDebug("No cached uri $uri");
return false;
}
public static function cacheConfig($filepath, ConfigGraph $configgraph){
if(!function_exists("memcache_connect")) return false;
// return false; // DISABLE
// logDebug('Caching '.$filepath);
$key = LinkedDataApiCache::configCacheKey($filepath);
$mc = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
$mc->add($key, $configgraph, false );
return $key;
}
/**
* @return cached object from memcache, or false if not found.
*/
public static function hasCachedConfig($filepath){
// return false; // DISABLE
// logDebug("Looking in memcache for $filepath");
if(!function_exists("memcache_connect")) return false;
$key = LinkedDataApiCache::configCacheKey($filepath);
$mc = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
$cachedObject = $mc->get($key);
if ($cachedObject)
{
return $cachedObject;
}
logDebug("No cached version of ConfigGraph from $filepath");
return false;
}
public static function configCacheKey($filepath){
$mtime = filemtime($filepath);
return md5($filepath.$mtime);
}
private static function cacheKey($requestUri, $mimetype)
{
$key = $requestUri;
if (isset($mimetype))
$key .= trim($mimetype);
return md5($key);
}
private static function cacheIndexedKey($requestUri, $mimetype, $index)
{
$key = $requestUri;
if (isset($mimetype))
$key .= trim($mimetype);
$key .= $index;
return md5($key);
}
}