1
1
<?php
2
2
/*
3
3
* Project : PHP Cache Class
4
+ * Version : 1.1
4
5
* Author : Hardik Choudhary (ThinTake)
5
6
* Author URL : https://thintake.in
7
+ * Class URL : https://github.com/hardik-choudhary/php-caching
6
8
* License : GNU GPLv3
7
9
*/
8
10
@@ -19,10 +21,10 @@ class Cache
19
21
public ?string $ cacheDirectory = NULL ;
20
22
21
23
/**
22
- * Sub folder in cacheDirectory
24
+ * Sub folder in cacheDirectory optional
23
25
* @var string|null
24
26
*/
25
- public ?string $ currentFolder = NULL ;
27
+ public ?string $ subFolder = NULL ;
26
28
27
29
/**
28
30
* Cache constructor.
@@ -36,14 +38,15 @@ public function __construct(string $cacheDirectory)
36
38
}
37
39
38
40
/**
39
- * @param string $currentFolder
41
+ * Set sub folder in cache directory. Like: {cacheDirectory}/{subFolder}, "cache/pages-cache"
42
+ * @param string $subFolder
40
43
*/
41
- public function setCurrentFolder (string $ currentFolder ): void
44
+ public function setSubFolder (string $ subFolder ): void
42
45
{
43
- $ this ->currentFolder = $ currentFolder ;
46
+ $ this ->subFolder = $ subFolder ;
44
47
45
- $ this ->createDirectory ("{ $ this ->cacheDirectory } / { $ this ->currentFolder }" );
46
- // $this->createDefaultFiles("{ $this->cacheDirectory}/{ $this->currentFolder}" );
48
+ $ this ->createDirectory ($ this ->cacheDirectory . DIRECTORY_SEPARATOR . $ this ->subFolder );
49
+ // $this->createDefaultFiles($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->subFolder );
47
50
}
48
51
49
52
/**
@@ -54,17 +57,35 @@ public function setCurrentFolder(string $currentFolder): void
54
57
* @return string|null Return null if file expired or doesn't exist
55
58
*/
56
59
public function read (string $ cacheName , int $ maxAge = 0 , bool $ deleteExpired = TRUE ): ?string
60
+ {
61
+ $ cacheFile = $ this ->getCachePath ($ cacheName );
62
+ if ($ this ->checkCache ($ cacheName , $ maxAge , $ deleteExpired )){
63
+ return file_get_contents ($ cacheFile );
64
+ }
65
+ return NULL ;
66
+ }
67
+
68
+
69
+ /**
70
+ * Check is cache exist or not
71
+ *
72
+ * @param string $cacheName String that was used while creating cache.
73
+ * @param int $maxAge (in Seconds). Return NULL if file older then these seconds. Default: 0, No limit
74
+ * @param bool $deleteExpired Delete cache if file age is more then maxAge. Default: TRUE
75
+ * @return bool
76
+ */
77
+ public function checkCache (string $ cacheName , int $ maxAge = 0 , bool $ deleteExpired = TRUE ) :bool
57
78
{
58
79
$ cacheFile = $ this ->getCachePath ($ cacheName );
59
80
if (file_exists ($ cacheFile )) {
60
81
if ($ maxAge == 0 || (time () - filemtime ($ cacheFile )) <= $ maxAge ){
61
- return file_get_contents ( $ cacheFile ) ;
82
+ return TRUE ;
62
83
}
63
84
elseif ($ deleteExpired ){
64
85
$ this ->delete ($ cacheName );
65
86
}
66
87
}
67
- return NULL ;
88
+ return FALSE ;
68
89
}
69
90
70
91
/**
@@ -80,15 +101,6 @@ public function write(string $cacheName, string $content) :void
80
101
fclose ($ handle );
81
102
}
82
103
83
- /**
84
- * Delete cache file
85
- * @param string $cacheName
86
- */
87
- public function delete (string $ cacheName ) :void
88
- {
89
- @unlink ($ this ->getCachePath ($ cacheName ));
90
- }
91
-
92
104
/**
93
105
* Create directory if doesn't exists
94
106
* @param string $directory
@@ -108,13 +120,13 @@ private function createDirectory(string $directory) :void
108
120
*/
109
121
private function createDefaultFiles (string $ directory ) :void
110
122
{
111
- if (!file_exists ("{ $ directory} /. htaccess " )) {
112
- $ f = @fopen ("{ $ directory} /. htaccess " , "a+ " );
123
+ if (!file_exists ($ directory . DIRECTORY_SEPARATOR . " htaccess " )) {
124
+ $ f = @fopen ($ directory . DIRECTORY_SEPARATOR . " htaccess " , "a+ " );
113
125
@fwrite ($ f , "deny from all " );
114
126
@fclose ($ f );
115
127
}
116
- if (!file_exists ("{ $ directory} / index.html " )) {
117
- $ f = @fopen ("{ $ directory} / index.html " , "a+ " );
128
+ if (!file_exists ($ directory . DIRECTORY_SEPARATOR . " index.html " )) {
129
+ $ f = @fopen ($ directory . DIRECTORY_SEPARATOR . " index.html " , "a+ " );
118
130
@fclose ($ f );
119
131
}
120
132
}
@@ -124,10 +136,78 @@ private function createDefaultFiles(string $directory) :void
124
136
* @param string $cacheName String that was used while creating cache
125
137
* @return string
126
138
*/
127
- private function getCachePath (string $ cacheName ) :string
139
+ public function getCachePath (string $ cacheName ) :string
140
+ {
141
+ return $ this ->getCacheDir () . DIRECTORY_SEPARATOR . hash ('sha1 ' , $ cacheName ) .".cache " ;
142
+ }
143
+
144
+ /**
145
+ * Get current cache directory with selected
146
+ * @return string
147
+ */
148
+ public function getCacheDir (): string
149
+ {
150
+ return ($ this ->subFolder != NULL )? $ this ->cacheDirectory . DIRECTORY_SEPARATOR . $ this ->subFolder : $ this ->cacheDirectory ;
151
+ }
152
+
153
+ /**
154
+ * Delete cache single file
155
+ * @param string $cacheName
156
+ */
157
+ public function delete (string $ cacheName ) :void
158
+ {
159
+ unlink ($ this ->getCachePath ($ cacheName ));
160
+ }
161
+
162
+ /**
163
+ * Clear specific cache.
164
+ * @param int $maxAge (in Seconds). Delete all files older then these seconds. Default: 0, Clear All Files
165
+ */
166
+ public function clear (int $ maxAge = 0 ) :void
167
+ {
168
+ $ cacheDir = $ this ->getCacheDir ();
169
+ foreach (array_diff (scandir ($ cacheDir ), array ('. ' , '.. ' , '.htaccess ' , 'index.html ' )) as $ file ){
170
+ $ cacheFile = $ cacheDir . DIRECTORY_SEPARATOR . $ file ;
171
+ if (is_file ($ cacheFile ) && ($ maxAge == 0 || (time () - filemtime ($ cacheFile )) >= $ maxAge )){
172
+ unlink ($ cacheFile );
173
+ }
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Clear all cache files
179
+ */
180
+ public function clearAll () :bool
128
181
{
129
- $ SubFolder = ($ this ->currentFolder != NULL )? "/ {$ this ->currentFolder }" : '' ;
130
- return "{$ this ->cacheDirectory }{$ SubFolder }/ " . hash ('sha1 ' , $ cacheName ) .".cache " ;
182
+ return $ this ->deleteDirectory ($ this ->cacheDirectory );
131
183
}
132
184
185
+ /**
186
+ * Delete a directory
187
+ * @param $dir
188
+ * @return bool
189
+ */
190
+ private function deleteDirectory ($ dir ):bool
191
+ {
192
+ if (!file_exists ($ dir )) {
193
+ return true ;
194
+ }
195
+
196
+ if (!is_dir ($ dir )) {
197
+ return unlink ($ dir );
198
+ }
199
+
200
+ foreach (scandir ($ dir ) as $ item ) {
201
+ if ($ item == '. ' || $ item == '.. ' ) {
202
+ continue ;
203
+ }
204
+
205
+ if (!$ this ->deleteDirectory ($ dir . DIRECTORY_SEPARATOR . $ item )) {
206
+ return false ;
207
+ }
208
+
209
+ }
210
+
211
+ return rmdir ($ dir );
212
+ }
133
213
}
0 commit comments