Example #1
0
// 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
}
Example #2
0
// NewCache creates a new cache
func NewCache(name string, maxEntries int, maxSizeMB int, maxAge time.Duration) *Cache {
	c := &Cache{
		name:         name,
		maxAge:       maxAge,
		maxSizeBytes: maxSizeMB * 1024 * 1024,
	}

	var err error
	c.lruBackend, err = simplelru.NewLRU(maxEntries, simplelru.EvictCallback(c.onEvicted))
	if err != nil {
		panic(err)
	}
	return c
}