Documentation
¶
Overview ¶
Package backend provides interfaces and types for implementing cache backends. It defines the contract that all cache backends must follow, including operations for storing, retrieving, and managing cached items. The package supports generic backend implementations with type constraints to ensure type safety.
The main interface IBackend provides methods for:
- Getting and setting cache items
- Managing cache capacity and item count
- Removing items and clearing the cache
- Listing items with optional filters
Backend implementations must satisfy the IBackendConstrain type constraint, which currently supports InMemory and Redis backend types.
Index ¶
- func ApplyOptions[T IBackendConstrain](backend *T, options ...Option[T])
- type IBackend
- type IBackendConstrain
- type IFilter
- type InMemory
- func (cacheBackend *InMemory) Capacity() int
- func (cacheBackend *InMemory) Clear(ctx context.Context) error
- func (cacheBackend *InMemory) Count(_ context.Context) int
- func (cacheBackend *InMemory) Get(_ context.Context, key string) (*cache.Item, bool)
- func (cacheBackend *InMemory) List(_ context.Context, filters ...IFilter) ([]*cache.Item, error)
- func (cacheBackend *InMemory) Remove(ctx context.Context, keys ...string) error
- func (cacheBackend *InMemory) Set(_ context.Context, item *cache.Item) error
- func (cacheBackend *InMemory) SetCapacity(capacity int)
- type Option
- type Redis
- func (cacheBackend *Redis) Capacity() int
- func (cacheBackend *Redis) Clear(ctx context.Context) error
- func (cacheBackend *Redis) Count(ctx context.Context) int
- func (cacheBackend *Redis) Get(ctx context.Context, key string) (*cache.Item, bool)
- func (cacheBackend *Redis) List(ctx context.Context, filters ...IFilter) ([]*cache.Item, error)
- func (cacheBackend *Redis) Remove(ctx context.Context, keys ...string) error
- func (cacheBackend *Redis) Set(ctx context.Context, item *cache.Item) error
- func (cacheBackend *Redis) SetCapacity(capacity int)
- type SortOrderFilter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyOptions ¶
func ApplyOptions[T IBackendConstrain](backend *T, options ...Option[T])
ApplyOptions applies the given options to the given backend.
Types ¶
type IBackend ¶
type IBackend[T IBackendConstrain] interface { // Get retrieves the item with the given key from the cache. // If the key is not found in the cache, it returns nil. Get(ctx context.Context, key string) (item *cache.Item, ok bool) // Set adds a new item to the cache. Set(ctx context.Context, item *cache.Item) error // Capacity returns the maximum number of items that can be stored in the cache. Capacity() int // SetCapacity sets the maximum number of items that can be stored in the cache. SetCapacity(capacity int) // Count returns the number of items currently stored in the cache. Count(ctx context.Context) int // Remove deletes the item with the given key from the cache. Remove(ctx context.Context, keys ...string) error // List the items in the cache that meet the specified criteria. List(ctx context.Context, filters ...IFilter) (items []*cache.Item, err error) // Clear removes all items from the cache. Clear(ctx context.Context) error }
IBackend defines the contract that all cache backends must implement. It provides a generic interface for cache operations with type safety through the IBackendConstrain type parameter.
Type parameter T must satisfy IBackendConstrain, limiting implementations to supported backend types like InMemory and Redis.
All methods accept a context.Context parameter for cancellation and timeout control, enabling graceful handling of long-running operations.
func NewInMemory ¶
NewInMemory creates a new in-memory cache with the given options.
type IBackendConstrain ¶
IBackendConstrain defines the type constraint for cache backend implementations. It restricts the generic type parameter to supported backend types, ensuring type safety and proper implementation at compile time.
type IFilter ¶
type IFilter interface {
ApplyFilter(backendType string, items []*cache.Item) ([]*cache.Item, error)
}
IFilter is a backend agnostic interface for a filter that can be applied to a list of items.
func WithFilterFunc ¶
WithFilterFunc returns a filter that filters the items by a given field's value.
func WithSortBy ¶
WithSortBy returns a filter that sorts the items by a given field.
type InMemory ¶
type InMemory struct { sync.RWMutex // mutex to protect the cache from concurrent access // contains filtered or unexported fields }
InMemory is a cache backend that stores the items in memory, leveraging a custom `ConcurrentMap`.
func (*InMemory) Get ¶
Get retrieves the item with the given key from the cacheBackend. If the item is not found, it returns nil.
func (*InMemory) List ¶
List returns a list of all items in the cache filtered and ordered by the given options.
func (*InMemory) Remove ¶
Remove removes items with the given key from the cacheBackend. If an item is not found, it does nothing.
func (*InMemory) SetCapacity ¶
SetCapacity sets the capacity of the cache.
type Option ¶
type Option[T IBackendConstrain] func(*T)
Option is a function type that can be used to configure the `HyperCache` struct.
func WithCapacity ¶
func WithCapacity[T IBackendConstrain](capacity int) Option[T]
WithCapacity is an option that sets the capacity of the cache.
func WithKeysSetName ¶
WithKeysSetName is an option that sets the name of the set that holds the keys of the items in the cache.
func WithRedisClient ¶
WithRedisClient is an option that sets the redis client to use.
func WithSerializer ¶
func WithSerializer[T Redis](serializer serializer.ISerializer) Option[Redis]
WithSerializer is an option that sets the serializer to use. The serializer is used to serialize and deserialize the items in the cache.
- The default serializer is `serializer.MsgpackSerializer`.
- The `serializer.JSONSerializer` can be used to serialize and deserialize the items in the cache as JSON.
- The interface `serializer.ISerializer` can be implemented to use a custom serializer.
type Redis ¶
type Redis struct { Serializer serializer.ISerializer // Serializer is the serializer used to serialize the items before storing them in the cache // contains filtered or unexported fields }
Redis is a cache backend that stores the items in a redis implementation.
func (*Redis) Capacity ¶
Capacity returns the maximum number of items that can be stored in the cache.
func (*Redis) Get ¶
Get retrieves the Item with the given key from the cacheBackend. If the item is not found, it returns nil.
func (*Redis) List ¶
List returns a list of all the items in the cacheBackend that match the given filter options.
func (*Redis) SetCapacity ¶
SetCapacity sets the capacity of the cache.
type SortOrderFilter ¶
type SortOrderFilter struct {
// contains filtered or unexported fields
}
SortOrderFilter is a filter that sorts the items by a given field.
func WithSortOrderAsc ¶
func WithSortOrderAsc(ascending bool) SortOrderFilter
WithSortOrderAsc returns a filter that determines whether to sort ascending or not.
func (SortOrderFilter) ApplyFilter ¶
ApplyFilter applies the sort order filter to the given list of items.