Documentation ¶
Overview ¶
Package cache implements a basic in memory key-value storage system using map as storing mechanism. The cache and the cache items also have an expiration time. The cache will be invalidated once the expiration time is reached. On cache initialization a cleanup interval is also required. The scope of the cleanup method is to run at a predefined interval and to remove all the expired cache items.
Example (ExpirationTime) ¶
c1 := New[string, string](NoExpiration, 0) c1.Set("item1", "a", DefaultExpiration) item, _ := c1.Get("item1") fmt.Println(item.expiration) c1.Update("item1", "b", NoExpiration) item, _ = c1.Get("item1") fmt.Println(item.expiration) err := c1.DeleteExpired() fmt.Println(err) c1.Set("item1", "a", 10*time.Millisecond) <-time.After(20 * time.Millisecond) c1.DeleteExpired() fmt.Println(c1.Count()) c1.Set("item1", "b", 1*time.Millisecond) c1.Set("item2", "b", 50*time.Millisecond) <-time.After(20 * time.Millisecond) c1.DeleteExpired() fmt.Println(c1.Count()) <-time.After(70 * time.Millisecond) c1.DeleteExpired() fmt.Println(c1.Count()) c2 := New[string, int](5*time.Millisecond, 100*time.Millisecond) c2.Set("a", 1, DefaultExpiration) c2.Set("b", 2, NoExpiration) c2.Set("c", 3, 50*time.Millisecond) c2.Set("d", 4, 200*time.Millisecond) <-time.After(150 * time.Millisecond) fmt.Println(c2.Count()) <-time.After(300 * time.Millisecond) fmt.Println(c2.Count())
Output: -1 -1 <nil> 1 2 1 2 1
Example (LRUCache) ¶
c, _ := NewLRU[string, string](3) item, available := c.Get("foo") fmt.Println(available) c.Add("foo", "bar") item, available = c.Get("foo") fmt.Println(available) fmt.Println(item) c.Add("foo2", "bar2") c.Add("foo3", "bar3") c.Add("foo4", "baz") fmt.Println(c.Count()) fmt.Println() oldestKey, oldestValue, oldestAvailable := c.GetOldest() fmt.Println(oldestAvailable) fmt.Println(oldestKey) fmt.Println(oldestValue) fmt.Println() youngestKey, youngestValue, youngestAvailable := c.GetYoungest() fmt.Println(youngestAvailable) fmt.Println(youngestKey) fmt.Println(youngestValue) fmt.Println() oldestKey, oldestValue, oldestAvailable = c.RemoveOldest() fmt.Println(oldestAvailable) fmt.Println(oldestKey) fmt.Println(oldestValue) fmt.Println()
Output: false true bar 3 true foo2 bar2 true foo2 bar2 true foo3 bar3
Index ¶
- Constants
- type Cache
- func (c *Cache[K, V]) Count() int
- func (c *Cache[K, V]) Delete(key K) error
- func (c Cache) DeleteExpired() error
- func (c *Cache[K, V]) Flush()
- func (c *Cache[K, V]) Get(key K) (*Item[V], error)
- func (c *Cache[K, V]) IsExpired(key K) bool
- func (c *Cache[K, V]) List() map[K]*Item[V]
- func (c *Cache[K, V]) MapToCache(m map[K]V, d time.Duration) error
- func (c *Cache[K, V]) Set(key K, val V, d time.Duration) error
- func (c *Cache[K, V]) SetDefault(key K, val V) error
- func (c *Cache[K, V]) Update(key K, val V, d time.Duration) error
- type Item
- type LRUCache
- func (c *LRUCache[K, V]) Add(key K, value V) (oldestKey K, oldestValue V, removed bool)
- func (c *LRUCache[K, V]) Count() int
- func (c *LRUCache[K, V]) Flush()
- func (c *LRUCache[K, V]) Get(key K) (value V, available bool)
- func (c *LRUCache[K, V]) GetOldest() (key K, value V, available bool)
- func (c *LRUCache[K, V]) GetYoungest() (key K, value V, available bool)
- func (c *LRUCache[K, V]) Remove(key K) (value V, removed bool)
- func (c *LRUCache[K, V]) RemoveOldest() (key K, value V, removed bool)
- func (c *LRUCache[K, V]) RemoveYoungest() (key K, value V, removed bool)
Examples ¶
Constants ¶
const ( NoExpiration time.Duration = -1 DefaultExpiration time.Duration = 0 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
Cache is a publicly available struct type, which incorporates the unexported cache struct type holding the cache components.
Example ¶
c := New[string, string](DefaultExpiration, 1*time.Minute) item, err := c.Get("foo") fmt.Println(err) fmt.Println(item) c.Set("foo", "bar", DefaultExpiration) item, _ = c.Get("foo") fmt.Println(item.Val()) err = c.Set("foo", "", DefaultExpiration) fmt.Println(err) fmt.Println(c.IsExpired("foo")) c.Update("foo", "baz", DefaultExpiration) item, _ = c.Get("foo") fmt.Println(item.Val()) list := c.List() fmt.Println(len(list)) c.Flush() fmt.Println(c.Count()) c.Set("foo", "bar", DefaultExpiration) item, _ = c.Get("foo") fmt.Println(item.Val()) err = c.Delete("foo") fmt.Println(err) fmt.Println(c.Count())
Output: item with key 'foo' not found <nil> bar item with key 'foo' already exists. Use the Update method false baz 1 0 bar <nil> 0
func New ¶
New instantiates a cache struct which requires an expiration time and a cleanup interval. The cache will be invalidated once the expiration time is reached. If the expiration time is less than zero (or NoExpiration) the cache items will never expire and should be deleted manually. A cleanup method is running in the background and removes the expired caches at a predefined interval.
func (Cache) DeleteExpired ¶
func (c Cache) DeleteExpired() error
DeleteExpired removes all the expired items from the cache.
func (*Cache[K, V]) Flush ¶
func (c *Cache[K, V]) Flush()
Flush removes all the existing items in the cache.
func (*Cache[K, V]) Get ¶
Get returns a cache item defined by its key. If the item is expired an error is returned. If an item is expired it's considered as nonexistent, it will be evicted from the cache when the purge method is invoked at the predefined interval.
func (*Cache[K, V]) MapToCache ¶
MapToCache transfers the map values into the cache.
func (*Cache[K, V]) Set ¶
Set inserts a new item into the cache, but first verifies if an item with the same key already exists in the cache. In case an item with the specified key already exists in the cache it will return an error.
func (*Cache[K, V]) SetDefault ¶
SetDefault adds a new item into the cache with the default expiration time.
type Item ¶
type Item[V any] struct { // contains filtered or unexported fields }
Item holds the cache object (which could be of any type) and an expiration time. The expiration time defines the object lifetime.
type LRUCache ¶ added in v1.0.3
type LRUCache[K comparable, V any] struct { // contains filtered or unexported fields }
LRUCache implements a fixed size LRU cache using a map and a double linked list.
func NewLRU ¶ added in v1.0.3
func NewLRU[K comparable, V any](size int) (*LRUCache[K, V], error)
NewLRU initializes a new LRU cache.
func (*LRUCache[K, V]) Add ¶ added in v1.0.3
Add adds a value to the cache. If the oldest value is evicted, this value and they key for it is returned.
func (*LRUCache[K, V]) Count ¶ added in v1.0.3
Count return the number of the current values from the cache. It should be LE then the initial size of the cache.
func (*LRUCache[K, V]) Flush ¶ added in v1.0.3
func (c *LRUCache[K, V]) Flush()
Flush clears all values from the cache.
func (*LRUCache[K, V]) Get ¶ added in v1.0.3
Get return the element for the key if the element is present in the cache.
func (*LRUCache[K, V]) GetOldest ¶ added in v1.0.3
GetOldest returns the oldest key/value pair from the cache if the cache has any values.
func (*LRUCache[K, V]) GetYoungest ¶ added in v1.0.3
GetYoungest returns the youngest key/value pair from the cache if the cache has any values.
func (*LRUCache[K, V]) Remove ¶ added in v1.0.3
Remove removes an element form the cache denoted by the key. The value removed is returned.
func (*LRUCache[K, V]) RemoveOldest ¶ added in v1.0.3
RemoveOldest removes the oldest value from the cache. It returns he key/value pair which was removed.
func (*LRUCache[K, V]) RemoveYoungest ¶ added in v1.0.3
RemoveYoungest removes the youngest value from the cache. The key/value pair removed is returned.