Skip to content

Commit a39b0d7

Browse files
author
Yitz Willroth
committed
:feat: add touch for cache ttl extension
1 parent cf9cede commit a39b0d7

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)
@@ -526,6 +521,22 @@ public function flexible($key, $ttl, $callback, $lock = null, $alwaysDefer = fal
526521
return $value;
527522
}
528523

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

548559
/**
549560
* {@inheritdoc}
550-
*
551-
* @return bool
552561
*/
553562
public function delete($key): bool
554563
{
@@ -557,8 +566,6 @@ public function delete($key): bool
557566

558567
/**
559568
* {@inheritdoc}
560-
*
561-
* @return bool
562569
*/
563570
public function deleteMultiple($keys): bool
564571
{
@@ -575,8 +582,6 @@ public function deleteMultiple($keys): bool
575582

576583
/**
577584
* {@inheritdoc}
578-
*
579-
* @return bool
580585
*/
581586
public function clear(): bool
582587
{
@@ -738,7 +743,6 @@ public function getEventDispatcher()
738743
/**
739744
* Set the event dispatcher instance.
740745
*
741-
* @param \Illuminate\Contracts\Events\Dispatcher $events
742746
* @return void
743747
*/
744748
public function setEventDispatcher(Dispatcher $events)
@@ -750,7 +754,6 @@ public function setEventDispatcher(Dispatcher $events)
750754
* Determine if a cached value exists.
751755
*
752756
* @param string $key
753-
* @return bool
754757
*/
755758
public function offsetExists($key): bool
756759
{
@@ -761,7 +764,6 @@ public function offsetExists($key): bool
761764
* Retrieve an item from the cache by key.
762765
*
763766
* @param string $key
764-
* @return mixed
765767
*/
766768
public function offsetGet($key): mixed
767769
{
@@ -773,7 +775,6 @@ public function offsetGet($key): mixed
773775
*
774776
* @param string $key
775777
* @param mixed $value
776-
* @return void
777778
*/
778779
public function offsetSet($key, $value): void
779780
{
@@ -784,7 +785,6 @@ public function offsetSet($key, $value): void
784785
* Remove an item from the cache.
785786
*
786787
* @param string $key
787-
* @return void
788788
*/
789789
public function offsetUnset($key): void
790790
{

0 commit comments

Comments
 (0)