Documentation ¶
Index ¶
- Constants
- func Clear() error
- func Close() error
- func Delete(key string) error
- func Get(key string) (interface{}, error)
- func GetDefault(key string, defaultValue interface{}) (interface{}, error)
- func Has(key string) bool
- func Keys() ([]string, error)
- func RegisterCache(name string, cache Cache)
- func RemoveCache(name string)
- func Set(key string, value interface{}, ttl Duration) error
- func SetDefault(cache Cache)
- type Cache
- type CacheConnector
- type Duration
- type MemoryCache
- func (c *MemoryCache[T]) Clear() (err error)
- func (c *MemoryCache[T]) Close() error
- func (c *MemoryCache[T]) Delete(key string) error
- func (c *MemoryCache[T]) Get(key string) (value T, err error)
- func (c *MemoryCache[T]) GetDefault(key string, defaultValue T) (value T, err error)
- func (c *MemoryCache[T]) Has(key string) (exists bool)
- func (c *MemoryCache[T]) Keys() ([]string, error)
- func (c *MemoryCache[T]) Len() int
- func (c *MemoryCache[T]) Run(interval time.Duration)
- func (c *MemoryCache[T]) Set(key string, value T, ttl time.Duration) error
- func (c *MemoryCache[T]) TTL(key string) (ttl time.Duration)
Constants ¶
const DefaultCache = "default"
const (
ErrItemNotFound errorType = iota
)
const Infinity = Duration(1<<63 - 1) // 290 years
Variables ¶
This section is empty.
Functions ¶
func Clear ¶
func Clear() error
Clear removes all keys from the default cache backend.
If any error occurs, Clear should return the error.
func Close ¶
func Close() error
Close closes the default cache backend.
If any error occurs, Close should return the error.
func Delete ¶
Delete removes a key from the default cache backend.
If the key does not exist, Delete should return ErrItemNotFound.
func Get ¶
Get retrieves a value from the default cache backend.
If the key does not exist, Get returns nil and ErrItemNotFound.
func GetDefault ¶
GetDefault retrieves a value from the default cache backend.
If the key does not exist, GetDefault returns the defaultValue.
It may return an error if the key exists but the cache itself returns an error.
func Has ¶
Has returns true if the key exists in the default cache backend.
If any error occurs, Has returns false.
func Keys ¶
Keys returns all keys in the default cache backend.
If any error occurs, Keys returns an empty slice and the error.
func RegisterCache ¶
RegisterCache registers a cache backend with a name.
This can later be used to retrieve the cache backend using GetCache.
func RemoveCache ¶
func RemoveCache(name string)
RemoveCache removes a cache backend from the cache backend registry.
This should be used when a cache backend is no longer needed.
func Set ¶
Set sets a value in the default cache backend.
The value is stored in the cache with the specified key. The value will expire after the specified ttl.
func SetDefault ¶
func SetDefault(cache Cache)
SetDefault sets the default cache backend.
The default cache backend is used by the cache package functions.
Types ¶
type Cache ¶
type Cache interface { // Get retrieves a value from the cache. // // If the key does not exist, Get returns nil and ErrItemNotFound. Get(key string) (interface{}, error) // GetDefault retrieves a value from the cache. // // If the key does not exist, GetDefault returns the defaultValue. // // It may return an error if the key exists but the cache itself returns an error. GetDefault(key string, defaultValue interface{}) (interface{}, error) // Set sets a value in the cache. // // The value is stored in the cache with the specified key. // The value will expire after the specified ttl. // // If the TTL is 0, or Infinity, the value will never expire. Set(key string, value interface{}, ttl Duration) error // TTL returns the time to live for a key. // // If the key does not exist, TTL returns 0. // // If any error occurs, TTL returns 0. TTL(key string) Duration // Has returns true if the key exists in the cache. // // If any error occurs, Has returns false. Has(key string) bool // Delete removes a key from the cache. // // If the key does not exist, Delete should return ErrItemNotFound. Delete(key string) error // Keys returns all keys in the cache. // // If any error occurs, Keys returns an empty slice and the error. Keys() ([]string, error) // Clear removes all keys from the cache. // // If any error occurs, Clear should return the error. Clear() error // Close closes the cache. // // If any error occurs, Close should return the error. Close() error }
type CacheConnector ¶
type MemoryCache ¶
type MemoryCache[T any] struct { // contains filtered or unexported fields }
A simple in-memory cache implementation based on a map of string[TYPE].
Look at the interface implementation in cache.go for more information on the methods.
func NewGenericMemoryCache ¶
func NewGenericMemoryCache[T any]() *MemoryCache[T]
Might as well make it generic, right?
func NewMemoryCache ¶
func NewMemoryCache(interval time.Duration) *MemoryCache[interface{}]
Returns a new in-memory cache.
func (*MemoryCache[T]) Clear ¶
func (c *MemoryCache[T]) Clear() (err error)
func (*MemoryCache[T]) Close ¶
func (c *MemoryCache[T]) Close() error
func (*MemoryCache[T]) Delete ¶
func (c *MemoryCache[T]) Delete(key string) error
func (*MemoryCache[T]) Get ¶
func (c *MemoryCache[T]) Get(key string) (value T, err error)
func (*MemoryCache[T]) GetDefault ¶
func (c *MemoryCache[T]) GetDefault(key string, defaultValue T) (value T, err error)
func (*MemoryCache[T]) Has ¶
func (c *MemoryCache[T]) Has(key string) (exists bool)
func (*MemoryCache[T]) Keys ¶
func (c *MemoryCache[T]) Keys() ([]string, error)
func (*MemoryCache[T]) Len ¶
func (c *MemoryCache[T]) Len() int
func (*MemoryCache[T]) Run ¶
func (c *MemoryCache[T]) Run(interval time.Duration)