Documentation
¶
Overview ¶
Package cache provides a simple in-memory concurrency-safe key-value store for strings, with optional expiration. Think of it as a basic and local version of Redis or https://github.com/patrickmn/go-cache.
Index ¶
- Constants
- type Cache
- func (c *Cache[T]) Add(key string, value T, ttl time.Duration) bool
- func (c *Cache[T]) Clear()
- func (c *Cache[T]) Del(key string)
- func (c *Cache[T]) Get(key string) (T, bool)
- func (c *Cache[T]) Item(key string) (Item[T], bool)
- func (c *Cache[T]) ItemCount() int
- func (c *Cache[T]) Items() map[string]Item[T]
- func (c *Cache[T]) Keys() []string
- func (c *Cache[T]) Len() int
- func (c *Cache[T]) Replace(key string, value T, ttl time.Duration) bool
- func (c *Cache[T]) Set(key string, value T, ttl time.Duration)
- type Item
Constants ¶
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
Cache is an in-memory concurrency-safe key-value store for strings, with optional expiration. Think of it as a basic and local version of Redis or https://github.com/patrickmn/go-cache.
func (*Cache[T]) Add ¶
Add adds a value to the cache only if the key does not already exist or is expired. It returns true if the item was added, false otherwise. Note that any negative TTL value is treated as NoExpiration.
func (*Cache[T]) Del ¶
Del removes a specified item from the cache. If the item does not exist, this is a no-op.
func (*Cache[T]) Get ¶
Get retrieves a value from the cache, and also returns a boolean indicating if it was found and not expired. It also handles lazy expiration (deleting expired items).
func (*Cache[T]) Item ¶
Item retrieves a copy of an Item from the cache, and also returns a boolean indicating if it was found and not expired. It also handles lazy expiration (deleting expired items).
func (*Cache[T]) ItemCount ¶
ItemCount returns the number of unexpired items in the cache. This is different from [Len], which returns the total number of items, including expired-but-still-present ones.
func (*Cache[T]) Keys ¶
Keys returns a slice of all unexpired keys in the cache. The order of keys is not guaranteed.
func (*Cache[T]) Len ¶
Len returns the total number of items in the cache, including expired-but-still-present ones.
func (*Cache[T]) Replace ¶
Replace updates a value in the cache only if the key already exists and is not expired. It returns true if the item was replaced, false otherwise. See also KeepTTL, and note that any negative TTL value is treated as NoExpiration.
func (*Cache[T]) Set ¶
Set adds a value to the cache with an optional Time-To-Live duration until it expires (see also DefaultExpiration and NoExpiration). Note that any negative TTL value is treated as NoExpiration.