cache

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cache defines caching interaction functionalities.

Index

Constants

View Source
const (
	// ID defines the id to be used as the container
	// registration id of a cache pool instance, as a base id of all other
	// cache package instances registered in the application container.
	ID = slate.ID + ".cache"

	// StoreStrategyTag defines the tag to be assigned to all
	// container store strategies.
	StoreStrategyTag = ID + ".store.strategy"

	// InMemoryStrategyID defines the id to be used as
	// the container registration id of an in-memory store factory
	// strategy instance.
	InMemoryStrategyID = ID + ".store.strategy.in_memory"

	// MemcachedStrategyID defines the id to be used as
	// the container registration id of a memcached service store factory
	// strategy instance.
	MemcachedStrategyID = ID + ".store.strategy.memcached"

	// BinaryMemcachedStrategyID defines the id to be used as
	// the container registration id of a binary connection memcached
	// service store factory strategy instance.
	BinaryMemcachedStrategyID = ID + ".store.strategy.binary_memcached"

	// RedisStrategyID defines the id to be used as
	// the container registration id of a redis
	// service store factory strategy instance.
	RedisStrategyID = ID + ".store.strategy.redis"

	// StoreFactoryID defines the id to be used as
	//	// the container registration id of a store factory instance.
	StoreFactoryID = ID + ".store.factory"
)
View Source
const (
	// DEFAULT @todo doc
	DEFAULT = time.Duration(0)

	// FOREVER @todo doc
	FOREVER = time.Duration(-1)
)
View Source
const (
	// EnvID defines the slate.api.cache package base environment variable name.
	EnvID = slate.EnvID + "_CACHE"
)
View Source
const (
	// InMemoryStoreType defines the value to be used to
	// declare an in-memory store type.
	InMemoryStoreType = "in-memory"
)
View Source
const (
	// UnknownStoreType defines the value to be used to
	// declare an unknown store type.
	UnknownStoreType = "unknown"
)

Variables

View Source
var (
	// StoresConfigPath contains the configuration path that holds the
	// cache stores connection configurations.
	StoresConfigPath = env.String(EnvID+"_STORES_CONFIG_PATH", "slate.api.cache.stores")

	// ObserveConfig defines the store pool cfg observing flag
	// used to register in the cfg object an observer of the store
	// cfg entries list, so it can reset the stores pool.
	ObserveConfig = env.Bool(EnvID+"_OBSERVE_CONFIG", true)

	// DefaultExpiration @todo doc.
	DefaultExpiration = env.Int(EnvID+"_DEFAULT_EXPIRATION", 60000)
)
View Source
var (
	// ErrConfigNotFound defines an error that signal that the
	// configuration to the requested store was not found.
	ErrConfigNotFound = fmt.Errorf("cache store config not found")

	// ErrInvalidStore defines an error that signal that the
	// given cache configuration was unable to be parsed correctly.
	ErrInvalidStore = fmt.Errorf("invalid cache store config")

	// ErrMiss @todo doc
	ErrMiss = fmt.Errorf("cache key not found")

	// ErrNotStored @todo doc
	ErrNotStored = fmt.Errorf("cache element not stored")
)

Functions

This section is empty.

Types

type IStore

type IStore interface {
	// Get retrieves an item from the cache. Returns the item or nil, and a
	// bool indicating whether the key was found.
	Get(key string, value interface{}) error

	// Set sets an item to the cache, replacing any existing item.
	Set(key string, value interface{}, expire time.Duration) error

	// Add adds an item to the cache only if an item doesn't already exist
	// for the given key, or if the existing item has expired. Returns
	// an error otherwise.
	Add(key string, value interface{}, expire time.Duration) error

	// Replace sets a new value for the cache key only if it already exists.
	// Returns an error if it does not.
	Replace(key string, data interface{}, expire time.Duration) error

	// Delete removes an item from the cache. Does nothing if the key
	// is not in the cache.
	Delete(key string) error

	// Increment increments a real number, and returns error if the value
	// is not real
	Increment(key string, data uint64) (uint64, error)

	// Decrement decrements a real number, and returns error if the value
	// is not real
	Decrement(key string, data uint64) (uint64, error)

	// Flush sets all items from the cache.
	Flush() error
}

IStore is the interface of a cache backend layer.

type IStoreFactory

type IStoreFactory interface {
	Register(strategy IStoreStrategy) error
	Create(cfg config.IConfig) (IStore, error)
}

IStoreFactory defined the interface of a store factory instance.

func NewStoreFactory

func NewStoreFactory() IStoreFactory

NewStoreFactory @todo doc

type IStorePool

type IStorePool interface {
	Get(name string) (IStore, error)
}

IStorePool defines the interface of a store pool instance.

func NewStorePool

func NewStorePool(
	cfg config.IManager,
	factory IStoreFactory,
) (IStorePool, error)

NewStorePool will instantiate a new relational database store pool instance.

type IStoreStrategy

type IStoreStrategy interface {
	Accept(cfg config.IConfig) bool
	Create(cfg config.IConfig) (IStore, error)
}

IStoreStrategy @todo doc

type InMemoryStore

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

InMemoryStore represents the cache with memory persistence

func NewInMemoryStore

func NewInMemoryStore(
	defaultExpiration time.Duration,
) *InMemoryStore

NewInMemoryStore returns a InMemoryStore

func (*InMemoryStore) Add

func (c *InMemoryStore) Add(
	key string,
	value interface{},
	expire time.Duration,
) error

Add (see IStore interface)

func (*InMemoryStore) Decrement

func (c *InMemoryStore) Decrement(
	key string,
	n uint64,
) (uint64, error)

Decrement (see IStore interface)

func (*InMemoryStore) Delete

func (c *InMemoryStore) Delete(
	key string,
) error

Delete (see IStore interface)

func (*InMemoryStore) Flush

func (c *InMemoryStore) Flush() error

Flush (see IStore interface)

func (*InMemoryStore) Get

func (c *InMemoryStore) Get(
	key string,
	value interface{},
) error

Get (see IStore interface)

func (*InMemoryStore) Increment

func (c *InMemoryStore) Increment(
	key string,
	n uint64,
) (uint64, error)

Increment (see IStore interface)

func (*InMemoryStore) Replace

func (c *InMemoryStore) Replace(
	key string,
	value interface{},
	expire time.Duration,
) error

Replace (see IStore interface)

func (*InMemoryStore) Set

func (c *InMemoryStore) Set(
	key string,
	value interface{},
	expire time.Duration,
) error

Set (see IStore interface)

type InMemoryStoreStrategy

type InMemoryStoreStrategy struct{}

InMemoryStoreStrategy @todo doc

func NewInMemoryStoreStrategy

func NewInMemoryStoreStrategy() *InMemoryStoreStrategy

NewInMemoryStoreStrategy @todo doc

func (InMemoryStoreStrategy) Accept

func (InMemoryStoreStrategy) Accept(
	cfg config.IConfig,
) bool

Accept @todo doc

func (InMemoryStoreStrategy) Create

func (InMemoryStoreStrategy) Create(
	cfg config.IConfig,
) (IStore, error)

Create @todo doc

type Provider

type Provider struct{}

Provider defines the slate.cache module service provider to be used on the application initialization to register the caching services.

func (Provider) Boot

func (p Provider) Boot(
	container ...slate.IContainer,
) error

Boot will start the cache package.

func (Provider) Register

func (p Provider) Register(
	container ...slate.IContainer,
) error

Register will register the cache package instances in the application container.

type StoreFactory

type StoreFactory []IStoreStrategy

StoreFactory is a persistence store generator based on a registered list of store generation strategies.

func (StoreFactory) Create

func (f StoreFactory) Create(
	cfg config.IConfig,
) (IStore, error)

Create will instantiate and return a new store loaded by a configuration instance.

func (*StoreFactory) Register

func (f *StoreFactory) Register(
	strategy IStoreStrategy,
) error

Register will register a new store factory strategy to be used on creation requests.

Jump to

Keyboard shortcuts

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