-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaching.php
More file actions
173 lines (156 loc) · 4.64 KB
/
caching.php
File metadata and controls
173 lines (156 loc) · 4.64 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
<?php
//Definitions. Do not put trailing / for dir.
define('CACHEDIR', 'cache');
/**
* Class to cache API calls in the relevant format for a specified time.
* Author: Aaron Parker
* URL: http://www.parkeyparker.co.uk/
**/
class Caching {
var $APIURL = "";
var $APIFunc = "";
var $returnType = "";
var $cacheTime = 0;
var $cacheFile = "";
function APICallInit($APIFunc, $parameters, $returnType, $cacheTime) {
if (strlen($APIFunc) < 1 or $cacheTime < 0) {
//Required data is missing!
echo "You have omitted some important data from the caching function, please rectify and try again";
return false;
} else {
//All required data is present
$this->APIFunc = $APIFunc;
$this->cacheTime = $cacheTime;
//Remove any periods that precede the return type
$returnType = preg_replace("/^\./", "", $returnType);
//Check that the return type is relevant to the function requested
if ($APIFunc == "livestats_widget" or $APIFunc == "trends_widget") {
if ($returnType != "jpg" and $returnType != "png") {
//Need to be images,"\n" default to png
$returnType = "png";
}
} else {
if ($returnType != "json" and $returnType != "jsonp" and $returnType != "serialized" and $returnType != "xml") {
//Invalid return type, set default
$returnType = "serialized";
}
}
$this->returnType = $returnType;
$strParameters = "";
foreach ($parameters as $param => $value) {
$strParameters .= $param . '=' . $value . '&';
}
$strParameters = preg_replace("/&$/", "", $strParameters);
//Set the cache file
//Hash the parameters and append to the filename of the cache file.
$paramHash = substr(md5($strParameters), 0, 20);
echo $paramHash . ' ';
$cacheFile = CACHEDIR . '/' . $APIFunc . '-' . $paramHash . '.' . $returnType;
$this->cacheFile = $cacheFile;
//Create the URL
$URL = "http";
$URL .= (isset($_SERVER['HTTPS']) ? "s" : "");
$URL .= "://api.gosquared.com/" . $APIFunc . "." . $returnType . "?" . $strParameters;
$this->APIURL = $URL;
echo $URL;
}
}
function checkCacheValid() {
//Get the times
if (file_exists($this->cacheFile)) {
echo 'file exists';
$fileModTime = filemtime($this->cacheFile);
if ($fileModTime) {
echo $fileModTime;
$cacheRenewalTime = $fileModTime + $this->cacheTime;
if ((time() - $cacheRenewalTime) > 0) {
//cache is still valid
echo 'Cache Valid';
return true;
}
}
}
return false;
}
function updateCache() {
switch ($this->returnType) {
case 'jpg':
case 'png':
// Save the image in the cache location
$curl = curl_init();
$timeout = 0;
curl_setopt ($curl, CURLOPT_URL, $this->APIURL);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
// Getting binary data
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
//Get image via cURL
$image = curl_exec($curl);
curl_close($curl);
if ($image) {
//Save image to cache file
$cache = fopen($this->cacheFile, 'w');
fwrite($cache, $image);
fclose($cache);
return $this->cacheFile;
}
break;
case 'json':
case 'jsonp':
case 'xml':
case 'serialized':
$data = file_get_contents($this->APIURL);
if ($data) {
$cache = fopen($this->cacheFile, 'w');
//Perhaps check if the first part is "Fatal Error"??
fwrite($cache, $data);
fclose($cache);
return $data;
}
break;
}
return false;
}
function getCachedData() {
switch ($this->returnType) {
case 'png':
case 'jpg':
//Return the file path so that it can be used in an img tag
return $this->cacheFile;
break;
case 'json':
case 'jsonp':
case 'xml':
case 'serialized':
//Return the cached data direct to the calling variable.
//The returned data must then be unserialized / dealt with.
$data = file_get_contents($this->cacheFile);
return $data;
break;
}
}
function getAPIResults() {
if ($this->APIFunc == 'livestats') {
return file_get_contents($this->APIURL);
} else {
//Check the cache is still valid
$valid = $this->checkCacheValid();
if ($valid) {
//Cache is valid so return the data held in it
return $this->getCachedData();
} else {
//Cache is not valid and so needs updating
$cacheUpdated = $this->updateCache();
if ($cacheUpdated) {
//The cache is up to date and $cacheUpdated contains data to return
return $cacheUpdated;
} else {
//Cache was not updated, use the old cache data until the cache can be updated.
return $this->getCachedData();
}
}
}
}
}
?>