memory

package
v3.82.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2024 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

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 New

func New[T any](opts ...CacheOption[T]) *Cache[T]

New 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 NewWithData

func NewWithData[T any](data []CacheEntry[T], opts ...CacheOption[T]) *Cache[T]

NewWithData 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

func (c *Cache[T]) Contents() string

Contents returns a comma-separated string containing all keys in the cache.

func (*Cache[T]) Count

func (c *Cache[T]) Count() int

Count returns the number of key-value pairs in the cache.

func (*Cache[T]) Delete

func (c *Cache[T]) Delete(key string)

Delete removes the key-value pair from the cache.

func (*Cache[T]) Exists

func (c *Cache[T]) Exists(key string) bool

Exists returns true if the given key exists in the cache.

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (T, bool)

Get returns the value for the given key.

func (*Cache[T]) Keys added in v3.34.0

func (c *Cache[T]) Keys() []string

Keys returns all keys in the cache.

func (*Cache[T]) Set

func (c *Cache[T]) Set(key string, value T)

Set adds a key-value pair to the cache.

func (*Cache[T]) Values added in v3.34.0

func (c *Cache[T]) Values() []T

Values returns all values in the cache.

type CacheEntry added in v3.63.9

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 added in v3.63.9

type CacheOption[T any] func(*Cache[T])

CacheOption defines a function type used for configuring a Cache.

func WithExpirationInterval added in v3.63.9

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 added in v3.63.9

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL