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

Commit b23c873

Browse files
Add getTime() Method
Add getTime() Method and replaced fwrite() with file_put_contents() to write cache files
1 parent 7b5f99a commit b23c873

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

cache.class-old.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ public function checkCache($cacheName, $maxAge = 0, $deleteExpired = TRUE)
9898
public function write($cacheName, $content)
9999
{
100100
$cacheFile = $this->getCachePath($cacheName);
101-
$handle = fopen($cacheFile, 'a');
102-
fwrite($handle, $content);
103-
fclose($handle);
101+
file_put_contents($cacheFile, $content);
104102
}
105103

106104
/**
@@ -194,7 +192,6 @@ private function deleteDirectory($dir)
194192
if (!file_exists($dir)) {
195193
return true;
196194
}
197-
198195
if (!is_dir($dir)) {
199196
return unlink($dir);
200197
}
@@ -203,13 +200,25 @@ private function deleteDirectory($dir)
203200
if ($item == '.' || $item == '..') {
204201
continue;
205202
}
206-
207203
if (!$this->deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
208204
return false;
209205
}
210-
211206
}
212-
213207
return rmdir($dir);
214208
}
209+
210+
/**
211+
* Gets cache modification time
212+
*
213+
* @param string $cacheName String that was used while creating cache.
214+
* @return int|null
215+
*/
216+
public function getTime($cacheName)
217+
{
218+
$cacheFile = $this->getCachePath($cacheName);
219+
if (file_exists($cacheFile)) {
220+
return filemtime($cacheFile);
221+
}
222+
return FALSE;
223+
}
215224
}

cache.class.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ public function checkCache(string $cacheName, int $maxAge = 0, bool $deleteExpir
9898
public function write(string $cacheName, string $content) :void
9999
{
100100
$cacheFile = $this->getCachePath($cacheName);
101-
$handle = fopen($cacheFile, 'a');
102-
fwrite($handle, $content);
103-
fclose($handle);
101+
file_put_contents($cacheFile, $content);
104102
}
105103

106104
/**
@@ -194,7 +192,6 @@ private function deleteDirectory($dir):bool
194192
if (!file_exists($dir)) {
195193
return true;
196194
}
197-
198195
if (!is_dir($dir)) {
199196
return unlink($dir);
200197
}
@@ -203,13 +200,25 @@ private function deleteDirectory($dir):bool
203200
if ($item == '.' || $item == '..') {
204201
continue;
205202
}
206-
207203
if (!$this->deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
208204
return false;
209205
}
210-
211206
}
212-
213207
return rmdir($dir);
214208
}
209+
210+
/**
211+
* Gets cache modification time
212+
*
213+
* @param string $cacheName String that was used while creating cache.
214+
* @return int|null
215+
*/
216+
public function getTime(string $cacheName) :?int
217+
{
218+
$cacheFile = $this->getCachePath($cacheName);
219+
if (file_exists($cacheFile)) {
220+
return filemtime($cacheFile);
221+
}
222+
return FALSE;
223+
}
215224
}

0 commit comments

Comments
 (0)