Documentation ¶
Index ¶
- type Cache
- func (c *Cache[T]) Clear()
- func (c *Cache[T]) Contents() string
- func (c *Cache[T]) Count() int
- func (c *Cache[T]) Delete(key string)
- func (c *Cache[T]) Exists(key string) bool
- func (c *Cache[T]) Get(key string) (T, bool)
- func (c *Cache[T]) Keys() []string
- func (c *Cache[T]) Set(key string, value T)
- func (c *Cache[T]) Values() []T
- type CacheEntry
- type CacheOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
Cache wraps the go-cache library to provide an in-memory key-value store.
func NewCache ¶
func NewCache[T any](opts ...CacheOption[T]) *Cache[T]
NewCache constructs a new in-memory cache instance with optional configurations. By default, it sets the expiration and purge intervals to 12 and 13 hours, respectively. These defaults can be overridden using the functional options: WithExpirationInterval and WithPurgeInterval.
func NewCacheWithData ¶
func NewCacheWithData[T any](data []CacheEntry[T], opts ...CacheOption[T]) *Cache[T]
NewCacheWithData constructs a new in-memory cache with existing data. It also accepts CacheOption parameters to override default configuration values.
func (*Cache[T]) Clear ¶
func (c *Cache[T]) Clear()
Clear removes all key-value pairs from the cache.
func (*Cache[T]) Contents ¶
Contents returns a comma-separated string containing all keys in the cache.
type CacheEntry ¶
type CacheEntry[T any] struct { // Key is the unique identifier for the entry. Key string // Value is the data stored in the entry. Value T }
CacheEntry represents a single entry in the cache, consisting of a key and its corresponding value.
type CacheOption ¶
CacheOption defines a function type used for configuring a Cache.
func WithExpirationInterval ¶
func WithExpirationInterval[T any](interval time.Duration) CacheOption[T]
WithExpirationInterval returns a CacheOption to set the expiration interval of cache items. The interval determines the duration a cached item remains in the cache before it is expired.
func WithPurgeInterval ¶
func WithPurgeInterval[T any](interval time.Duration) CacheOption[T]
WithPurgeInterval returns a CacheOption to set the interval at which the cache purges expired items. Regular purging helps in freeing up memory by removing stale entries.