Skip to content

Commit bf56bec

Browse files
authored
Merge branch 'laravel:master' into master
2 parents d6b8830 + cc3e3a9 commit bf56bec

24 files changed

+464
-0
lines changed

src/Illuminate/Cache/ApcStore.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ public function forever($key, $value)
9292
return $this->put($key, $value, 0);
9393
}
9494

95+
/**
96+
* Adjust the expiration time of a cached item.
97+
*
98+
* @param string $key
99+
* @param int $seconds
100+
* @return bool
101+
*/
102+
public function touch($key, $seconds)
103+
{
104+
$value = $this->apc->get($key = $this->getPrefix().$key);
105+
106+
if (is_null($value)) {
107+
return false;
108+
}
109+
110+
return $this->apc->put($key, $value, $seconds);
111+
}
112+
95113
/**
96114
* Remove an item from the cache.
97115
*

src/Illuminate/Cache/ArrayStore.php

Lines changed: 23 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,28 @@ public function forever($key, $value)
130131
return $this->put($key, $value, 0);
131132
}
132133

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

src/Illuminate/Cache/DatabaseStore.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,21 @@ public function restoreLock($name, $owner)
352352
return $this->lock($name, 0, $owner);
353353
}
354354

355+
/**
356+
* Adjust the expiration time of a cached item.
357+
*
358+
* @param string $key
359+
* @param int $seconds
360+
* @return bool
361+
*/
362+
public function touch($key, $seconds)
363+
{
364+
return (bool) $this->table()
365+
->where('key', '=', $this->getPrefix().$key)
366+
->where('expiration', '>', $now = $this->getTime())
367+
->update(['expiration' => $now + $seconds]);
368+
}
369+
355370
/**
356371
* Remove an item from the cache.
357372
*

src/Illuminate/Cache/DynamoDbStore.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,43 @@ public function restoreLock($name, $owner)
428428
return $this->lock($name, 0, $owner);
429429
}
430430

431+
/**
432+
* Adjust the expiration time of a cached item.
433+
*
434+
* @param string $key
435+
* @param int $seconds
436+
* @return bool
437+
*
438+
* @throws DynamoDbException
439+
*/
440+
public function touch($key, $seconds)
441+
{
442+
try {
443+
$this->dynamo->updateItem([
444+
'TableName' => $this->table,
445+
'Key' => [$this->keyAttribute => ['S' => $this->getPrefix().$key]],
446+
'UpdateExpression' => 'SET #expiry = :expiry',
447+
'ConditionExpression' => 'attribute_exists(#key) AND #expiry > :now',
448+
'ExpressionAttributeNames' => [
449+
'#key' => $this->keyAttribute,
450+
'#expiry' => $this->expirationAttribute,
451+
],
452+
'ExpressionAttributeValues' => [
453+
':expiry' => ['N' => (string) $this->toTimestamp($seconds)],
454+
':now' => ['N' => (string) $this->currentTime()],
455+
],
456+
]);
457+
} catch (DynamoDbException $e) {
458+
if (str_contains($e->getMessage(), 'ConditionalCheckFailed')) {
459+
return false;
460+
}
461+
462+
throw $e;
463+
}
464+
465+
return true;
466+
}
467+
431468
/**
432469
* Remove an item from the cache.
433470
*

src/Illuminate/Cache/FileStore.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,24 @@ public function restoreLock($name, $owner)
238238
return $this->lock($name, 0, $owner);
239239
}
240240

241+
/**
242+
* Adjust the expiration time of a cached item.
243+
*
244+
* @param string $key
245+
* @param int $seconds
246+
* @return bool
247+
*/
248+
public function touch($key, $seconds)
249+
{
250+
$payload = $this->getPayload($this->getPrefix().$key);
251+
252+
if (is_null($payload['data'])) {
253+
return false;
254+
}
255+
256+
return $this->put($key, $payload['data'], $seconds);
257+
}
258+
241259
/**
242260
* Remove an item from the cache.
243261
*

src/Illuminate/Cache/MemcachedStore.php

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

205+
/**
206+
* Adjust the expiration time of a cached item.
207+
*
208+
* @param string $key
209+
* @param int $seconds
210+
* @return bool
211+
*/
212+
public function touch($key, $seconds)
213+
{
214+
return $this->memcached->touch($this->getPrefix().$key, $this->calculateExpiration($seconds));
215+
}
216+
205217
/**
206218
* Remove an item from the cache.
207219
*

src/Illuminate/Cache/MemoizedStore.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ public function restoreLock($name, $owner)
196196
return $this->repository->resoreLock(...func_get_args());
197197
}
198198

199+
/**
200+
* Adjust the expiration time of a cached item.
201+
*
202+
* @param string $key
203+
* @param int $seconds
204+
* @return bool
205+
*/
206+
public function touch($key, $seconds)
207+
{
208+
unset($this->cache[$this->prefix($key)]);
209+
210+
return $this->repository->touch($key, $seconds);
211+
}
212+
199213
/**
200214
* Remove an item from the cache.
201215
*

src/Illuminate/Cache/NullStore.php

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

96+
/**
97+
* Adjust the expiration time of a cached item.
98+
*
99+
* @param string $key
100+
* @param int $seconds
101+
* @return bool
102+
*/
103+
public function touch($key, $seconds)
104+
{
105+
return false;
106+
}
107+
96108
/**
97109
* Remove an item from the cache.
98110
*

src/Illuminate/Cache/RedisStore.php

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

251+
/**
252+
* Adjust the expiration time of a cached item.
253+
*
254+
* @param string $key
255+
* @param int $seconds
256+
* @return bool
257+
*/
258+
public function touch($key, $seconds)
259+
{
260+
return (bool) $this->connection()->expire($this->getPrefix().$key, (int) max(1, $seconds));
261+
}
262+
251263
/**
252264
* Remove an item from the cache.
253265
*

src/Illuminate/Cache/Repository.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,26 @@ public function flexible($key, $ttl, $callback, $lock = null, $alwaysDefer = fal
526526
return $value;
527527
}
528528

529+
/**
530+
* Set the expiration of a cached item; null TTL will retain the item forever.
531+
*
532+
* @param string $key
533+
* @param \DateTimeInterface|\DateInterval|int|null $ttl
534+
* @return bool
535+
*/
536+
public function touch($key, $ttl = null)
537+
{
538+
$value = $this->get($key);
539+
540+
if (is_null($value)) {
541+
return false;
542+
}
543+
544+
return is_null($ttl)
545+
? $this->forever($key, $value)
546+
: $this->store->touch($this->itemKey($key), $this->getSeconds($ttl));
547+
}
548+
529549
/**
530550
* Remove an item from the cache.
531551
*

0 commit comments

Comments
 (0)