在每次访问中重置缓存 TTL
Reset Cache TTL in each Access
我想知道是否有任何方法可以从上次访问时更新缓存 TTL?
目前,我有一种方法可以通过 API 调用登录到 adobe connect,并且 API 会话在上次调用后的 4 天内有效。
但是我的缓存驱动程序从添加的那一刻起只在缓存中保留会话 4 天。但我想保留它自上次访问以来的 4 天!
有什么办法可以更新Cache TTL吗?
我确定忘记并重新插入密钥不是最佳做法。
/**
* Login Client Based on information that introduced in environment/config file
*
* @param Client $client
*
* @return void
*/
private function loginClient(Client $client)
{
$config = $this->app["config"]->get("adobeConnect");
$session = Cache::store($config["session-cache"]["driver"])->remember(
$config['session-cache']['key'],
$config['session-cache']['expire'],
function () use ($config, $client) {
$client->login($config["user-name"], $config["password"]);
return $client->getSession();
});
$client->setSession($session);
}
您可以侦听事件 CacheHit、测试键模式,并使用新的 TTL 重置该键的缓存。
为此,您应该创建一个新的侦听器并将其添加到 EventServiceProvider
:
protected $listen = [
'Illuminate\Cache\Events\CacheHit' => [
'App\Listeners\UpdateAdobeCache',
]
];
听众:
class UpdateAdobeCache {
public function handle(CacheHit $event)
{
if ($event->key === 'the_cache_key') { // you could also test for a match with regexp
Cache::store($config["session-cache"]["driver"])->put($event->key, $event->value, $newTTL);
}
}
}
我想知道是否有任何方法可以从上次访问时更新缓存 TTL?
目前,我有一种方法可以通过 API 调用登录到 adobe connect,并且 API 会话在上次调用后的 4 天内有效。 但是我的缓存驱动程序从添加的那一刻起只在缓存中保留会话 4 天。但我想保留它自上次访问以来的 4 天!
有什么办法可以更新Cache TTL吗? 我确定忘记并重新插入密钥不是最佳做法。
/**
* Login Client Based on information that introduced in environment/config file
*
* @param Client $client
*
* @return void
*/
private function loginClient(Client $client)
{
$config = $this->app["config"]->get("adobeConnect");
$session = Cache::store($config["session-cache"]["driver"])->remember(
$config['session-cache']['key'],
$config['session-cache']['expire'],
function () use ($config, $client) {
$client->login($config["user-name"], $config["password"]);
return $client->getSession();
});
$client->setSession($session);
}
您可以侦听事件 CacheHit、测试键模式,并使用新的 TTL 重置该键的缓存。
为此,您应该创建一个新的侦听器并将其添加到 EventServiceProvider
:
protected $listen = [
'Illuminate\Cache\Events\CacheHit' => [
'App\Listeners\UpdateAdobeCache',
]
];
听众:
class UpdateAdobeCache {
public function handle(CacheHit $event)
{
if ($event->key === 'the_cache_key') { // you could also test for a match with regexp
Cache::store($config["session-cache"]["driver"])->put($event->key, $event->value, $newTTL);
}
}
}