cache

package
v1.6.7 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: GPL-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCache = "default"
View Source
const (
	ErrItemNotFound errorType = iota
)
View Source
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

func Delete(key string) error

Delete removes a key from the default cache backend.

If the key does not exist, Delete should return ErrItemNotFound.

func Get

func Get(key string) (interface{}, error)

Get retrieves a value from the default cache backend.

If the key does not exist, Get returns nil and ErrItemNotFound.

func GetDefault

func GetDefault(key string, defaultValue interface{}) (interface{}, error)

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

func Has(key string) bool

Has returns true if the key exists in the default cache backend.

If any error occurs, Has returns false.

func Keys

func Keys() ([]string, error)

Keys returns all keys in the default cache backend.

If any error occurs, Keys returns an empty slice and the error.

func RegisterCache

func RegisterCache(name string, cache Cache)

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

func Set(key string, value interface{}, ttl Duration) error

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
}

func Default

func Default() Cache

GetDefault retrieves the default cache backend.

If the default cache backend does not exist, GetDefault returns nil.

func GetCache

func GetCache(names ...string) Cache

GetCache retrieves a cache backend by name.

If the cache backend does not exist, GetCache returns nil.

type CacheConnector

type CacheConnector interface {
	Connect() error
	HasConnected() bool
}

type Duration

type Duration = time.Duration

func TTL

func TTL(key string) Duration

TTL returns the time to live for a key in the default cache backend.

If the key does not exist, TTL returns 0.

If any error occurs, TTL returns 0.

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)

func (*MemoryCache[T]) Set

func (c *MemoryCache[T]) Set(key string, value T, ttl time.Duration) error

func (*MemoryCache[T]) TTL

func (c *MemoryCache[T]) TTL(key string) (ttl time.Duration)

Jump to

Keyboard shortcuts

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