Skip to content

Commit 3c6f9e9

Browse files
author
Yitz Willroth
committed
:feat: add touch for cache ttl extension
1 parent 5ce08d4 commit 3c6f9e9

22 files changed

+425
-20
lines changed

src/Illuminate/Cache/ApcStore.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ public function forget($key)
103103
return $this->apc->delete($this->prefix.$key);
104104
}
105105

106+
/**
107+
* Set the expiration time of a cached item.
108+
*/
109+
public function touch(string $key, int $ttl): bool
110+
{
111+
$value = $this->apc->get($key = $this->getPrefix().$key);
112+
113+
if (is_null($value)) {
114+
return false;
115+
}
116+
117+
return $this->apc->put($key, $value, $ttl);
118+
}
119+
106120
/**
107121
* Remove all items from the cache.
108122
*

src/Illuminate/Cache/ArrayStore.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Cache;
44

55
use Illuminate\Contracts\Cache\LockProvider;
6+
use Illuminate\Support\Arr;
67
use Illuminate\Support\Carbon;
78
use Illuminate\Support\InteractsWithTime;
89

@@ -130,6 +131,24 @@ public function forever($key, $value)
130131
return $this->put($key, $value, 0);
131132
}
132133

134+
/**
135+
* Set the expiration time of a cached item.
136+
*/
137+
public function touch(string $key, int $ttl): bool
138+
{
139+
$item = Arr::get($this->storage, $key = $this->getPrefix().$key, null);
140+
141+
if (is_null($item)) {
142+
return false;
143+
}
144+
145+
$item['expiresAt'] = $this->calculateExpiration($ttl);
146+
147+
$this->storage = array_merge($this->storage, [$key => $item]);
148+
149+
return true;
150+
}
151+
133152
/**
134153
* Remove an item from the cache.
135154
*

src/Illuminate/Cache/DatabaseStore.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,17 @@ protected function forgetManyIfExpired(array $keys, bool $prefixed = false)
411411
return true;
412412
}
413413

414+
/**
415+
* Set the expiration time of a cached item.
416+
*/
417+
public function touch(string $key, int $ttl): bool
418+
{
419+
return (bool) $this->table()
420+
->where('key', '=', $this->getPrefix().$key)
421+
->where('expiration', '>', $now = $this->getTime())
422+
->update(['expiration' => $now + $ttl]);
423+
}
424+
414425
/**
415426
* Remove all items from the cache.
416427
*

src/Illuminate/Cache/DynamoDbStore.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,39 @@ public function forget($key)
448448
return true;
449449
}
450450

451+
/**
452+
* Set the expiration time of a cached item.
453+
*
454+
* @throws DynamoDbException
455+
*/
456+
public function touch(string $key, int $ttl): bool
457+
{
458+
try {
459+
$this->dynamo->updateItem([
460+
'TableName' => $this->table,
461+
'Key' => [$this->keyAttribute => ['S' => $this->getPrefix().$key]],
462+
'UpdateExpression' => 'SET #expiry = :expiry',
463+
'ConditionExpression' => 'attribute_exists(#key) AND #expiry > :now',
464+
'ExpressionAttributeNames' => [
465+
'#key' => $this->keyAttribute,
466+
'#expiry' => $this->expirationAttribute,
467+
],
468+
'ExpressionAttributeValues' => [
469+
':expiry' => ['N' => (string) $this->toTimestamp($ttl)],
470+
':now' => ['N' => (string) $this->currentTime()],
471+
],
472+
]);
473+
} catch (DynamoDbException $e) {
474+
if (str_contains($e->getMessage(), 'ConditionalCheckFailed')) {
475+
return false;
476+
}
477+
478+
throw $e;
479+
}
480+
481+
return true;
482+
}
483+
451484
/**
452485
* Remove all items from the cache.
453486
*

src/Illuminate/Cache/FileStore.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,20 @@ public function forget($key)
257257
return false;
258258
}
259259

260+
/**
261+
* Set the expiration time of a cached item.
262+
*/
263+
public function touch(string $key, int $ttl): bool
264+
{
265+
$payload = $this->getPayload($this->getPrefix().$key);
266+
267+
if (is_null($payload['data'])) {
268+
return false;
269+
}
270+
271+
return $this->put($key, $payload['data'], $ttl);
272+
}
273+
260274
/**
261275
* Remove all items from the cache.
262276
*
@@ -298,7 +312,7 @@ protected function getPayload($key)
298312
}
299313

300314
$expire = substr($contents, 0, 10);
301-
} catch (Exception) {
315+
} catch (Exception $e) {
302316
return $this->emptyPayload();
303317
}
304318

@@ -314,6 +328,7 @@ protected function getPayload($key)
314328
try {
315329
$data = unserialize(substr($contents, 10));
316330
} catch (Exception) {
331+
317332
$this->forget($key);
318333

319334
return $this->emptyPayload();

src/Illuminate/Cache/MemcachedStore.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ public function restoreLock($name, $owner)
202202
return $this->lock($name, 0, $owner);
203203
}
204204

205+
/**
206+
* Set the expiration time of a cached item.
207+
*/
208+
public function touch(string $key, int $ttl): bool
209+
{
210+
return $this->memcached->touch($this->getPrefix().$key, $this->calculateExpiration($ttl));
211+
}
212+
205213
/**
206214
* Remove an item from the cache.
207215
*

src/Illuminate/Cache/MemoizedStore.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ public function forget($key)
208208
return $this->repository->forget($key);
209209
}
210210

211+
/**
212+
* Set the expiration time of a cached item.
213+
*/
214+
public function touch(string $key, int $ttl): bool
215+
{
216+
unset($this->cache[$this->prefix($key)]);
217+
218+
return $this->repository->touch($key, $ttl);
219+
}
220+
211221
/**
212222
* Remove all items from the cache.
213223
*

src/Illuminate/Cache/NullStore.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ public function restoreLock($name, $owner)
9393
return $this->lock($name, 0, $owner);
9494
}
9595

96+
/**
97+
* Set the expiration time of a cached item.
98+
*/
99+
public function touch(string $key, int $ttl): bool
100+
{
101+
return false;
102+
}
103+
96104
/**
97105
* Remove an item from the cache.
98106
*

src/Illuminate/Cache/RedisStore.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ public function restoreLock($name, $owner)
248248
return $this->lock($name, 0, $owner);
249249
}
250250

251+
/**
252+
* Set the expiration time of a cached item.
253+
*/
254+
public function touch(string $key, int $ttl): bool
255+
{
256+
return (bool) $this->connection()->expire($this->getPrefix().$key, (int) max(1, $ttl));
257+
}
258+
251259
/**
252260
* Remove an item from the cache.
253261
*
@@ -458,7 +466,6 @@ protected function serialize($value)
458466
* Determine if the given value should be stored as plain value.
459467
*
460468
* @param mixed $value
461-
* @return bool
462469
*/
463470
protected function shouldBeStoredWithoutSerialization($value): bool
464471
{

src/Illuminate/Cache/Repository.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ public function missing($key)
106106
*
107107
* @param array|string $key
108108
* @param mixed $default
109-
* @return mixed
110109
*/
111110
public function get($key, $default = null): mixed
112111
{
@@ -251,8 +250,6 @@ public function put($key, $value, $ttl = null)
251250

252251
/**
253252
* {@inheritdoc}
254-
*
255-
* @return bool
256253
*/
257254
public function set($key, $value, $ttl = null): bool
258255
{
@@ -262,7 +259,6 @@ public function set($key, $value, $ttl = null): bool
262259
/**
263260
* Store multiple items in the cache for a given number of seconds.
264261
*
265-
* @param array $values
266262
* @param \DateTimeInterface|\DateInterval|int|null $ttl
267263
* @return bool
268264
*/
@@ -296,7 +292,6 @@ public function putMany(array $values, $ttl = null)
296292
/**
297293
* Store multiple items in the cache indefinitely.
298294
*
299-
* @param array $values
300295
* @return bool
301296
*/
302297
protected function putManyForever(array $values)
@@ -525,6 +520,22 @@ public function flexible($key, $ttl, $callback, $lock = null)
525520
return $value;
526521
}
527522

523+
/**
524+
* Set the expiration of a cached item; null TTL will retain indefinitely.
525+
*/
526+
public function touch(string $key, \DateTimeInterface|\DateInterval|int|null $ttl = null): bool
527+
{
528+
$value = $this->get($key);
529+
530+
if (is_null($value)) {
531+
return false;
532+
}
533+
534+
return is_null($ttl)
535+
? $this->forever($key, $value)
536+
: $this->store->touch($this->itemKey($key), $this->getSeconds($ttl));
537+
}
538+
528539
/**
529540
* Remove an item from the cache.
530541
*
@@ -546,8 +557,6 @@ public function forget($key)
546557

547558
/**
548559
* {@inheritdoc}
549-
*
550-
* @return bool
551560
*/
552561
public function delete($key): bool
553562
{
@@ -556,8 +565,6 @@ public function delete($key): bool
556565

557566
/**
558567
* {@inheritdoc}
559-
*
560-
* @return bool
561568
*/
562569
public function deleteMultiple($keys): bool
563570
{
@@ -574,8 +581,6 @@ public function deleteMultiple($keys): bool
574581

575582
/**
576583
* {@inheritdoc}
577-
*
578-
* @return bool
579584
*/
580585
public function clear(): bool
581586
{
@@ -737,7 +742,6 @@ public function getEventDispatcher()
737742
/**
738743
* Set the event dispatcher instance.
739744
*
740-
* @param \Illuminate\Contracts\Events\Dispatcher $events
741745
* @return void
742746
*/
743747
public function setEventDispatcher(Dispatcher $events)
@@ -749,7 +753,6 @@ public function setEventDispatcher(Dispatcher $events)
749753
* Determine if a cached value exists.
750754
*
751755
* @param string $key
752-
* @return bool
753756
*/
754757
public function offsetExists($key): bool
755758
{
@@ -760,7 +763,6 @@ public function offsetExists($key): bool
760763
* Retrieve an item from the cache by key.
761764
*
762765
* @param string $key
763-
* @return mixed
764766
*/
765767
public function offsetGet($key): mixed
766768
{
@@ -772,7 +774,6 @@ public function offsetGet($key): mixed
772774
*
773775
* @param string $key
774776
* @param mixed $value
775-
* @return void
776777
*/
777778
public function offsetSet($key, $value): void
778779
{
@@ -783,7 +784,6 @@ public function offsetSet($key, $value): void
783784
* Remove an item from the cache.
784785
*
785786
* @param string $key
786-
* @return void
787787
*/
788788
public function offsetUnset($key): void
789789
{

0 commit comments

Comments
 (0)