Documentation
¶
Overview ¶
Package cache is a cache package written in Go with no dependency. It uses Least-Recently Used (LRU) algorithm for replacement policy. Behind the package, it uses doubly-linked list which is built-in data structure that comes from container/list. Any data can be stored in cache.
Firstly, you need to create a new cache as follows.
c, err := cache.New(5, cache.Config{})
The first parameter is capacity of the cache. For the example, the cache can keep up to 5 data. If a new data is being tried to add when the cache is full, the cache removes the least-recently used one and adds the new data.
Index ¶
- type Cache
- func (c *Cache) Add(key interface{}, val interface{}, exp time.Duration) error
- func (c *Cache) Cap() int
- func (c *Cache) Clear()
- func (c *Cache) ClearExpiredData()
- func (c *Cache) Contains(key interface{}) bool
- func (c *Cache) Get(key interface{}) (interface{}, bool)
- func (c *Cache) Keys() []interface{}
- func (c *Cache) Len() int
- func (c *Cache) Peek(key interface{}) (interface{}, bool)
- func (c *Cache) Remove(key interface{}) error
- func (c *Cache) RemoveOldest() (k interface{}, v interface{}, ok bool)
- func (c *Cache) Replace(key interface{}, val interface{}) error
- func (c *Cache) Resize(size int) int
- type Config
- type Item
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct { // CleanInterval is the time duration to make cache empty. CleanInterval time.Duration // ExpirationTimeoutInterval indicates the time to delete expired items. ExpirationTimeoutInterval int64 // contains filtered or unexported fields }
Cache is the main cache type.
func New ¶
New creates a new cache and returns it with error type. Capacity of the cache needs to be more than zero.
func (*Cache) Add ¶
Add saves data to cache if it is not saved yet. If the capacity is full, the least-recently used one will be removed and new data will be added. If you do not want to add an expired time for data, you need to pass 0.
func (*Cache) ClearExpiredData ¶ added in v0.6.0
func (c *Cache) ClearExpiredData()
ClearExpiredData deletes the all expired data in cache.
func (*Cache) Contains ¶
Contains checks the given key and returns the information that it exists on cache or not. Calling this function doesn't change the access order of the cache.
func (*Cache) Get ¶
Get retrieves the data from list and returns it with bool information which indicates whether found. If there is no such data in cache, it returns nil and false.
func (*Cache) Keys ¶
func (c *Cache) Keys() []interface{}
Keys returns all keys in cache. It does not change frequency of the item access.
func (*Cache) Peek ¶ added in v0.2.0
Peek returns the given key without updating access frequency of the item.
func (*Cache) Remove ¶
Remove deletes the item from the cache. Updates the length of the cache decrementing by one.
func (*Cache) RemoveOldest ¶ added in v0.3.0
RemoveOldest removes the least recently used one. Returns removed key, value, and bool value that indicates whether remove operation is done successfully.
func (*Cache) Replace ¶ added in v0.6.0
Replace changes the value of the given key, if the key exists. If the key does not exist, it returns error.