cache

package
v0.0.40 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cache provides a set of caching utilities for Go applications.

Index

Constants

View Source
const DefaultDelimiter = ":"

DefaultDelimiter is the default delimiter used in cache keys.

Variables

This section is empty.

Functions

func WithConcat added in v0.0.39

func WithConcat(concat func(ns, key string) string) settings.Setting[Option]

WithConcat sets the delimiter option.

Types

type Cache

type Cache[T any] interface {
	// Get retrieves the value associated with the given key.
	Get(ctx context.Context, key string) (T, error)

	// GetAndDelete retrieves the value associated with the given key and deletes it.
	GetAndDelete(ctx context.Context, key string) (T, error)

	// Exists checks if a value exists for the given key.
	Exists(ctx context.Context, key string) error

	// Set sets the value for the given key.
	Set(ctx context.Context, key string, value T, expiration ...time.Duration) error

	// Delete deletes the value associated with the given key.
	Delete(ctx context.Context, key string) error

	// Iterator iterates over all key-value pairs in the cache.
	Iterator(ctx context.Context, fn func(ctx context.Context, key string, value T) bool) error

	// Close releases any resources associated with the cache.
	Close(ctx context.Context) error
}

Cache is the interface that wraps the basic Get, Set, and Delete methods.

type NSCache

type NSCache[T any] interface {
	// Get retrieves the value associated with the given key in the specified namespace.
	Get(ctx context.Context, ns, key string) (T, error)

	// GetAndDelete retrieves the value associated with the given key in the specified namespace and deletes it.
	GetAndDelete(ctx context.Context, ns, key string) (T, error)

	// Exists checks if a value exists for the given key in the specified namespace.
	Exists(ctx context.Context, ns, key string) error

	// Set sets the value for the given key in the specified namespace.
	Set(ctx context.Context, ns, key string, value T, expiration ...time.Duration) error

	// Delete deletes the value associated with the given key in the specified namespace.
	Delete(ctx context.Context, ns, key string) error

	// Iterator iterates over all key-value pairs in the specified namespace.
	Iterator(ctx context.Context, ns string, fn func(ctx context.Context, key string, value T) bool) error

	// Close releases any resources associated with the cache.
	Close(ctx context.Context) error
}

NSCache is the interface that wraps the basic Get, Set, and Delete methods for namespaced values.

type ObjectCache

type ObjectCache interface {
	// Get retrieves the value associated with the given key.
	Get(ctx context.Context, key string) (any, error)

	// GetAndDelete retrieves the value associated with the given key and deletes it.
	GetAndDelete(ctx context.Context, key string) (any, error)

	// Exists checks if a value exists for the given key.
	Exists(ctx context.Context, key string) error

	// Set sets the value for the given key.
	Set(ctx context.Context, key string, value any, expiration ...time.Duration) error

	// Delete deletes the value associated with the given key.
	Delete(ctx context.Context, key string) error

	// Iterator iterates over all key-value pairs in the cache.
	Iterator(ctx context.Context, fn func(ctx context.Context, key string, value any) bool) error

	// Close releases any resources associated with the cache.
	Close(ctx context.Context) error
}

ObjectCache is the interface that wraps the basic Get, Set, and Delete methods for object values.

type ObjectNSCache

type ObjectNSCache interface {
	// Get retrieves the value associated with the given key in the specified namespace.
	Get(ctx context.Context, ns, key string) (any, error)

	// GetAndDelete retrieves the value associated with the given key in the specified namespace and deletes it.
	GetAndDelete(ctx context.Context, ns, key string) (any, error)

	// Exists checks if a value exists for the given key in the specified namespace.
	Exists(ctx context.Context, ns, key string) error

	// Set sets the value for the given key in the specified namespace.
	Set(ctx context.Context, ns, key string, value any, expiration ...time.Duration) error

	// Delete deletes the value associated with the given key in the specified namespace.
	Delete(ctx context.Context, ns, key string) error

	// Iterator iterates over all key-value pairs in the specified namespace.
	Iterator(ctx context.Context, ns string, fn func(ctx context.Context, key string, value any) bool) error

	// Close releases any resources associated with the cache.
	Close(ctx context.Context) error
}

ObjectNSCache is the interface that wraps the basic Get, Set, and Delete methods for namespaced object values.

type Option

type Option struct {
	// Joint is the separator used in cache key generation.
	Concat func(ns, key string) string
}

Option specifies configuration options for the cache.

func DefaultOption added in v0.0.39

func DefaultOption() Option

DefaultOption returns the default option.

Jump to

Keyboard shortcuts

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