forked from openphacts/OPS_LinkedDataApi
/
lda-cache.class.php
218 lines (177 loc) · 6.35 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
<?php
require_once 'lda.inc.php';
class LinkedDataApiCachedResponse
{
var $eTag;
var $generatedTime;
var $lastModified;
var $mimetype;
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");
echo $this->body;
}
}
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) {
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){
$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 $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)
{
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)
{
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)
{
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;
$cacheableResponse->body = $response->body;
$key = LinkedDataApiCache::cacheKey($request->getOrderedUriWithoutApiKeys(), $cacheableResponse->mimetype);
logDebug('Caching Response as '.$key.' with mimetype '.$cacheableResponse->mimetype);
$mc = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
$mc->add($key, $cacheableResponse, false, PUELIA_CACHE_AGE);
}
public static function cacheURI($uri){
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){
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;
logDebug('Caching '.$filepath);
$key = LinkedDataApiCache::configCacheKey($filepath);
$mc = memcache_connect(PUELIA_MEMCACHE_HOST, PUELIA_MEMCACHE_PORT);
$mc->add($key, $configgraph, false );
return $key;
}
public static function hasCachedConfig($filepath){
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);
}
}