Documentation
¶
Index ¶
- func ApplyFilterOptions[T IBackendConstrain](backend *T, options ...FilterOption[T])
- func ApplyOptions[T IBackendConstrain](backend *T, options ...Option[T])
- type FilterFunc
- type FilterOption
- type IBackend
- type IBackendConstrain
- type IFilterableBackend
- type IInMemory
- type IRedisBackend
- type InMemory
- func (cacheBackend *InMemory) Capacity() int
- func (cacheBackend *InMemory) Clear()
- func (cacheBackend *InMemory) Get(key string) (item *models.Item, ok bool)
- func (cacheBackend *InMemory) List(options ...FilterOption[InMemory]) ([]*models.Item, error)
- func (cacheBackend *InMemory) Remove(keys ...string) (err error)
- func (cacheBackend *InMemory) Set(item *models.Item) error
- func (cacheBackend *InMemory) SetCapacity(capacity int)
- func (cacheBackend *InMemory) Size() int
- type Option
- type Redis
- func (cacheBackend *Redis) Capacity() int
- func (cacheBackend *Redis) Clear() error
- func (cacheBackend *Redis) Get(key string) (item *models.Item, ok bool)
- func (cacheBackend *Redis) List(options ...FilterOption[Redis]) ([]*models.Item, error)
- func (cacheBackend *Redis) Remove(keys ...string) error
- func (cacheBackend *Redis) Set(item *models.Item) error
- func (cacheBackend *Redis) SetCapacity(capacity int)
- func (cacheBackend *Redis) Size() int
- type SortFilters
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyFilterOptions ¶
func ApplyFilterOptions[T IBackendConstrain](backend *T, options ...FilterOption[T])
ApplyFilterOptions applies the given options to the given filter.
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 FilterFunc ¶ added in v0.0.6
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.
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 WithSortBy ¶
func WithSortBy[T IBackendConstrain](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 WithSortOrderAsc ¶ added in v0.0.6
func WithSortOrderAsc[T IBackendConstrain](ascending bool) FilterOption[T]
WithSortOrderAsc is an option that sets the sort order to ascending or descending. When sorting the items in the cache, they will be sorted in ascending or 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. NewInMemory or NewRedisBackend.
type IBackendConstrain ¶
IBackendConstrain is the interface that defines the constrain type that must be implemented by cache backends.
type IFilterableBackend ¶ added in v0.0.6
type IFilterableBackend interface {
// contains filtered or unexported methods
}
IFilterableBackend is an interface that defines the methods that a backend should implement to be filterable.
type IInMemory ¶ added in v0.0.5
type IInMemory[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[InMemory]) ([]*models.Item, error) // Clear removes all items from the cache. Clear() }
IInMemory is the interface that must be implemented by in-memory cache backends.
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[Redis]) ([]*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 Redis](redisOptions ...Option[Redis]) (backend IRedisBackend[T], err error)
NewRedisBackend creates a new redis cache with the given options.
type InMemory ¶ added in v0.0.5
type InMemory struct { SortFilters // filters applied when listing the items in the cache // contains filtered or unexported fields }
InMemory is a cache backend that stores the items in memory, leveraging a custom `ConcurrentMap`.
func (*InMemory) Clear ¶ added in v0.0.5
func (cacheBackend *InMemory) Clear()
Clear removes all items from the cacheBackend.
func (*InMemory) Get ¶ added in v0.0.5
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
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
Remove removes items with the given key from the cacheBackend. If an item is not found, it does nothing.
func (*InMemory) SetCapacity ¶ added in v0.0.5
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
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 ¶ 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 { // mutex sync.RWMutex // mutex to protect the cache from concurrent access Serializer serializer.ISerializer // Serializer is the serializer used to serialize the items before storing them in the cache SortFilters // SortFilters holds the filters applied when listing the items 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
Capacity returns the maximum number of items that can be stored in the cache.
func (*Redis) Get ¶ added in v0.0.7
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
List returns a list of all the items in the cacheBackend that match the given filter options.
func (*Redis) SetCapacity ¶ added in v0.0.7
SetCapacity sets the capacity of 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