-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.class-old.php
224 lines (204 loc) · 6.38 KB
/
cache.class-old.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
<?php
/*
* Project : PHP Cache Class
* Version : 1.1
* Author : Hardik Choudhary (ThinTake)
* Author URL : https://thintake.in
* Class URL : https://github.com/hardik-choudhary/php-caching
* License : GNU GPLv3
*
* This class works on PHP 5.1.6 or higher (Minimum PHP version 5.1.6 is required)
*/
/**
* Class Cache
*/
class Cache
{
/**
* Directory where cache files will be stored
* @var string|null
*/
public $cacheDirectory = NULL;
/**
* Sub folder in cacheDirectory optional
* @var string|null
*/
public $subFolder = NULL;
/**
* Cache constructor.
* @param string $cacheDirectory
*/
public function __construct(string $cacheDirectory)
{
$this->cacheDirectory = $cacheDirectory;
$this->createDirectory($this->cacheDirectory);
$this->createDefaultFiles($this->cacheDirectory);
}
/**
* Set sub folder in cache directory. Like: {cacheDirectory}/{subFolder}, "cache/pages-cache"
* @param string $subFolder
*/
public function setSubFolder(string $subFolder)
{
$this->subFolder = $subFolder;
$this->createDirectory($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->subFolder);
// $this->createDefaultFiles($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->subFolder);
}
/**
* Get cache file
* @param string $cacheName String that was used while creating cache
* @param int $maxAge (in Seconds). Return NULL if file older then these seconds. Default: 0, No limit
* @param bool $deleteExpired Delete cache if file age is more then maxAge. Default: TRUE
* @return string|null Return null if file expired or doesn't exist
*/
public function read($cacheName, $maxAge = 0, $deleteExpired = TRUE)
{
$cacheFile = $this->getCachePath($cacheName);
if($this->checkCache($cacheName, $maxAge, $deleteExpired)){
return file_get_contents($cacheFile);
}
return NULL;
}
/**
* Check is cache exist or not
*
* @param string $cacheName String that was used while creating cache.
* @param int $maxAge (in Seconds). Return NULL if file older then these seconds. Default: 0, No limit
* @param bool $deleteExpired Delete cache if file age is more then maxAge. Default: TRUE
* @return bool
*/
public function checkCache($cacheName, $maxAge = 0, $deleteExpired = TRUE)
{
$cacheFile = $this->getCachePath($cacheName);
if (file_exists($cacheFile)) {
if($maxAge == 0 || (time() - filemtime($cacheFile)) <= $maxAge){
return TRUE;
}
elseif($deleteExpired){
$this->delete($cacheName);
}
}
return FALSE;
}
/**
* Create new cache file
* @param string $cacheName Any string that will be used to access the cache in future
* @param string $content Content
*/
public function write($cacheName, $content)
{
$cacheFile = $this->getCachePath($cacheName);
file_put_contents($cacheFile, $content);
}
/**
* Create directory if doesn't exists
* @param string $directory
*/
private function createDirectory($directory)
{
if (!file_exists($directory)) {
$oldmask = umask(0);
@mkdir($directory, 0777, true);
@umask($oldmask);
}
}
/**
* Create .htaccess and index.html file. (To deny direct access to cache files)
* @param string $directory
*/
private function createDefaultFiles($directory)
{
if (!file_exists($directory . DIRECTORY_SEPARATOR . ".htaccess")) {
$f = @fopen($directory . DIRECTORY_SEPARATOR . ".htaccess", "a+");
@fwrite($f, "deny from all");
@fclose($f);
}
if (!file_exists($directory . DIRECTORY_SEPARATOR . "index.html")) {
$f = @fopen($directory . DIRECTORY_SEPARATOR . "index.html", "a+");
@fclose($f);
}
}
/**
* Get full path of cache file
* @param string $cacheName String that was used while creating cache
* @return string
*/
public function getCachePath($cacheName)
{
return $this->getCacheDir() . DIRECTORY_SEPARATOR . hash('sha1', $cacheName) .".cache";
}
/**
* Get current cache directory with selected
* @return string
*/
public function getCacheDir()
{
return ($this->subFolder != NULL)? $this->cacheDirectory . DIRECTORY_SEPARATOR . $this->subFolder: $this->cacheDirectory;
}
/**
* Delete cache single file
* @param string $cacheName
*/
public function delete($cacheName)
{
@unlink($this->getCachePath($cacheName));
}
/**
* Clear specific cache.
* @param int $maxAge (in Seconds). Delete all files older then these seconds. Default: 0, Clear All Files
*/
public function clear($maxAge = 0)
{
$cacheDir = $this->getCacheDir();
foreach (array_diff(scandir($cacheDir), array('.', '..', '.htaccess', 'index.html')) as $file){
$cacheFile = $cacheDir . DIRECTORY_SEPARATOR . $file;
if(is_file($cacheFile) && ($maxAge == 0 || (time() - filemtime($cacheFile)) >= $maxAge)){
unlink($cacheFile);
}
}
}
/**
* Clear all cache files
*/
public function clearAll()
{
return $this->deleteDirectory($this->cacheDirectory);
}
/**
* Delete a directory
* @param $dir
* @return bool
*/
private function deleteDirectory($dir)
{
if (!file_exists($dir)) {
return true;
}
if (!is_dir($dir)) {
return unlink($dir);
}
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (!$this->deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}
return rmdir($dir);
}
/**
* Gets cache modification time
*
* @param string $cacheName String that was used while creating cache.
* @return int|null
*/
public function getTime($cacheName)
{
$cacheFile = $this->getCachePath($cacheName);
if (file_exists($cacheFile)) {
return filemtime($cacheFile);
}
return FALSE;
}
}