// NewARC creates an ARC of the given size func NewARC(size int) (*ARCCache, error) { // Create the sub LRUs b1, err := simplelru.NewLRU(size, nil) if err != nil { return nil, err } b2, err := simplelru.NewLRU(size, nil) if err != nil { return nil, err } t1, err := simplelru.NewLRU(size, nil) if err != nil { return nil, err } t2, err := simplelru.NewLRU(size, nil) if err != nil { return nil, err } // Initialize the ARC c := &ARCCache{ size: size, p: 0, t1: t1, b1: b1, t2: t2, b2: b2, } return c, nil }
// NewWithEvict constructs a fixed size cache with the given eviction // callback. func NewWithEvict(size int, onEvicted func(key interface{}, value interface{})) (*Cache, error) { lru, err := simplelru.NewLRU(size, simplelru.EvictCallback(onEvicted)) if err != nil { return nil, err } c := &Cache{ lru: lru, } return c, nil }
// New2QParams creates a new TwoQueueCache using the provided // parameter values. func New2QParams(size int, recentRatio float64, ghostRatio float64) (*TwoQueueCache, error) { if size <= 0 { return nil, fmt.Errorf("invalid size") } if recentRatio < 0.0 || recentRatio > 1.0 { return nil, fmt.Errorf("invalid recent ratio") } if ghostRatio < 0.0 || ghostRatio > 1.0 { return nil, fmt.Errorf("invalid ghost ratio") } // Determine the sub-sizes recentSize := int(float64(size) * recentRatio) evictSize := int(float64(size) * ghostRatio) // Allocate the LRUs recent, err := simplelru.NewLRU(size, nil) if err != nil { return nil, err } frequent, err := simplelru.NewLRU(size, nil) if err != nil { return nil, err } recentEvict, err := simplelru.NewLRU(evictSize, nil) if err != nil { return nil, err } // Initialize the cache c := &TwoQueueCache{ size: size, recentSize: recentSize, recent: recent, frequent: frequent, recentEvict: recentEvict, } return c, nil }