知识卡片
缓存操作区分存在与淘汰
原书版本的LRU接口中,Add返回的是是否发生淘汰,不是是否成功添加;Get返回值与存在标记,并更新最近使用次序;Contains只检查存在而不更新次序,Remove返回此前是否存在。可先用New设定容量,再以Add与Get读写;选择探测方法要避免无意改变淘汰次序。这是原书API说明,具体版本仍需核验。原书在包级变量初始化后、main之前运行的init中创建缓存;实际创建若可能失败,应明确处理错误。
原稿形式:evicted := cache.Add(key, value);value, exists := cache.Get(key),两个布尔结果承担不同职责。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Scalability · 查看原书依据
Add adds a value to the cache and returns true if an eviction occurred. func (c *Cache) Add(key, value interface{}) (evicted bool) // Check if a key is in the cache (without updating the recent-ness). func (c *Cache) Contains(key interface{}) bool // Get looks up a key's value and returns (value, true) if it exists.
参考来源
- 位置:《Cloud Native Go》第07章《Scalability》(来源项目book-sources;源文件:extracted/Cloud Native Go (Matthew A. Titmus)/_epub-src/OEBPS/ch07.xhtml)
- 结论依据:原书版本的LRU接口中,Add返回的是是否发生淘汰,不是是否成功添加;Get返回值与存在标记,并更新最近使用次序;Contains只检查存在而不更新次序,Remove返回此前是否存在。原文相应定义、示例或限制为此结论提供依据;补充纠错与版本边界在正文就近标明。
- 原始内容:Add adds a value to the cache and returns true if an eviction occurred. func (c *Cache) Add(key, value interface{}) (evicted bool) // Check if a key is in the cache (without updating the recent-ness). func (c *Cache) Contains(key interface{}) bool // Get looks up a key's value and returns (value, true) if it exists.