在 Laravel 6 中,当设置过期时间后,标签标识符键(‘tag:’.$name.’:key’)与标准引用键(:standard_ref)未自动过期的解决
1、现在存储被打上标签的缓存数据,打上了 1 个标签:theme_editor_qVnbtdW6HjsG5clEw6j6ygedgjrP4A9EDIsY
return Cache::tags([ ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId] ) ->put(ThemePreviewInterface::THEME_EDITOR_SESSION_PREFIX . $sessionId, json_encode($data), ThemePreviewInterface::TTL);
2、运行程序以后,总计生成了 3 个 key,如图1
object_local_database_object_local_cache:tag:theme_editor_qVnbtdW6HjsG5clEw6j6ygedgjrP4A9EDIsY:key object_local_database_object_local_cache:63316a53e72ae936365540:standard_ref object_local_database_object_local_cache:8900c8427a4e5d7ac70fed24623811c3b5330ddf:theme_editor_session:qVnbtdW6HjsG5clEw6j6ygedgjrP4A9EDIsY
3、过期时间设置为 120 秒,当 2 分钟之后,再次刷新 Redis,只有 key object_local_database_object_local_cache:8900c8427a4e5d7ac70fed24623811c3b5330ddf:theme_editor_session:qVnbtdW6HjsG5clEw6j6ygedgjrP4A9EDIsY 已经被自动删除掉。如图2
4、相当于还剩下 2 个冗余的 key,皆是与 标签 相关的。决定在写入缓存时,将 2 个冗余的 key 皆设置过期时间。由于是使用的 Laravel 的 Redis 缓存。决定基于 Redis 设置过期时间来实现。
Cache::tags([ ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId] ) ->put(ThemePreviewInterface::THEME_EDITOR_SESSION_PREFIX . $sessionId, json_encode($data), ThemePreviewInterface::TTL); $names = Cache::tags(ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId)->getTags()->getNames(); var_dump($names); foreach ($names as $name) { var_dump(Cache::getPrefix() . Cache::tags(ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId)->getTags()->tagKey($name)); var_dump(Redis::connection('cache')->command('expire', [Cache::getPrefix() . Cache::tags(ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId)->getTags()->tagKey($name), ThemePreviewInterface::TTL])); var_dump(Cache::getPrefix() . Cache::tags(ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId)->getTags()->tagId($name) . ':' . RedisTaggedCache::REFERENCE_KEY_STANDARD); var_dump(Redis::connection('cache')->command('expire', [Cache::getPrefix() . Cache::tags(ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId)->getTags()->tagId($name) . ':' . RedisTaggedCache::REFERENCE_KEY_STANDARD, ThemePreviewInterface::TTL])); } // var_dump(Cache::tags(ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId)->getTags()->getNames()); // var_dump(Cache::getPrefix()); exit;
5、打印结果如下。如图3
array(1) { [0]=> string(49) "theme_editor_I37HieXlW2yzmNqWTwGsMoRu3vjivgpaGJ1T" } string(93) "object_local_cache:tag:theme_editor_I37HieXlW2yzmNqWTwGsMoRu3vjivgpaGJ1T:key" bool(true) string(71) "object_local_cache:63326932d41fb365623106:standard_ref" bool(true)
6、Redis 中生成了 3 个 key,如图4
object_local_database_object_local_cache:tag:theme_editor_I37HieXlW2yzmNqWTwGsMoRu3vjivgpaGJ1T:key object_local_database_object_local_cache:63326932d41fb365623106:standard_ref object_local_database_object_local_cache:59840c6d708feb22c6540bd14dbeb0c34bf4bbc4:theme_editor_session:I37HieXlW2yzmNqWTwGsMoRu3vjivgpaGJ1T
7、由于过期时间设置 120 秒,当 2 分钟过去后,Redis 中的数据从 12 条减少至 9 条。之前生成的 3 个 key 已经自动被删除。如图5
8、最终实现代码如下
$name = ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId; $tags = Cache::tags($name); $result = $tags->put(ThemePreviewInterface::THEME_EDITOR_SESSION_PREFIX . $sessionId, json_encode($data), ThemePreviewInterface::TTL); if ($result) { Redis::connection('cache')->expire(Cache::getPrefix() . $tags->getTags()->tagKey($name), ThemePreviewInterface::TTL); Redis::connection('cache')->expire(Cache::getPrefix() . $tags->getTags()->tagId($name) . ':' . RedisTaggedCache::REFERENCE_KEY_STANDARD, ThemePreviewInterface::TTL); } return $result;
近期评论