1. 概述
Adaptive Replacement Cache(ARC)是一种缓存更换算法,用于提高缓存的命中率。ARC 动态调解缓存战略,以顺应实际的访问模式,从而在不确定的工作负载下表现良好。它通过同时维护两个缓存列表来追踪近来使用和频繁使用的数据块,并根据访问模式在这两个列表之间动态分配缓存空间。
2. 基本概念
ARC 使用两个LRU队列(T1和T2)和两个历史列表(B1和B2):
- T1: 存储一次命中(recently used once)的缓存项。
- T2: 存储多次命中(recently used multiple times)的缓存项。
- B1: 存储曾经在T1中,但已经被移除的缓存项。
- B2: 存储曾经在T2中,但已经被移除的缓存项。
3. 主要操纵
- 缓存命中:如果缓存项在T1或T2中,则为缓存命中。
- 缓存未命中:如果缓存项不在T1和T2中,则为缓存未命中。
ARC的核心操纵如下:
- 插入:
- 若新缓存项在T1或T2中,则移动到T2的前端。
- 若不在T1或T2中,则插入到T1的前端。
- 调解缓存巨细:
- 若新缓存项在B1中,则表明近来访问模式偏向于访问新的缓存项,增加T1的容量。
- 若新缓存项在B2中,则表明近来访问模式偏向于访问频繁访问的缓存项,增加T2的容量。
- 更换:
- 根据动态调解的容量,从T1或T2中移除最旧的缓存项,并将其元数据移动到B1或B2。
4. 线程安全的Go实现示例
以下是一个简单的线程安全的ARC缓存的Go实现:- package arc
- import (
- "container/list"
- "sync"
- )
- type ARC struct {
- mtx sync.Mutex
- capacity int
- t1 *list.List
- b1 *list.List
- t2 *list.List
- b2 *list.List
- cache map[interface{}]*list.Element
- }
- func NewARC(capacity int) *ARC {
- return &ARC{
- capacity: capacity,
- t1: list.New(),
- b1: list.New(),
- t2: list.New(),
- b2: list.New(),
- cache: make(map[interface{}]*list.Element),
- }
- }
- // Get returns the item from the cache.
- func (c *ARC) Get(item any) any {
- c.mtx.Lock()
- defer c.mtx.Unlock()
- if elem, found := c.cache[item]; found {
- c.t2.MoveToFront(elem)
- return elem.Value
- }
- return nil
- }
- func (c *ARC) Put(item any) {
- c.mtx.Lock()
- defer c.mtx.Unlock()
- if c.capacity == 0 {
- return
- }
- if elem, found := c.cache[item]; found {
- elem.Value = item
- c.t2.MoveToFront(elem)
- return
- }
- // not found, so add it to the cache
- if c.t1.Len()+c.t2.Len() == c.capacity {
- if c.t1.Len() == c.capacity {
- c.removeLast(c.b1)
- } else {
- c.removeLast(c.t1)
- }
- } else if c.t1.Len()+c.b1.Len()+c.t2.Len()+c.b2.Len() >= c.capacity {
- if c.t1.Len()+c.b1.Len()+c.t2.Len()+c.b2.Len() == 2*c.capacity {
- c.removeLast(c.b2)
- } else {
- c.removeLast(c.t2)
- }
- }
- // add the new item to the cache
- elem := c.t1.PushFront(item)
- c.cache[item] = elem
- }
- // removeLast removes the last element from the list.
- func (c *ARC) removeLast(l *list.List) {
- if l.Len() == 0 {
- return
- }
- elem := l.Back()
- l.Remove(elem)
- delete(c.cache, elem)
- }
复制代码 说明:
- NewARC:创建一个新的ARC缓存实例。
- Get:从缓存中获取值,并将其提升到T2。
- Put:插入新的缓存项,并根据ARC的规则调解缓存。
- removeLast:从指定列表中移除最后一个元素。
声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。
Author: mengbin
blog: mengbin
Github: mengbin92
cnblogs: 恋水无意
腾讯云开辟者社区:孟斯特
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |