没有不值得去解决的问题,也没有不值得去学习的技术!

Practical Analysis of Cache Tags in Laravel 6

After running the program, check the data structure in GUI for Redis. Expand tag. The value of cache:tag:theme_editor:key is: s:22:"633146b13433b471207802";. This indicates the associated key is: cache:633146b13433b471207802:standard_ref. This is the structure generated by the tag: theme_editor. There are 2 keys in total

1. Now store the tagged cache data, applying 2 tags: theme_editor, theme_editor_rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW

PHP

return Cache::tags([
		ThemePreviewInterface::TAG_THEME_EDITOR,
		ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId]
	)
	->put(ThemePreviewInterface::THEME_EDITOR_SESSION_PREFIX . $sessionId, json_encode($data), ThemePreviewInterface::TTL);

2. After running the program, check the data structure in GUI for Redis. Expand tag. The value of cache:tag:theme_editor:key is: s:22:”633146b13433b471207802″;. This indicates the associated key is: cache:633146b13433b471207802:standard_ref. This is the structure generated by the tag: theme_editor. There are 2 keys in total. As shown in Figure 1

After running the program, check the data structure in GUI for Redis. Expand tag. The value of cache:tag:theme_editor:key is: s:22:"633146b13433b471207802";. This indicates the associated key is: cache:633146b13433b471207802:standard_ref. This is the structure generated by the tag: theme_editor. There are 2 keys in total
Figure 1

3. After running the program, check the data structure in GUI for Redis. Expand tag. The value of cache:tag:theme_editor_rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW:key is: s:22:”633146b134d67627586383″;. This indicates the associated key is: cache:633146b134d67627586383:standard_ref. This is the structure generated by the tag: theme_editor_rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW. There are 2 keys in total. As shown in Figure 2

After running the program, check the data structure in GUI for Redis. Expand tag. The value of cache:tag:theme_editor_rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW:key is: s:22:"633146b134d67627586383";. This indicates the associated key is: cache:633146b134d67627586383:standard_ref. This is the structure generated by the tag: theme_editor_rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW. There are 2 keys in total
Figure 2

4. The lists of keys associated with the 2 tags are identical, yielding the corresponding key: cache:327d4fd2bc91588c8a5e4c7248cf680165a2e12a:theme_editor_session:rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW. As shown in Figure 3

The lists of keys associated with the 2 tags are identical, yielding the corresponding key: cache:327d4fd2bc91588c8a5e4c7248cf680165a2e12a:theme_editor_session:rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW
Figure 3

5. Decide to delete the cache data corresponding to cache:327d4fd2bc91588c8a5e4c7248cf680165a2e12a:theme_editor_session:rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW. Do not use the tags method. The operation result is false, indicating deletion failed. As shown in Figure 4

Decide to delete the cache data corresponding to cache:327d4fd2bc91588c8a5e4c7248cf680165a2e12a:theme_editor_session:rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW. Do not use the tags method. The operation result is false, indicating deletion failed
Figure 4
PHP

        $result = Cache::forget($sessionId);

        dd($result);

6. Use the tags method. The operation result is true, indicating successful deletion. As shown in Figure 5

Use the tags method. The operation result is true, indicating successful deletion
Figure 5
PHP

        $result = Cache::tags(ThemePreviewInterface::TAG_THEME_EDITOR . '_' . $sessionId)->flush();

        dd($result);

7. Check the data in Redis. The associated value list in cache:633146b13433b471207802:standard_ref still has 1 record, not deleted: cache:327d4fd2bc91588c8a5e4c7248cf680165a2e12a:theme_editor_session:rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW. The key cache:633146b134d67627586383:standard_ref has been deleted, and the key corresponding to the 1 record in its associated value list has also been deleted. This matches expectations. As shown in Figure 6

Check the data in Redis. The associated value list in cache:633146b13433b471207802:standard_ref still has 1 record, not deleted: cache:327d4fd2bc91588c8a5e4c7248cf680165a2e12a:theme_editor_session:rjxMAeCRpPuaD4LJlljDOMXb47hRdVTb2vPW. The key cache:633146b134d67627586383:standard_ref has been deleted, and the key corresponding to the 1 record in its associated value list has also been deleted. This matches expectations
Figure 6

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系