Skip to content

Commit e36282a

Browse files
committed
🔨 improve cache with time to live options
Signed-off-by: otengkwame <[email protected]>
1 parent c0a3e84 commit e36282a

File tree

2 files changed

+63
-23
lines changed

2 files changed

+63
-23
lines changed

Core/core/Cache/Cache.php

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,54 @@ public function setCachePath($path = '')
8282
* @param string $key containing the identifier of the cached item
8383
* @return bool whether the item is currently cached or not
8484
*/
85-
public function isCached($key)
85+
public function isCached($key, $ttl = null)
8686
{
8787
$key = sha1($key);
8888

8989
$this->setCachePath(); // Set the correct cache path
9090

91+
// Use the default expiration time if $ttl is not provided
92+
$ttl = $ttl ?? $this->expireAfter;
93+
9194
$cachePath = $this->filesCachePath();
92-
93-
// checks if the cached item exists and that it has not expired.
94-
$fileExpires = file_exists($cachePath .$key. $this->cacheExtension)
95-
? (filectime($cachePath .$key. $this->cacheExtension) + $this->expireAfter)
96-
: (time() - 10);
9795

98-
return ($fileExpires >= time()) ? true : false;
96+
$cachedFile = $cachePath . $key . $this->cacheExtension;
97+
98+
if (!file_exists($cachedFile)) {
99+
return false;
100+
}
101+
102+
// Get the modification time of the cached file
103+
// and add the expiration time
104+
$expirationTime = filemtime($cachedFile) + $ttl;
105+
106+
// Check if the item has expired or not
107+
if ($expirationTime < time()) {
108+
return false;
109+
}
110+
111+
return true;
99112
}
100113

101-
public function setCacheItem($key, $value)
114+
/**
115+
* Set Cache Item with Time to live
116+
*
117+
* @return void
118+
*/
119+
public function setCacheItem($key, $value, $ttl = null)
102120
{
103121
$this->setCachePath(); // Set the correct cache path
104122

105-
$cachePath = $this->filesCachePath();
123+
// Use the default expiration time if $ttl is not provided
124+
$ttl = $ttl ?? $this->expireAfter;
106125

107-
// hashing the key in order to ensure that the item
126+
// Hash the key in order to ensure that the item
108127
// is stored with an appropriate file name in the file system.
109128
$key = sha1($key);
110129

111-
// serializes or compress the contents so that they can
130+
$cachedFile = $this->filesCachePath() . $key . $this->cacheExtension;
131+
132+
// Serialize or compress the contents so that they can
112133
// be stored in plain text
113134
if ($this->serializeWith === self::JSON) {
114135
$value = json_encode($value);
@@ -124,11 +145,12 @@ public function setCacheItem($key, $value)
124145
if ($this->serializeWith === self::SERIALIZE) {
125146
$value = serialize($value);
126147
}
127-
128-
try {
129-
file_put_contents($cachePath . $key . $this->cacheExtension, $value);
148+
149+
try {
150+
file_put_contents($cachedFile, $value, LOCK_EX);
151+
touch($cachedFile, time() + $ttl);
130152
} catch(\Exception $e) {
131-
log_message('error', $e->message);
153+
log_message('error', $e->getMessage());
132154
}
133155

134156
}
@@ -143,17 +165,17 @@ public function getCacheItem($key)
143165
{
144166
$this->setCachePath(); // Set the correct cache path
145167

146-
$cachePath = $this->filesCachePath();
168+
$key = sha1($key); // hash the key
147169

148-
$key = sha1($key);
170+
$cachedFile = $this->filesCachePath() . $key . $this->cacheExtension;
149171

150-
$exists = file_exists($cachePath . $key . $this->cacheExtension);
172+
$exists = file_exists($cachedFile);
151173

152174
if (!$exists) {
153175
return false;
154176
}
155177

156-
$items = file_get_contents($cachePath .$key. $this->cacheExtension);
178+
$items = file_get_contents($cachedFile);
157179

158180
// unserializes or uncompress the contents so that they can
159181
// be read in array or object
@@ -173,10 +195,28 @@ public function getCacheItem($key)
173195
$items = unserialize($items);
174196
}
175197

176-
177198
return $items;
178199
}
179200

201+
/**
202+
* Return time remaining until cached file expires
203+
*
204+
* @return mixed
205+
*/
206+
public function getTTL($key, $ttl = null)
207+
{
208+
$this->setCachePath(); // Set the correct cache path
209+
210+
// Use the default expiration time if $ttl is not provided
211+
$ttl = $ttl ?? $this->expireAfter;
212+
213+
$cachedFile = $this->filesCachePath() . $key . $this->cacheExtension;
214+
215+
$expirationTime = filemtime($cachedFile) + $ttl;
216+
$timeRemaining = $expirationTime - time();
217+
return $timeRemaining > 0 ? $timeRemaining : 0;
218+
}
219+
180220
/**
181221
* Delete's the cached item
182222
*

Core/core/Helpers/Arrayz.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function set($data, $ttl = 1800): Arrayz
199199
}
200200

201201
$this->cache->serializeWith = $this->cacheAs;
202-
$this->cache->setCacheItem($this->cacheItem, $data);
202+
$this->cache->setCacheItem($this->cacheItem, $data, $ttl);
203203

204204
return $this;
205205
}
@@ -223,11 +223,11 @@ public function delete(): Arrayz
223223
* @param string $item
224224
* @return bool
225225
*/
226-
public function available($item = ''): bool
226+
public function available($item = '', $ttl = null): bool
227227
{
228228
$this->cache->setCachePath($this->cachePath);
229229

230-
return $this->cache->isCached($item);
230+
return $this->cache->isCached($item, $ttl);
231231
}
232232

233233
/**

0 commit comments

Comments
 (0)