Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 833bacf

Browse files
Added More methods
1 parent 590abb4 commit 833bacf

File tree

1 file changed

+105
-25
lines changed

1 file changed

+105
-25
lines changed

cache.class.php

Lines changed: 105 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22
/*
33
* Project : PHP Cache Class
4+
* Version : 1.1
45
* Author : Hardik Choudhary (ThinTake)
56
* Author URL : https://thintake.in
7+
* Class URL : https://github.com/hardik-choudhary/php-caching
68
* License : GNU GPLv3
79
*/
810

@@ -19,10 +21,10 @@ class Cache
1921
public ?string $cacheDirectory = NULL;
2022

2123
/**
22-
* Sub folder in cacheDirectory
24+
* Sub folder in cacheDirectory optional
2325
* @var string|null
2426
*/
25-
public ?string $currentFolder = NULL;
27+
public ?string $subFolder = NULL;
2628

2729
/**
2830
* Cache constructor.
@@ -36,14 +38,15 @@ public function __construct(string $cacheDirectory)
3638
}
3739

3840
/**
39-
* @param string $currentFolder
41+
* Set sub folder in cache directory. Like: {cacheDirectory}/{subFolder}, "cache/pages-cache"
42+
* @param string $subFolder
4043
*/
41-
public function setCurrentFolder(string $currentFolder): void
44+
public function setSubFolder(string $subFolder): void
4245
{
43-
$this->currentFolder = $currentFolder;
46+
$this->subFolder = $subFolder;
4447

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);
4750
}
4851

4952
/**
@@ -54,17 +57,35 @@ public function setCurrentFolder(string $currentFolder): void
5457
* @return string|null Return null if file expired or doesn't exist
5558
*/
5659
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
5778
{
5879
$cacheFile = $this->getCachePath($cacheName);
5980
if (file_exists($cacheFile)) {
6081
if($maxAge == 0 || (time() - filemtime($cacheFile)) <= $maxAge){
61-
return file_get_contents($cacheFile);
82+
return TRUE;
6283
}
6384
elseif($deleteExpired){
6485
$this->delete($cacheName);
6586
}
6687
}
67-
return NULL;
88+
return FALSE;
6889
}
6990

7091
/**
@@ -80,15 +101,6 @@ public function write(string $cacheName, string $content) :void
80101
fclose($handle);
81102
}
82103

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-
92104
/**
93105
* Create directory if doesn't exists
94106
* @param string $directory
@@ -108,13 +120,13 @@ private function createDirectory(string $directory) :void
108120
*/
109121
private function createDefaultFiles(string $directory) :void
110122
{
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+");
113125
@fwrite($f, "deny from all");
114126
@fclose($f);
115127
}
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+");
118130
@fclose($f);
119131
}
120132
}
@@ -124,10 +136,78 @@ private function createDefaultFiles(string $directory) :void
124136
* @param string $cacheName String that was used while creating cache
125137
* @return string
126138
*/
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
128181
{
129-
$SubFolder = ($this->currentFolder != NULL)? "/{$this->currentFolder}": '';
130-
return "{$this->cacheDirectory}{$SubFolder}/". hash('sha1', $cacheName) .".cache";
182+
return $this->deleteDirectory($this->cacheDirectory);
131183
}
132184

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+
}
133213
}

0 commit comments

Comments
 (0)