Documentation
¶
Overview ¶
Example ¶
type Foo struct {
Value int
Timestamp time.Time
}
cache := New(10, func(a, b interface{}) bool {
return a.(*Foo).Timestamp.Before(b.(*Foo).Timestamp)
})
item1 := Foo{10, time.Now()}
item2 := Foo{20, time.Now().Add(time.Second)}
cache.Add("one", &item1)
cache.Add("two", &item2)
Index ¶
- type Cache
- func (c *Cache) Add(key interface{}, value interface{})
- func (c *Cache) All(keys ...interface{}) bool
- func (c *Cache) Any(keys ...interface{}) bool
- func (c *Cache) Capacity() int
- func (c *Cache) ChangeCapacity(delta int)
- func (c *Cache) Evict(count int) int
- func (c *Cache) Fix()
- func (c *Cache) Get(key interface{}) (interface{}, bool)
- func (c *Cache) Len() int
- func (c *Cache) Purge()
- func (c *Cache) Remove(keys ...interface{}) (removed int)
- func (c *Cache) SetCapacity(capacity int)
- type Less
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a cache abstraction. It uses user-defined comparator to evaluate priorities of cached items. Items with lowest priorities will be evicted first.
func New ¶
New creates a new Cache instance. Capacity allowed to be zero. In this case cache becomes dummy, 'Add' do nothing and items can't be stored in.
func (*Cache) Add ¶
func (c *Cache) Add(key interface{}, value interface{})
Add adds a `value` into a cache. If `key` already exists, `value` will be overwritten. `key` must be a KeyType (see https://golang.org/ref/spec#KeyType)
func (*Cache) ChangeCapacity ¶
ChangeCapacity change cache capacity by `delta`. If `delta` is positive cache capacity will be expanded, if `delta` is negative, it will be shrunk. Redundant items will be evicted.
func (*Cache) SetCapacity ¶
SetCapacity sets cache capacity. Redundant items will be evicted. Capacity never become less than zero.