backend

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2023 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyBackendOptions

func ApplyBackendOptions[T IBackendConstrain](backend *T, options ...BackendOption[T])

ApplyBackendOptions applies the given options to the given backend.

func ApplyFilterOptions

func ApplyFilterOptions[T any](backend *T, options ...FilterOption[T])

ApplyFilterOptions applies the given options to the given filter.

Types

type BackendOption

type BackendOption[T IBackendConstrain] func(*T)

BackendOption is a function type that can be used to configure the `HyperCache` struct.

func WithCapacity

func WithCapacity[T InMemoryBackend](capacity int) BackendOption[InMemoryBackend]

WithCapacity is an option that sets the capacity of the cache.

func WithRedisClient

func WithRedisClient[T RedisBackend](client *redis.Client) BackendOption[RedisBackend]

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

type FilterOption

type FilterOption[T any] func(*T)

FilterOption is a function type that can be used to configure the `Filter` struct.

func WithFilterFunc

func WithFilterFunc[T any](fn func(item *models.Item) bool) FilterOption[T]

WithFilterFunc is an option that sets the filter function to use. The filter function is a predicate that takes a `Item` as an argument and returns a boolean indicating whether the item should be included in the cache.

func WithSortAscending

func WithSortAscending[T any]() FilterOption[T]

WithSortAscending is an option that sets the sort order to ascending. When sorting the items in the cache, they will be sorted in ascending order based on the field specified with the `WithSortBy` option.

func WithSortBy

func WithSortBy[T any](field types.SortingField) FilterOption[T]

WithSortBy is an option that sets the field to sort the items by. The field can be any of the fields in the `Item` struct.

func WithSortDescending

func WithSortDescending[T any]() FilterOption[T]

WithSortDescending is an option that sets the sort order to descending. When sorting the items in the cache, they will be sorted in descending order based on the field specified with the `WithSortBy` option.

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 *models.Item, ok bool)
	// Set adds a new item to the cache.
	Set(item *models.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)
	// Size returns the number of items currently stored in the cache.
	Size() int
	// Remove deletes the item with the given key from the cache.
	Remove(keys ...string) error
}

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

func NewBackend

func NewBackend[T IBackendConstrain](backendType string, opts ...any) (IBackend[T], error)

NewBackend creates a new cache backend. Deprecated: Use specific backend constructors instead, e.g. NewInMemoryBackend or NewRedisBackend.

type IBackendConstrain

type IBackendConstrain interface {
	InMemoryBackend | RedisBackend
}

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

type IInMemoryBackend

type IInMemoryBackend[T IBackendConstrain] interface {
	// IBackend[T] is the interface that must be implemented by cache backends.
	IBackend[T]
	// List the items in the cache that meet the specified criteria.
	List(options ...FilterOption[InMemoryBackend]) ([]*models.Item, error)
	// Clear removes all items from the cache.
	Clear()
}

IInMemoryBackend is the interface that must be implemented by in-memory cache backends.

func NewInMemoryBackend

func NewInMemoryBackend[T InMemoryBackend](opts ...BackendOption[InMemoryBackend]) (backend IInMemoryBackend[T], err error)

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

type IRedisBackend

type IRedisBackend[T IBackendConstrain] interface {
	// IBackend[T] is the interface that must be implemented by cache backends.
	IBackend[T]
	// List the items in the cache that meet the specified criteria.
	List(options ...FilterOption[RedisBackend]) ([]*models.Item, error)
	// Clear removes all items from the cache.
	Clear() error
}

IRedisBackend is the interface that must be implemented by Redis cache backends.

func NewRedisBackend

func NewRedisBackend[T RedisBackend](redisOptions ...BackendOption[RedisBackend]) (backend IRedisBackend[T], err error)

NewRedisBackend creates a new redis cache with the given options.

type InMemoryBackend

type InMemoryBackend struct {
	SortFilters // filters applied when listing the items in the cache
	// contains filtered or unexported fields
}

InMemoryBackend is a cache backend that stores the items in memory, leveraging a custom `ConcurrentMap`.

func (*InMemoryBackend) Capacity

func (cacheBackend *InMemoryBackend) Capacity() int

Capacity returns the capacity of the cacheBackend.

func (*InMemoryBackend) Clear

func (cacheBackend *InMemoryBackend) Clear()

Clear removes all items from the cacheBackend.

func (*InMemoryBackend) Get

func (cacheBackend *InMemoryBackend) Get(key string) (item *models.Item, ok bool)

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

func (*InMemoryBackend) List

func (cacheBackend *InMemoryBackend) List(options ...FilterOption[InMemoryBackend]) ([]*models.Item, error)

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

func (*InMemoryBackend) Remove

func (cacheBackend *InMemoryBackend) Remove(keys ...string) (err error)

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

func (*InMemoryBackend) Set

func (cacheBackend *InMemoryBackend) Set(item *models.Item) error

Set adds a Item to the cache.

func (*InMemoryBackend) SetCapacity

func (cacheBackend *InMemoryBackend) SetCapacity(capacity int)

SetCapacity sets the capacity of the cache.

func (*InMemoryBackend) Size

func (cacheBackend *InMemoryBackend) Size() int

Size returns the number of items in the cacheBackend.

type RedisBackend

type RedisBackend struct {
	SortFilters // SortFilters holds the filters applied when listing the items in the cache
	// contains filtered or unexported fields
}

RedisBackend is a cache backend that stores the items in a redis implementation.

func (*RedisBackend) Capacity

func (cacheBackend *RedisBackend) Capacity() int

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

func (*RedisBackend) Clear

func (cacheBackend *RedisBackend) Clear() error

Clear removes all items from the cache

func (*RedisBackend) Get

func (cacheBackend *RedisBackend) Get(key string) (item *models.Item, ok bool)

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

func (*RedisBackend) List

func (cacheBackend *RedisBackend) List(options ...FilterOption[RedisBackend]) ([]*models.Item, error)

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

func (*RedisBackend) Remove

func (cacheBackend *RedisBackend) Remove(keys ...string) error

Remove removes an item from the cache with the given key

func (*RedisBackend) Set

func (cacheBackend *RedisBackend) Set(item *models.Item) error

Set stores the Item in the cacheBackend.

func (*RedisBackend) SetCapacity

func (cacheBackend *RedisBackend) SetCapacity(capacity int)

SetCapacity sets the capacity of the cache.

func (*RedisBackend) Size

func (cacheBackend *RedisBackend) Size() int

Size returns the number of items in the cache.

type SortFilters

type SortFilters struct {
	// SortBy is the field to sort the items by.
	// The field can be any of the fields in the `Item` struct.
	SortBy string
	// SortAscending is a boolean indicating whether the items should be sorted in ascending order.
	// If set to false, the items will be sorted in descending order.
	SortAscending bool
	// FilterFunc is a predicate that takes a `Item` as an argument and returns a boolean indicating whether the item should be included in the cache.
	FilterFunc func(item *models.Item) bool // filters applied when listing the items in the cache
}

SortFilters holds the filters applied when listing the items in the cache

Jump to

Keyboard shortcuts

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