backend

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyOptions added in v0.0.5

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(key string) (item *types.Item, ok bool)
	// Set adds a new item to the cache.
	Set(item *types.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() 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 []*types.Item, err error)
	// Clear removes all items from the cache.
	Clear(ctx context.Context) error
}

IBackend is the interface that must be implemented by cache backends.

func NewInMemory added in v0.0.5

func NewInMemory(opts ...Option[InMemory]) (backend IBackend[InMemory], err error)

NewInMemory creates a new in-memory cache with the given options.

func NewRedis added in v0.1.3

func NewRedis(redisOptions ...Option[Redis]) (backend IBackend[Redis], err error)

NewRedis creates a new redis cache with the given options.

type IBackendConstrain

type IBackendConstrain interface {
	InMemory | Redis
}

IBackendConstrain is the interface that defines the constrain type implemented by cache backends.

type IFilter added in v0.1.3

type IFilter interface {
	ApplyFilter(backendType string, items []*types.Item) ([]*types.Item, error)
}

IFilter is a backend agnostic interface for a filter that can be applied to a list of items.

func WithFilterFunc

func WithFilterFunc(fn func(item *types.Item) bool) IFilter

WithFilterFunc returns a filter that filters the items by a given field's value.

func WithSortBy

func WithSortBy(field string) IFilter

WithSortBy returns a filter that sorts the items by a given field.

type InMemory added in v0.0.5

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) Capacity added in v0.0.5

func (cacheBackend *InMemory) Capacity() int

Capacity returns the capacity of the cacheBackend.

func (*InMemory) Clear added in v0.0.5

func (cacheBackend *InMemory) Clear(ctx context.Context) error

Clear removes all items from the cacheBackend.

func (*InMemory) Count added in v0.1.0

func (cacheBackend *InMemory) Count() int

Count returns the number of items in the cache.

func (*InMemory) Get added in v0.0.5

func (cacheBackend *InMemory) Get(key string) (item *types.Item, ok bool)

Get retrieves the item with the given key from the cacheBackend. If the item is not found, it returns nil.

func (*InMemory) List added in v0.0.5

func (cacheBackend *InMemory) List(ctx context.Context, filters ...IFilter) (items []*types.Item, err error)

List returns a list of all items in the cache filtered and ordered by the given options

func (*InMemory) Remove added in v0.0.5

func (cacheBackend *InMemory) Remove(ctx context.Context, keys ...string) (err error)

Remove removes items with the given key from the cacheBackend. If an item is not found, it does nothing.

func (*InMemory) Set added in v0.0.5

func (cacheBackend *InMemory) Set(item *types.Item) error

Set adds a Item to the cache.

func (*InMemory) SetCapacity added in v0.0.5

func (cacheBackend *InMemory) SetCapacity(capacity int)

SetCapacity sets the capacity of the cache.

type Option added in v0.0.5

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 added in v0.0.6

func WithKeysSetName[T Redis](keysSetName string) Option[Redis]

WithKeysSetName is an option that sets the name of the set that holds the keys of the items in the cache

func WithRedisClient

func WithRedisClient[T Redis](client *redis.Client) Option[Redis]

WithRedisClient is an option that sets the redis client to use.

func WithSerializer added in v0.0.6

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 added in v0.0.7

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 added in v0.0.7

func (cacheBackend *Redis) Capacity() int

Capacity returns the maximum number of items that can be stored in the cache.

func (*Redis) Clear added in v0.0.7

func (cacheBackend *Redis) Clear(ctx context.Context) error

Clear removes all items from the cache

func (*Redis) Count added in v0.1.0

func (cacheBackend *Redis) Count() int

Count returns the number of items in the cache.

func (*Redis) Get added in v0.0.7

func (cacheBackend *Redis) Get(key string) (item *types.Item, ok bool)

Get retrieves the Item with the given key from the cacheBackend. If the item is not found, it returns nil.

func (*Redis) List added in v0.0.7

func (cacheBackend *Redis) List(ctx context.Context, filters ...IFilter) (items []*types.Item, err error)

List returns a list of all the items in the cacheBackend that match the given filter options.

func (*Redis) Remove added in v0.0.7

func (cacheBackend *Redis) Remove(ctx context.Context, keys ...string) error

Remove removes an item from the cache with the given key

func (*Redis) Set added in v0.0.7

func (cacheBackend *Redis) Set(item *types.Item) error

Set stores the Item in the cacheBackend.

func (*Redis) SetCapacity added in v0.0.7

func (cacheBackend *Redis) SetCapacity(capacity int)

SetCapacity sets the capacity of the cache.

type SortOrderFilter added in v0.1.3

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

SortOrderFilter is a filter that sorts the items by a given field.

func WithSortOrderAsc added in v0.0.6

func WithSortOrderAsc(ascending bool) SortOrderFilter

WithSortOrderAsc returns a filter that determines whether to sort ascending or not.

func (SortOrderFilter) ApplyFilter added in v0.1.3

func (f SortOrderFilter) ApplyFilter(backendType string, items []*types.Item) ([]*types.Item, error)

ApplyFilter applies the sort order filter to the given list of items.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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