Documentation
¶
Index ¶
- Constants
- Variables
- func Clear(ctx context.Context) error
- func Close(ctx context.Context) error
- func ContextWithTransaction(ctx context.Context, tx Transaction) context.Context
- func CounterValue(ctx context.Context, key string) (int64, error)
- func Decrement(ctx context.Context, key string, amount int64) (int64, error)
- func Delete(ctx context.Context, key string) error
- func Expire(ctx context.Context, key string, ttl time.Duration) error
- func Get(ctx context.Context, key string) (interface{}, error)
- func GetDefault(ctx context.Context, key string, defaultValue interface{}) (interface{}, error)
- func GetDefaultTTL(provided time.Duration) time.Duration
- func Has(ctx context.Context, key string) bool
- func Increment(ctx context.Context, key string, amount int64) (int64, error)
- func Keys(ctx context.Context) ([]string, error)
- func RegisterCache(name string, cache TransactionalCache)
- func RemoveCache(name string)
- func RunInTx(ctx context.Context, fn func(ctx context.Context, tx Transaction) error) error
- func Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func SetDefault(cache TransactionalCache)
- func SetGetDefaultTTL(fn func(provided time.Duration) time.Duration)
- func TTL(ctx context.Context, key string) time.Duration
- func Typed_ContextWithTransaction[T any](ctx context.Context, tx TypedTransaction[T]) context.Context
- type Cache
- type CacheConnector
- type CacheOperation
- type MemoryCache
- func (c *MemoryCache[T]) Clear(_ context.Context) (err error)
- func (c *MemoryCache[T]) Close(_ context.Context) error
- func (c *MemoryCache[T]) CounterValue(ctx context.Context, key string) (int64, error)
- func (c *MemoryCache[T]) Decrement(ctx context.Context, key string, amount int64) (int64, error)
- func (c *MemoryCache[T]) Delete(_ context.Context, key string) error
- func (c *MemoryCache[T]) Expire(_ context.Context, key string, ttl time.Duration) error
- func (c *MemoryCache[T]) Get(_ context.Context, key string) (value T, err error)
- func (c *MemoryCache[T]) GetDefault(_ context.Context, key string, defaultValue T) (value T, err error)
- func (c *MemoryCache[T]) Has(_ context.Context, key string) (exists bool)
- func (c *MemoryCache[T]) Increment(_ context.Context, key string, amount int64) (int64, error)
- func (c *MemoryCache[T]) Keys(_ context.Context) ([]string, error)
- func (c *MemoryCache[T]) Len(_ context.Context) int
- func (c *MemoryCache[T]) Run(interval time.Duration)
- func (c *MemoryCache[T]) RunInTx(ctx context.Context, ...) error
- func (c *MemoryCache[T]) Set(_ context.Context, key string, value T, ttl time.Duration) error
- func (c *MemoryCache[T]) TTL(_ context.Context, key string) (ttl time.Duration)
- type Transaction
- type TransactionalCache
- type TypedCache
- type TypedTransaction
- type TypedTransactionalCache
Constants ¶
const ( DefaultCache = "default" Infinity = internal.Infinity )
const ( OP_UNKNOWN = internal.OP_UNKNOWN OP_GET = internal.OP_GET // Get(c context.Context, key string) (T, error) OP_GETDEFAULT = internal.OP_GETDEFAULT // GetDefault(c context.Context, key string, defaultValue T) (T, error) OP_SET = internal.OP_SET // Set(c context.Context, key string, value T, ttl time.Duration) error OP_INCR = internal.OP_INCR // Increment(c context.Context, key string, amount int64) (int64, error) OP_DECR = internal.OP_DECR // Decrement(c context.Context, key string, amount int64) (int64, error) OP_CVAL = internal.OP_CVAL // CounterValue(c context.Context, key string) (int64, error) OP_EXPR = internal.OP_EXPR // Expire(c context.Context, key string, ttl time.Duration) error OP_TTL = internal.OP_TTL // TTL(c context.Context, key string) time.Duration OP_HAS = internal.OP_HAS // Has(c context.Context, key string) bool OP_DEL = internal.OP_DEL // Delete(c context.Context, key string) error OP_KEYS = internal.OP_KEYS // Keys(c context.Context) ([]string, error) OP_CLEAR = internal.OP_CLEAR // Clear(c context.Context) error OP_CLOSE = internal.OP_CLOSE // Close(c context.Context) error )
Variables ¶
var ( ErrItemNotFound = internal.ErrItemNotFound ErrInvalidType = internal.ErrInvalidType ErrNotSupported = internal.ErrNotSupported )
Functions ¶
func Clear ¶
Clear removes all keys from the default cache backend.
If any error occurs, Clear should return the error.
If a transaction is active in the context, it will be called on the transaction instead.
func Close ¶
Close closes the default cache backend.
If any error occurs, Close should return the error.
If a transaction is active in the context, it will be called on the transaction instead.
func ContextWithTransaction ¶
func CounterValue ¶
CounterValue retrieves the counter for the specified key. If the key does not exist in the cache, an error is returned.
func Decrement ¶
Decrement atomically decrements a numeric key by the given amount. If the key does not exist, it initializes it to -amount with an infinite TTL. It does NOT reset the TTL of an existing key.
func Delete ¶
Delete removes a key from the default cache backend.
If the key does not exist, Delete should return ErrItemNotFound.
If a transaction is active in the context, it will be called on the transaction instead.
func Expire ¶
Expire sets the TTL for a given key. If the key does not exist in the cache, ErrItemNotFound is returned.
func Get ¶
Get retrieves a value from the default cache backend.
If the key does not exist, Get returns nil and ErrItemNotFound.
If a transaction is active in the context, it will be called on the transaction instead.
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.
If a transaction is active in the context, it will be called on the transaction instead.
func Has ¶
Has returns true if the key exists in the default cache backend.
If any error occurs, Has returns false.
If a transaction is active in the context, it will be called on the transaction instead.
func Increment ¶
Increment atomically increments a numeric key by the given amount. If the key does not exist, it initializes it to the amount with an infinite TTL. It does NOT reset the TTL of an existing key.
func Keys ¶
Keys returns all keys in the default cache backend.
If any error occurs, Keys returns an empty slice and the error.
If a transaction is active in the context, it will be called on the transaction instead.
func RegisterCache ¶
func RegisterCache(name string, cache TransactionalCache)
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 RunInTx ¶
RunInTx executes the given function inside a transaction. The provided txCache should be used for all operations inside the function.
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.
If a transaction is active in the context, it will be called on the transaction instead.
func SetDefault ¶
func SetDefault(cache TransactionalCache)
SetDefault sets the default cache backend.
The default cache backend is used by the cache package functions.
Types ¶
type CacheConnector ¶
type CacheConnector = internal.CacheConnector
type CacheOperation ¶
type CacheOperation = internal.CacheOperation
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]) CounterValue ¶
func (*MemoryCache[T]) Decrement ¶
Decrement atomically decrements a numeric key by the given amount. If the key does not exist, it initializes it to -amount with an infinite TTL. It does NOT reset the TTL of an existing key.
func (*MemoryCache[T]) Delete ¶
func (c *MemoryCache[T]) Delete(_ context.Context, key string) error
func (*MemoryCache[T]) Get ¶
func (c *MemoryCache[T]) Get(_ context.Context, key string) (value T, err error)
func (*MemoryCache[T]) GetDefault ¶
func (c *MemoryCache[T]) GetDefault(_ context.Context, key string, defaultValue T) (value T, err error)
func (*MemoryCache[T]) Has ¶
func (c *MemoryCache[T]) Has(_ context.Context, key string) (exists bool)
func (*MemoryCache[T]) Increment ¶
Increment atomically increments a numeric key by the given amount. If the key does not exist, it initializes it to the amount with an infinite TTL. It does NOT reset the TTL of an existing key.
func (*MemoryCache[T]) Run ¶
func (c *MemoryCache[T]) Run(interval time.Duration)
func (*MemoryCache[T]) RunInTx ¶
func (c *MemoryCache[T]) RunInTx(ctx context.Context, fn func(ctx context.Context, txCache internal.TypedTransaction[T]) error) error
type TransactionalCache ¶
type TransactionalCache = internal.TransactionalCache
type TypedCache ¶
type TypedCache[T any] = internal.TypedCache[T]
type TypedTransaction ¶
type TypedTransaction[T any] = internal.TypedTransaction[T]
type TypedTransactionalCache ¶
type TypedTransactionalCache[T any] = internal.TypedTransactionalCache[T]