Skip to content

Commit 7fcdb58

Browse files
author
capkokoon
committed
Various tweaks and fixes
Add .gitignore Fix deleteExpired function Use TTL (relative time) rather than absolute timestamp to eval expiration Add simple demo
1 parent 1ef87d2 commit 7fcdb58

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cache/

cache.class.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,16 @@ public function isCached($key)
7979
*
8080
* @param string $key
8181
* @param mixed $data
82-
* @param integer [optional] $expires
82+
* @param mixed [optional] $expires / TTL of the data in seconds
8383
* @return self
8484
*/
8585
public function store($key, $data, $expires = 0)
8686
{
87+
if (is_string($expires)) {
88+
if (intval($expires) > 0) {
89+
$expires = strtotime($expires, 0);
90+
}
91+
}
8792
$new_data = array(
8893
'time' => time(),
8994
'expire' => (int) $expires,
@@ -104,8 +109,7 @@ public function store($key, $data, $expires = 0)
104109
* Retrieve cached data by key
105110
*
106111
* @param string $key
107-
* @param boolean [optional] $timestamp
108-
* @return string
112+
* @return mixed
109113
*/
110114
public function retrieve($key)
111115
{
@@ -171,12 +175,12 @@ public function deleteExpired()
171175
$cache = $this->_loadCache();
172176
if (is_array($cache)) {
173177
$i = 0;
174-
$cache = array_map(function ($value) {
175-
if (!$this->_isExpired($value['time'], $value['expire'])) {
176-
++$i;
177-
return $value;
178-
}
179-
});
178+
foreach ($cache as $key => $value) {
179+
if ($this->_isExpired($value['time'], $value['expire'])) {
180+
unset($cache[$key]);
181+
++$i;
182+
}
183+
}
180184
if ($i > 0) {
181185
$this->_saveCache($cache);
182186
}

demo.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
include 'cache.class.php';
3+
$settings = array('testcache');
4+
$cache = new \Cache();
5+
$cache->store('test', 14345, '0');
6+
$cache->store('test2', 6.34324);
7+
$cache->deleteExpired();
8+
var_dump($cache);
9+
?>

0 commit comments

Comments
 (0)