caching

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const FlashDbDefaultJanitorEvery = 2 * time.Minute // default janitor interval
View Source
const FlashDbDefaultShardCount = 128 // sensible default for concurrency
View Source
const FlashDbMaxSizeInBytes = 1 << 30 // 1 GiB max storage size

Variables

This section is empty.

Functions

This section is empty.

Types

type AerospikeCache

type AerospikeCache struct {
	// contains filtered or unexported fields
}

AerospikeCache provides a high-level caching interface over the Aerospike key-value store.

This struct abstracts operations such as Get, Set, Exists, Delete, Increment, Decrement, and TTL management by internally using an Aerospike client that adheres to the AerospikeInterface.

Fields:

  • aero: An implementation of the AerospikeInterface that handles low-level interactions with the Aerospike database, including connection management and operations.
  • namespace: A logical grouping of data in Aerospike. Similar to a database in relational systems.
  • collection: Represents the 'set' within the namespace, used for organizing related records, similar to a table in relational databases.

Typical usage includes initializing this struct via NewAerospikeCache, and then using its methods to interact with the underlying Aerospike store in a type-safe, request-driven manner.

func NewAerospikeCache

func NewAerospikeCache(opts *Options, connector aerospike.AerospikeConnectorInterface) (*AerospikeCache, error)

NewAerospikeCache creates a new AerospikeCache instance using the provided options. It establishes a connection with the Aerospike server based on the specified cluster.

func (*AerospikeCache) Append

func (a *AerospikeCache) Append(request *AppendRequest) error

Append appends the given values to their corresponding bins in a record using the AppendRequest. Returns an error if the operation fails.

func (*AerospikeCache) AppendContext added in v1.0.1

func (a *AerospikeCache) AppendContext(ctx context.Context, request *AppendRequest) error

func (*AerospikeCache) Decrement

func (a *AerospikeCache) Decrement(request *DecrementRequest) error

Decrement performs atomic subtraction by using AppendOp with negative values from the DecrementRequest. Returns an error if the operation fails.

func (*AerospikeCache) DecrementContext added in v1.0.1

func (a *AerospikeCache) DecrementContext(ctx context.Context, request *DecrementRequest) error

func (*AerospikeCache) Delete

func (a *AerospikeCache) Delete(request *DeleteRequest) (bool, error)

Delete removes a record from Aerospike for the specified key provided in the DeleteRequest. Returns true if the key was deleted, false otherwise along with an error.

func (*AerospikeCache) DeleteContext added in v1.0.1

func (a *AerospikeCache) DeleteContext(ctx context.Context, request *DeleteRequest) (bool, error)

func (*AerospikeCache) Exists

func (a *AerospikeCache) Exists(request *ExistsRequest) (bool, error)

Exists checks if a given key exists in the Aerospike store using the provided ExistsRequest. Returns true if the key exists, otherwise false with an error if any occurs.

func (*AerospikeCache) ExistsContext added in v1.0.1

func (a *AerospikeCache) ExistsContext(ctx context.Context, request *ExistsRequest) (bool, error)

func (*AerospikeCache) Get

func (a *AerospikeCache) Get(request *GetRequest) (any, error)

Get retrieves the value for a given key and optional fields from Aerospike using the provided GetRequest. Returns the bin map associated with the key, or an error if the retrieval fails.

func (*AerospikeCache) GetContext added in v1.0.1

func (a *AerospikeCache) GetContext(ctx context.Context, request *GetRequest) (any, error)

func (*AerospikeCache) GetTTL

func (a *AerospikeCache) GetTTL(request *GetTTLRequest) (int64, error)

GetTTL retrieves the TTL (time to live) of a record for a given key using the GetTTLRequest. Returns the TTL in seconds or an error if the retrieval fails.

func (*AerospikeCache) GetTTLContext added in v1.0.1

func (a *AerospikeCache) GetTTLContext(ctx context.Context, request *GetTTLRequest) (int64, error)

func (*AerospikeCache) Increment

func (a *AerospikeCache) Increment(request *IncrementRequest) error

Increment performs atomic addition on one or more bins in a record specified in the IncrementRequest. Returns an error if any part of the operation fails.

func (*AerospikeCache) IncrementContext added in v1.0.1

func (a *AerospikeCache) IncrementContext(ctx context.Context, request *IncrementRequest) error

func (*AerospikeCache) MultiDelete

func (a *AerospikeCache) MultiDelete(request *MultiDeleteRequest) (map[string]bool, error)

MultiDelete deletes multiple records in Aerospike based on the keys in the MultiDeleteRequest. Returns a map of key to boolean indicating whether each record was successfully deleted.

func (*AerospikeCache) MultiDeleteContext added in v1.0.1

func (a *AerospikeCache) MultiDeleteContext(ctx context.Context, request *MultiDeleteRequest) (map[string]bool, error)

func (*AerospikeCache) MultiGet

func (a *AerospikeCache) MultiGet(request *MultiGetRequest) (map[string]any, error)

MultiGet retrieves multiple records from Aerospike for a list of keys provided in the MultiGetRequest. Returns a map of key to bin map for each found key, or an error if the operation fails.

func (*AerospikeCache) MultiGetContext added in v1.0.1

func (a *AerospikeCache) MultiGetContext(ctx context.Context, request *MultiGetRequest) (map[string]any, error)

func (*AerospikeCache) MultiSet

func (a *AerospikeCache) MultiSet(request *MultiSetRequest) (map[string]bool, error)

MultiSet sets multiple records in Aerospike using the provided MultiSetRequest. Returns a map of key to boolean indicating whether each record was successfully written.

func (*AerospikeCache) MultiSetContext added in v1.0.1

func (a *AerospikeCache) MultiSetContext(ctx context.Context, request *MultiSetRequest) (map[string]bool, error)

func (*AerospikeCache) Set

func (a *AerospikeCache) Set(request *SetRequest) (bool, error)

Set stores the given fields in Aerospike under the specified key using the SetRequest. Returns true on successful set, or false and an error if the operation fails.

func (*AerospikeCache) SetContext added in v1.0.1

func (a *AerospikeCache) SetContext(ctx context.Context, request *SetRequest) (bool, error)

func (*AerospikeCache) SetTTL

func (a *AerospikeCache) SetTTL(request *SetTTLRequest) error

SetTTL updates the TTL for a given key using the SetTTLRequest. Returns an error if the TTL update operation fails.

func (*AerospikeCache) SetTTLContext added in v1.0.1

func (a *AerospikeCache) SetTTLContext(ctx context.Context, request *SetTTLRequest) error

type AppendRequest

type AppendRequest struct {
	Key    string            // Key of the record to append to
	Fields map[string]string // Fields and the values to append
	Value  string            // Optional single field append value
	// contains filtered or unexported fields
}

AppendRequest is used to append string values to fields in a record.

func (*AppendRequest) GetCollection

func (c *AppendRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*AppendRequest) GetNamespace

func (c *AppendRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*AppendRequest) SetCollection

func (c *AppendRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*AppendRequest) SetNamespace

func (c *AppendRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type Cache

type Cache interface {

	// Exists checks if the specified key exists in the cache.
	//
	// Parameters:
	//   - request: A pointer to ExistsRequest that includes namespace, collection, and key.
	//
	// Returns:
	//   - bool: True if the key exists, false otherwise.
	//   - error: Any error encountered during the operation.
	Exists(request *ExistsRequest) (bool, error)

	// Get retrieves the value for a specific key.
	//
	// Parameters:
	//   - request: A pointer to GetRequest containing the namespace, collection, key, and optional field(s).
	//
	// Returns:
	//   - any: Retrieved value (often map[string]interface{} in systems like Aerospike).
	//   - error: If the key is not found or operation fails.
	Get(request *GetRequest) (any, error)

	// Set stores or updates a key-value pair in the cache with optional TTL.
	//
	// Parameters:
	//   - request: A pointer to SetRequest specifying namespace, collection, key, fields, and TTL.
	//
	// Returns:
	//   - bool: True if the operation was successful.
	//   - error: If the operation fails.
	Set(request *SetRequest) (bool, error)

	// Delete removes a key from the cache.
	//
	// Parameters:
	//   - request: A pointer to DeleteRequest containing namespace, collection, and key.
	//
	// Returns:
	//   - bool: True if the key was deleted, false if not found.
	//   - error: If deletion fails.
	Delete(request *DeleteRequest) (bool, error)

	// MultiGet fetches multiple values for a list of keys.
	//
	// Parameters:
	//   - request: A pointer to MultiGetRequest with a list of keys.
	//
	// Returns:
	//   - map[string]any: A map of keys to their corresponding values.
	//   - error: If the batch fetch fails.
	MultiGet(request *MultiGetRequest) (map[string]any, error)

	// MultiSet sets multiple key-value pairs in one batch call.
	//
	// Parameters:
	//   - request: A pointer to MultiSetRequest with a map of keys and their fields.
	//
	// Returns:
	//   - map[string]bool: A map indicating which keys were set successfully.
	//   - error: If the batch set operation fails.
	MultiSet(request *MultiSetRequest) (map[string]bool, error)

	// MultiDelete deletes multiple keys from the cache.
	//
	// Parameters:
	//   - request: A pointer to MultiDeleteRequest with a list of keys.
	//
	// Returns:
	//   - map[string]bool: A map of keys to a boolean indicating deletion success.
	//   - error: If the operation encounters an error.
	MultiDelete(request *MultiDeleteRequest) (map[string]bool, error)

	// Increment increases integer values of specified fields by given offsets.
	//
	// Parameters:
	//   - request: A pointer to IncrementRequest with field names and increment values.
	//
	// Returns:
	//   - error: If incrementing fails or is applied to non-numeric values.
	Increment(request *IncrementRequest) error

	// Decrement decreases integer values of specified fields by given offsets.
	//
	// Parameters:
	//   - request: A pointer to DecrementRequest with field names and decrement values.
	//
	// Returns:
	//   - error: If decrementing fails or is applied to non-numeric values.
	Decrement(request *DecrementRequest) error

	// Append appends string values to the specified fields of a key.
	//
	// Parameters:
	//   - request: A pointer to AppendRequest with the data to append.
	//
	// Returns:
	//   - error: If the append operation fails.
	Append(request *AppendRequest) error

	// GetTTL retrieves the remaining TTL for a specific key.
	//
	// Parameters:
	//   - request: A pointer to GetTTLRequest specifying the key.
	//
	// Returns:
	//   - int64: TTL in seconds.
	//   - error: If fetching TTL fails.
	GetTTL(request *GetTTLRequest) (int64, error)

	// SetTTL updates the TTL of a given key.
	//
	// Parameters:
	//   - request: A pointer to SetTTLRequest with the new TTL.
	//
	// Returns:
	//   - error: If updating the TTL fails.
	SetTTL(request *SetTTLRequest) error
}

Cache defines a generic caching interface for key-value stores.

This interface abstracts the operations required to interact with a cache backend such as Aerospike, Redis, or Memcached. It supports both single and batch operations, along with TTL (time-to-live) management and mutation functions.

Implementations of this interface can serve as a plug-and-play cache layer in applications, enabling flexible backend swapping without changing the client code.

func GetProvider

func GetProvider(opts *Options) (Cache, error)

GetProvider returns a cache instance based on the provider specified in the options. Currently, it supports Aerospike as the cache provider.

Parameters:

  • opts: A pointer to Options struct containing provider type, cluster name, namespace, etc.

Returns:

  • Cache: An implementation of the Cache interface corresponding to the selected provider.
  • error: If validation fails or the provider is unsupported.

Example:

cache, err := GetProvider(&Options{
  Provider: ProviderAerospike,
  Cluster:  "my-aerospike-cluster",
  Namespace: "ns1",
  Collection: "users",
})

type CacheProvider

type CacheProvider string

CacheProvider is a string type that represents the identifier for different cache providers.

const (
	// ProviderAerospike represents the cache provider identifier for Aerospike.
	// This constant is used to select the Aerospike-backed implementation of the Cache interface.
	ProviderAerospike CacheProvider = "aerospike"

	// ProviderRedis represents the cache provider identifier for Redis.
	// This constant is used to select the Redis-backed implementation of the Cache interface.
	ProviderRedis CacheProvider = "redis"

	// ProviderMemcached represents the cache provider identifier for Memcached.
	// This constant is used to select the Memcached-backed implementation of the Cache interface.
	ProviderMemcached CacheProvider = "memcached"

	// ProviderFlashDB represents the cache provider identifier for FlashDB.
	// This constant is used to select the FlashDB-backed implementation of the Cache interface.
	ProviderFlashDB CacheProvider = "flashdb"
)

type CacheRequest

type CacheRequest interface {
	GetNamespace() string
	GetCollection() string
	SetNamespace(namespace string)
	SetCollection(collection string)
}

CacheRequest defines the interface for all cache request types. It ensures that each request contains a namespace and collection, which are required to interact with specific cache stores (e.g., Aerospike).

type ContextCache

type ContextCache interface {
	Cache

	ExistsContext(ctx context.Context, request *ExistsRequest) (bool, error)
	GetContext(ctx context.Context, request *GetRequest) (any, error)
	SetContext(ctx context.Context, request *SetRequest) (bool, error)
	DeleteContext(ctx context.Context, request *DeleteRequest) (bool, error)
	MultiGetContext(ctx context.Context, request *MultiGetRequest) (map[string]any, error)
	MultiSetContext(ctx context.Context, request *MultiSetRequest) (map[string]bool, error)
	MultiDeleteContext(ctx context.Context, request *MultiDeleteRequest) (map[string]bool, error)
	IncrementContext(ctx context.Context, request *IncrementRequest) error
	DecrementContext(ctx context.Context, request *DecrementRequest) error
	AppendContext(ctx context.Context, request *AppendRequest) error
	GetTTLContext(ctx context.Context, request *GetTTLRequest) (int64, error)
	SetTTLContext(ctx context.Context, request *SetTTLRequest) error
}

ContextCache is the context-aware form of Cache. Implementations should use the supplied context for backend calls whenever the backend supports it.

type DecrementRequest

type DecrementRequest struct {
	Key    string           // Key of the record to decrement
	Fields map[string]int64 // Fields and their decrement values
	Value  int64            // Optional single field decrement value
	// contains filtered or unexported fields
}

DecrementRequest is used to decrement integer values of fields in a record.

func (*DecrementRequest) GetCollection

func (c *DecrementRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*DecrementRequest) GetNamespace

func (c *DecrementRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*DecrementRequest) SetCollection

func (c *DecrementRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*DecrementRequest) SetNamespace

func (c *DecrementRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type DeleteRequest

type DeleteRequest struct {
	Key string // The key to delete
	// contains filtered or unexported fields
}

DeleteRequest is used to delete a specific key from the cache.

func (*DeleteRequest) GetCollection

func (c *DeleteRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*DeleteRequest) GetNamespace

func (c *DeleteRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*DeleteRequest) SetCollection

func (c *DeleteRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*DeleteRequest) SetNamespace

func (c *DeleteRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type ExistsRequest

type ExistsRequest struct {
	Key string // The cache key to check
	// contains filtered or unexported fields
}

ExistsRequest is used to check the existence of a specific key in the cache.

func (*ExistsRequest) GetCollection

func (c *ExistsRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*ExistsRequest) GetNamespace

func (c *ExistsRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*ExistsRequest) SetCollection

func (c *ExistsRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*ExistsRequest) SetNamespace

func (c *ExistsRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type FlashDBCache

type FlashDBCache struct {
	// contains filtered or unexported fields
}

FlashDBCache is a cache layer built on top of flashdb. It implements the Cache interface and provides a simple in-memory cache.

func NewFlashDBCache

func NewFlashDBCache(opts Options) (*FlashDBCache, error)

NewFlashDBCache builds an in-process cache using flashdb as the provider. We derive sensible flashdb defaults from caching.Options.

func (*FlashDBCache) Append

func (c *FlashDBCache) Append(req *AppendRequest) error

Increment increases numeric fields in the cache. If Fields is provided, those specific fields are incremented.

func (*FlashDBCache) AppendContext added in v1.0.1

func (c *FlashDBCache) AppendContext(ctx context.Context, req *AppendRequest) error

func (*FlashDBCache) Close

func (c *FlashDBCache) Close()

Close releases resources held by the FlashDBCache.

func (*FlashDBCache) Decrement

func (c *FlashDBCache) Decrement(req *DecrementRequest) error

Decrement decreases numeric fields in the cache. If Fields is provided, those specific fields are decremented.

func (*FlashDBCache) DecrementContext added in v1.0.1

func (c *FlashDBCache) DecrementContext(ctx context.Context, req *DecrementRequest) error

func (*FlashDBCache) Delete

func (c *FlashDBCache) Delete(req *DeleteRequest) (bool, error)

Delete removes a key from the FlashDBCache. Returns true if the key was successfully deleted, false otherwise.

func (*FlashDBCache) DeleteContext added in v1.0.1

func (c *FlashDBCache) DeleteContext(ctx context.Context, req *DeleteRequest) (bool, error)

func (*FlashDBCache) Exists

func (c *FlashDBCache) Exists(req *ExistsRequest) (bool, error)

Exists checks if a key exists in the FlashDBCache. Returns true if the key is found, false otherwise.

func (*FlashDBCache) ExistsContext added in v1.0.1

func (c *FlashDBCache) ExistsContext(ctx context.Context, req *ExistsRequest) (bool, error)

func (*FlashDBCache) Get

func (c *FlashDBCache) Get(req *GetRequest) (any, error)

Get retrieves the value associated with a key from the FlashDBCache. Returns nil if the key is not found.

func (*FlashDBCache) GetContext added in v1.0.1

func (c *FlashDBCache) GetContext(ctx context.Context, req *GetRequest) (any, error)

func (*FlashDBCache) GetTTL

func (c *FlashDBCache) GetTTL(req *GetTTLRequest) (int64, error)

GetTTL retrieves the remaining TTL for a key in seconds. Returns 0 if the key does not have a TTL or does not exist.

func (*FlashDBCache) GetTTLContext added in v1.0.1

func (c *FlashDBCache) GetTTLContext(ctx context.Context, req *GetTTLRequest) (int64, error)

func (*FlashDBCache) Increment

func (c *FlashDBCache) Increment(req *IncrementRequest) error

Increment increases numeric fields in the cache. If Fields is provided, those specific fields are incremented.

func (*FlashDBCache) IncrementContext added in v1.0.1

func (c *FlashDBCache) IncrementContext(ctx context.Context, req *IncrementRequest) error

func (*FlashDBCache) MultiDelete

func (c *FlashDBCache) MultiDelete(req *MultiDeleteRequest) (map[string]bool, error)

MultiSet sets multiple keys in the FlashDBCache. Returns a map indicating success status for each key.

func (*FlashDBCache) MultiDeleteContext added in v1.0.1

func (c *FlashDBCache) MultiDeleteContext(ctx context.Context, req *MultiDeleteRequest) (map[string]bool, error)

func (*FlashDBCache) MultiGet

func (c *FlashDBCache) MultiGet(req *MultiGetRequest) (map[string]any, error)

MultiGet retrieves multiple keys from the FlashDBCache. Returns a map of keys to their values; missing keys will be absent.

func (*FlashDBCache) MultiGetContext added in v1.0.1

func (c *FlashDBCache) MultiGetContext(ctx context.Context, req *MultiGetRequest) (map[string]any, error)

func (*FlashDBCache) MultiSet

func (c *FlashDBCache) MultiSet(req *MultiSetRequest) (map[string]bool, error)

MultiSet sets multiple keys in the FlashDBCache. Returns a map indicating success status for each key.

func (*FlashDBCache) MultiSetContext added in v1.0.1

func (c *FlashDBCache) MultiSetContext(ctx context.Context, req *MultiSetRequest) (map[string]bool, error)

func (*FlashDBCache) Set

func (c *FlashDBCache) Set(req *SetRequest) (bool, error)

Set stores the value for a key in the FlashDBCache. Returns true if the operation succeeded, false otherwise.

func (*FlashDBCache) SetContext added in v1.0.1

func (c *FlashDBCache) SetContext(ctx context.Context, req *SetRequest) (bool, error)

func (*FlashDBCache) SetTTL

func (c *FlashDBCache) SetTTL(req *SetTTLRequest) error

SetTTL sets a new TTL for a key. If TTL is 0 or negative, the key will have no expiration.

func (*FlashDBCache) SetTTLContext added in v1.0.1

func (c *FlashDBCache) SetTTLContext(ctx context.Context, req *SetTTLRequest) error

type GetRequest

type GetRequest struct {
	Key    string   // The cache key to retrieve
	Fields []string // Optional list of fields (bins) to fetch
	// contains filtered or unexported fields
}

GetRequest is used to retrieve a specific record or fields from the cache.

func (*GetRequest) GetCollection

func (c *GetRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*GetRequest) GetNamespace

func (c *GetRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*GetRequest) SetCollection

func (c *GetRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*GetRequest) SetNamespace

func (c *GetRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type GetTTLRequest

type GetTTLRequest struct {
	Key string // The key for which TTL is queried
	// contains filtered or unexported fields
}

GetTTLRequest is used to fetch the TTL (time-to-live) of a specific key.

func (*GetTTLRequest) GetCollection

func (c *GetTTLRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*GetTTLRequest) GetNamespace

func (c *GetTTLRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*GetTTLRequest) SetCollection

func (c *GetTTLRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*GetTTLRequest) SetNamespace

func (c *GetTTLRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type IncrementRequest

type IncrementRequest struct {
	Key    string           // Key of the record to increment
	Fields map[string]int64 // Fields and their increment values
	Value  int64            // Optional single field increment value
	// contains filtered or unexported fields
}

IncrementRequest is used to increment integer values of fields in a record.

func (*IncrementRequest) GetCollection

func (c *IncrementRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*IncrementRequest) GetNamespace

func (c *IncrementRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*IncrementRequest) SetCollection

func (c *IncrementRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*IncrementRequest) SetNamespace

func (c *IncrementRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type MemcachedCache

type MemcachedCache struct {
	// contains filtered or unexported fields
}

MemcachedCache provides a caching layer backed by Memcached.

It implements the generic Cache interface and supports basic cache operations like Get, Set, Delete, Increment, Decrement, Append, and batch operations.

Keys are internally formatted using a "namespace:collection:key" pattern to allow logical grouping and separation across different applications or modules sharing the same Memcached cluster.

MemcachedCache ensures efficient use of connection pooling, safe concurrent access, and serializes non-string/[]byte values to JSON before storing.

Limitations:

  • Native TTL retrieval (GetTTL) is not supported by Memcached.
  • Values are always stored as byte slices ([]byte) internally.

func NewMemcachedCache

func NewMemcachedCache(opts *Options, connector memcached.MemcachedConnectorInterface) (*MemcachedCache, error)

NewMemcachedCache initializes a new MemcachedCache instance. It connects to the Memcached cluster specified in the options and prepares the client.

func (*MemcachedCache) Append

func (c *MemcachedCache) Append(request *AppendRequest) error

Append appends the given value to an existing key's value. Returns an error if the key does not exist or append fails.

func (*MemcachedCache) AppendContext added in v1.0.1

func (c *MemcachedCache) AppendContext(ctx context.Context, request *AppendRequest) error

func (*MemcachedCache) Decrement

func (c *MemcachedCache) Decrement(request *DecrementRequest) error

Decrement atomically decreases a key's numeric value by the specified amount. Returns an error if the operation fails or key doesn't exist.

func (*MemcachedCache) DecrementContext added in v1.0.1

func (c *MemcachedCache) DecrementContext(ctx context.Context, request *DecrementRequest) error

func (*MemcachedCache) Delete

func (c *MemcachedCache) Delete(request *DeleteRequest) (bool, error)

Delete removes a key from the cache. Returns true if the key was successfully deleted.

func (*MemcachedCache) DeleteContext added in v1.0.1

func (c *MemcachedCache) DeleteContext(ctx context.Context, request *DeleteRequest) (bool, error)

func (*MemcachedCache) Exists

func (c *MemcachedCache) Exists(request *ExistsRequest) (bool, error)

Exists checks if a given key exists in the cache. Returns true if the key is found, false otherwise.

func (*MemcachedCache) ExistsContext added in v1.0.1

func (c *MemcachedCache) ExistsContext(ctx context.Context, request *ExistsRequest) (bool, error)

func (*MemcachedCache) Get

func (c *MemcachedCache) Get(request *GetRequest) (any, error)

Get retrieves the value associated with a given key. Returns nil if the key is not found.

func (*MemcachedCache) GetContext added in v1.0.1

func (c *MemcachedCache) GetContext(ctx context.Context, request *GetRequest) (any, error)

func (*MemcachedCache) GetTTL

func (c *MemcachedCache) GetTTL(request *GetTTLRequest) (int64, error)

GetTTL is not supported in Memcached. Always returns an error indicating unsupported operation.

func (*MemcachedCache) GetTTLContext added in v1.0.1

func (c *MemcachedCache) GetTTLContext(ctx context.Context, request *GetTTLRequest) (int64, error)

func (*MemcachedCache) Increment

func (c *MemcachedCache) Increment(request *IncrementRequest) error

Increment atomically increases a key's numeric value by the specified amount. Returns an error if the operation fails or key doesn't exist.

func (*MemcachedCache) IncrementContext added in v1.0.1

func (c *MemcachedCache) IncrementContext(ctx context.Context, request *IncrementRequest) error

func (*MemcachedCache) MultiDelete

func (c *MemcachedCache) MultiDelete(request *MultiDeleteRequest) (map[string]bool, error)

MultiDelete deletes multiple keys from the cache. Returns a map of success status per key.

func (*MemcachedCache) MultiDeleteContext added in v1.0.1

func (c *MemcachedCache) MultiDeleteContext(ctx context.Context, request *MultiDeleteRequest) (map[string]bool, error)

func (*MemcachedCache) MultiGet

func (c *MemcachedCache) MultiGet(request *MultiGetRequest) (map[string]any, error)

MultiGet retrieves multiple values for a list of keys. Returns a map of keys to values, missing keys will be absent.

func (*MemcachedCache) MultiGetContext added in v1.0.1

func (c *MemcachedCache) MultiGetContext(ctx context.Context, request *MultiGetRequest) (map[string]any, error)

func (*MemcachedCache) MultiSet

func (c *MemcachedCache) MultiSet(request *MultiSetRequest) (map[string]bool, error)

MultiSet sets multiple key-value pairs into the cache in batch. Returns a map of success status per key.

func (*MemcachedCache) MultiSetContext added in v1.0.1

func (c *MemcachedCache) MultiSetContext(ctx context.Context, request *MultiSetRequest) (map[string]bool, error)

func (*MemcachedCache) Set

func (c *MemcachedCache) Set(request *SetRequest) (bool, error)

Set stores a value in the cache with optional TTL expiration. Returns true if the operation succeeds.

func (*MemcachedCache) SetContext added in v1.0.1

func (c *MemcachedCache) SetContext(ctx context.Context, request *SetRequest) (bool, error)

func (*MemcachedCache) SetTTL

func (c *MemcachedCache) SetTTL(request *SetTTLRequest) error

SetTTL updates the expiration time (TTL) of an existing key. Internally uses the Memcached Touch() operation.

func (*MemcachedCache) SetTTLContext added in v1.0.1

func (c *MemcachedCache) SetTTLContext(ctx context.Context, request *SetTTLRequest) error

type MultiDeleteRequest

type MultiDeleteRequest struct {
	Keys []string // Keys to delete
	// contains filtered or unexported fields
}

MultiDeleteRequest is used to delete multiple keys from the cache.

func (*MultiDeleteRequest) GetCollection

func (c *MultiDeleteRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*MultiDeleteRequest) GetNamespace

func (c *MultiDeleteRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*MultiDeleteRequest) SetCollection

func (c *MultiDeleteRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*MultiDeleteRequest) SetNamespace

func (c *MultiDeleteRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type MultiGetRequest

type MultiGetRequest struct {
	Keys []string // List of keys to retrieve
	// contains filtered or unexported fields
}

MultiGetRequest is used to fetch multiple keys in a single request.

func (*MultiGetRequest) GetCollection

func (c *MultiGetRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*MultiGetRequest) GetNamespace

func (c *MultiGetRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*MultiGetRequest) SetCollection

func (c *MultiGetRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*MultiGetRequest) SetNamespace

func (c *MultiGetRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type MultiSetRequest

type MultiSetRequest struct {
	FieldsMap map[string]map[string]any // Map of keys to their field maps
	ValueMap  map[string]any            // Optional values for simple stores
	TTL       int64                     // TTL applied to all keys
	// contains filtered or unexported fields
}

MultiSetRequest is used to set multiple keys in a single operation.

func (*MultiSetRequest) GetCollection

func (c *MultiSetRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*MultiSetRequest) GetNamespace

func (c *MultiSetRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*MultiSetRequest) SetCollection

func (c *MultiSetRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*MultiSetRequest) SetNamespace

func (c *MultiSetRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type Options

type Options struct {
	Provider    CacheProvider // Provider specifies the cache provider to use (e.g., "aerospike").
	Cluster     string        // Cluster defines the target cluster identifier or connection alias for the cache provider.
	Hosts       []string      // Hosts is a list of host addresses for the cache provider (e.g., Aerospike nodes).
	ConnTimeout int64         // ConnTimeout is the connection timeout duration in seconds.
	DefaultTTL  int64         // DefaultTTL is the default time-to-live for cache entries, in seconds.
	Namespace   string        // Namespace specifies the namespace within the cache system (used in Aerospike).
	Collection  string        // Collection denotes the logical grouping of records, similar to a set (used in Aerospike).
	Username    string        // Username  denotes the authentication username required to login (Optional)
	Password    string        // Password denotes the authentication password required to login (Optional)
}

Options holds the configuration settings required to initialize a Cache provider.

type RedisCache

type RedisCache struct {
	// contains filtered or unexported fields
}

RedisCache provides a caching layer backed by Redis.

func NewRedisCache

func NewRedisCache(opts *Options, connector redis.RedisConnectorInterface) (*RedisCache, error)

NewRedisCache initializes a new RedisCache instance using the provided options.

func (*RedisCache) Append

func (r *RedisCache) Append(request *AppendRequest) error

Append appends data to a string key.

func (*RedisCache) AppendContext

func (r *RedisCache) AppendContext(ctx context.Context, request *AppendRequest) error

func (*RedisCache) Decrement

func (r *RedisCache) Decrement(request *DecrementRequest) error

Decrement decreases the numeric value for a key.

func (*RedisCache) DecrementContext

func (r *RedisCache) DecrementContext(ctx context.Context, request *DecrementRequest) error

func (*RedisCache) Delete

func (r *RedisCache) Delete(request *DeleteRequest) (bool, error)

Delete removes a key from Redis.

func (*RedisCache) DeleteContext

func (r *RedisCache) DeleteContext(ctx context.Context, request *DeleteRequest) (bool, error)

func (*RedisCache) Exists

func (r *RedisCache) Exists(request *ExistsRequest) (bool, error)

Exists checks if the key exists in Redis.

func (*RedisCache) ExistsContext

func (r *RedisCache) ExistsContext(ctx context.Context, request *ExistsRequest) (bool, error)

func (*RedisCache) Get

func (r *RedisCache) Get(request *GetRequest) (any, error)

Get retrieves a value from Redis.

func (*RedisCache) GetContext

func (r *RedisCache) GetContext(ctx context.Context, request *GetRequest) (any, error)

func (*RedisCache) GetTTL

func (r *RedisCache) GetTTL(request *GetTTLRequest) (int64, error)

GetTTL returns the remaining TTL for a key.

func (*RedisCache) GetTTLContext

func (r *RedisCache) GetTTLContext(ctx context.Context, request *GetTTLRequest) (int64, error)

func (*RedisCache) Increment

func (r *RedisCache) Increment(request *IncrementRequest) error

Increment increases the numeric value for a key.

func (*RedisCache) IncrementContext

func (r *RedisCache) IncrementContext(ctx context.Context, request *IncrementRequest) error

func (*RedisCache) MultiDelete

func (r *RedisCache) MultiDelete(request *MultiDeleteRequest) (map[string]bool, error)

MultiDelete deletes multiple keys.

func (*RedisCache) MultiDeleteContext

func (r *RedisCache) MultiDeleteContext(ctx context.Context, request *MultiDeleteRequest) (map[string]bool, error)

func (*RedisCache) MultiGet

func (r *RedisCache) MultiGet(request *MultiGetRequest) (map[string]any, error)

MultiGet retrieves multiple keys in batch.

func (*RedisCache) MultiGetContext

func (r *RedisCache) MultiGetContext(ctx context.Context, request *MultiGetRequest) (map[string]any, error)

func (*RedisCache) MultiSet

func (r *RedisCache) MultiSet(request *MultiSetRequest) (map[string]bool, error)

MultiSet stores multiple key-value pairs in one batch.

func (*RedisCache) MultiSetContext

func (r *RedisCache) MultiSetContext(ctx context.Context, request *MultiSetRequest) (map[string]bool, error)

func (*RedisCache) Set

func (r *RedisCache) Set(request *SetRequest) (bool, error)

Set stores a key-value pair in Redis with optional TTL.

func (*RedisCache) SetContext

func (r *RedisCache) SetContext(ctx context.Context, request *SetRequest) (bool, error)

func (*RedisCache) SetTTL

func (r *RedisCache) SetTTL(request *SetTTLRequest) error

SetTTL updates the TTL for a key.

func (*RedisCache) SetTTLContext

func (r *RedisCache) SetTTLContext(ctx context.Context, request *SetTTLRequest) error

type SetRequest

type SetRequest struct {
	Key    string         // The key to set
	Value  any            // The value to set (optional if using Fields)
	Fields map[string]any // Field-level values for bin-based stores like Aerospike
	TTL    int64          // Time-to-live in seconds
	// contains filtered or unexported fields
}

SetRequest is used to insert or update a key in the cache.

func (*SetRequest) GetCollection

func (c *SetRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*SetRequest) GetNamespace

func (c *SetRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*SetRequest) SetCollection

func (c *SetRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*SetRequest) SetNamespace

func (c *SetRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

type SetTTLRequest

type SetTTLRequest struct {
	Key string // The key to update
	TTL int64  // New TTL in seconds
	// contains filtered or unexported fields
}

SetTTLRequest is used to update the TTL of a specific key.

func (*SetTTLRequest) GetCollection

func (c *SetTTLRequest) GetCollection() string

GetCollection returns the collection for the cache request.

func (*SetTTLRequest) GetNamespace

func (c *SetTTLRequest) GetNamespace() string

GetNamespace returns the namespace for the cache request.

func (*SetTTLRequest) SetCollection

func (c *SetTTLRequest) SetCollection(collection string)

SetCollection sets the collection for the cache request.

func (*SetTTLRequest) SetNamespace

func (c *SetTTLRequest) SetNamespace(namespace string)

SetNamespace sets the namespace for the cache request.

Jump to

Keyboard shortcuts

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