redis

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//nolint:gosec
	RedisClusterPassordEnvFileSuffix = "REDIS_STORE_PASSWORD_FILENAME"
	RedisClusterSizeEnvSuffix        = "REDIS_CLUSTER_SIZE"
	RedisNamespaceEnvSuffix          = "REDIS_KEY_NAMESPACE"
	RedisNodeAddressFmtSuffix        = "REDIS_NODE%d_STORE_ADDRESS"

	RedisNodeAddressSuffix = "REDIS_STORE_ADDRESS"
	RedisDBSuffix          = "REDIS_STORE_DB"
	RedisPasswordSuffix    = "AZURE_REDIS_STORE_PASSWORD_FILENAME"
)

Variables

View Source
var (
	ErrNoTenantID           = errors.New("no tenant ID supplied")
	ErrRedisClose           = errors.New("redis close error")
	ErrRedisConnect         = errors.New("redis connect error")
	ErrRedisCounterDisabled = errors.New("redis counter disabled")
	ErrRedisDial            = errors.New("redis dial error")
	ErrRedisDo              = errors.New("redis do error")
	ErrRedisSend            = errors.New("redis send error")
)
View Source
var (
	ErrUnknownRedisOperation = errors.New("unknown redis operation")
)

Functions

func CloseError

func CloseError(err error, name string) error

func ConnectError

func ConnectError(err error, name string) error

func DialError

func DialError(err error, name string) error

func DisabledError

func DisabledError(err error, name string) error

func DoError

func DoError(err error, name string) error

func NoTenantIDError

func NoTenantIDError(err error, name string) error

func SendError

func SendError(err error, name string) error

Types

type CachedReader

type CachedReader func(results any) error

type Client

type Client interface {
	// Do executes a Do command to redis
	Do(ctx context.Context, args ...any) *redis.Cmd
	Set(ctx context.Context, key string, value any, expiration time.Duration) *redis.StatusCmd
	SetNX(ctx context.Context, key string, value any, expiration time.Duration) *redis.BoolCmd
}

defines a redis client

type ClientContext

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

type CountRefresh

type CountRefresh = func(context.Context, string) (int64, error)

type CountResource

type CountResource struct {
	*Resource
	// contains filtered or unexported fields
}

CountResource - the counter is initialised with a maximum value and as resources are created the counter is decremented. Additionally when the counter reaches zero the actual number of resources is counted and the current count adjusted accordingly.

func NewResource

func NewResource(
	cfg RedisConfig,
	name string,
	opt CountResourceOption,
	opts ...ResourceOption,
) *CountResource

NewResource - creates pool of connections to redis that manages a decrementing counter. If limit is less than zero then the counter is disabled and all methods are noops.

func NewResourceWithMockedRedis

func NewResourceWithMockedRedis(
	name string,
	opt CountResourceOption,
	opts ...ResourceOption,
) (*CountResource, *mockClient)

func (*CountResource) Available

func (cr *CountResource) Available(ctx context.Context, tenantID string) (int64, error)

Available gets current value of counter and adjusts limit if required. An error is returned when either the redis counter cannot be read or this counter is disabled.

func (*CountResource) Consume

func (cr *CountResource) Consume(ctx context.Context, tenantID string) error

Consume subtracts 1 from counter

func (*CountResource) GetLimit added in v0.13.5

func (cr *CountResource) GetLimit() int64

GetLimit gets the current limit

func (*CountResource) Limited

func (cr *CountResource) Limited(ctx context.Context, tenantID string) bool

Limited returns true if the limit counter is enabled. The current limit is retrieved from upstream if necessary using the Limiter method. This happens when the current TTL parameters are exceeded.

func (*CountResource) Name

func (cr *CountResource) Name() string

func (*CountResource) ReadOnlyAvailable

func (cr *CountResource) ReadOnlyAvailable(ctx context.Context, tenantID string) (int64, error)

ReadOnlyAvailable

Returns the number of available units of this resource. Attempts the fast path first, of getting from the cache directly. Will attempt to re-initialise the count if its inaccessible or 0.

func (*CountResource) Return

func (cr *CountResource) Return(ctx context.Context, tenantID string) error

Return adds 1 to counter

type CountResourceOption

type CountResourceOption func(*CountResource)

func WithResourceCounter

func WithResourceCounter(resourceCounter ResourceCounter) CountResourceOption

type GobSerialiser

type GobSerialiser struct{}

Gob serialiser

func (GobSerialiser) Deserialise

func (gs GobSerialiser) Deserialise(d []byte, s *sessions.Session) error

func (GobSerialiser) Serialise

func (gs GobSerialiser) Serialise(s *sessions.Session) ([]byte, error)

type GorillaStore

type GorillaStore struct {
	// Client to connect to redis
	Client RedisClient

	Codecs []securecookie.Codec
	// default Options to use when a new session is created
	Options   sessions.Options
	MaxLength int
	// contains filtered or unexported fields
}

GorillaStore stores gorilla sessions in Redis

func NewRedisStore

func NewRedisStore(cfg RedisConfig, keyPairs ...[]byte) (*GorillaStore, error)

NewRedisStore returns a new RedisStore with default configuration

func (*GorillaStore) Close

func (s *GorillaStore) Close() error

Close closes the Redis store

func (*GorillaStore) Get

func (s *GorillaStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get returns a session for the given name after adding it to the registry.

func (*GorillaStore) KeyGen

func (s *GorillaStore) KeyGen(f KeyGenFunc)

KeyGen sets the key generator function

func (*GorillaStore) KeyPrefix

func (s *GorillaStore) KeyPrefix(keyPrefix string)

KeyPrefix sets the key prefix to store session in Redis

func (*GorillaStore) New

func (s *GorillaStore) New(r *http.Request, name string) (*sessions.Session, error)

New returns a session for the given name without adding it to the registry.

func (*GorillaStore) Save

func (s *GorillaStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error

Save adds a single session to the response.

If the Options.MaxAge of the session is <= 0 then the session file will be deleted from the store. With this process it enforces the properly session cookie handling so no need to trust in the cookie management in the web browser.

func (*GorillaStore) Serialiser

func (s *GorillaStore) Serialiser(ss SessionSerialiser)

Serialiser sets the session serialiser to store session

type HashCache

type HashCache struct {
	Cfg RedisConfig
	// contains filtered or unexported fields
}

HashCache uses redis optimistic locking and hash store to cache strings safely and prevents race conditions between Get/Set and Delete https://redis.io/topics/transactions#optimistic-locking-using-check-and-set

func NewHashCache

func NewHashCache(cfg RedisConfig, expiryTimeoutSeconds int64) (*HashCache, error)

func (*HashCache) CachedRead

func (c *HashCache) CachedRead(ctx context.Context, results any, name, field string, reader CachedReader) error

CachedRead returns the results from the cache if available. Otherwise the reader is invoked and its results are returned instead. The results from the reader, if invoked, are cached. This process is transactional. if the (name, field) is modified on another connection during the span from read to set, the set is not applied.

A note on err handling:

  • If we fail to read the value from the cache (for any reason) the reader callback is invoked once (and only once) regardless of any other error.
  • if the reader returns an err that is *always* returned to the caller. this is important as otherwise there is no way to distinguish between a failed read and an empty response from the cache or the db
  • errors writing to or fetching from the cache are treated as transient and are logged but not returned (persistent cache read/write errors will surface as performance issues or log spam)
  • errors marshalling or unmarshaling values for the cache are returned to the caller provided a reader error has not occurred.

func (*HashCache) Close

func (c *HashCache) Close() error

func (*HashCache) Delete

func (c *HashCache) Delete(ctx context.Context, name string) error

type JsonResource added in v0.13.9

type JsonResource struct {
	ClientContext
	// contains filtered or unexported fields
}

JsonResource is a Redis resource holding an object in JSON

func NewJsonResource added in v0.13.9

func NewJsonResource(name string, cfg RedisConfig, resType string) (*JsonResource, error)

NewJsonResource constructs a new instance of JsonResource, given the configuration.

  • name: name of the resource
  • resType: the general type of resource, mostly to help with organisation
  • cfg: the Redis cluster configuration to use

func (*JsonResource) Get added in v0.13.9

func (r *JsonResource) Get(ctx context.Context, tenantID string, target any) error

Get reads the resource for the given tenantID, and unmarshals it into target (which must be a pointer to a suitable empty struct.)

func (*JsonResource) Key added in v0.13.9

func (r *JsonResource) Key(tenantID string) string

Key gets the full resource identification key

func (*JsonResource) Name added in v0.13.9

func (r *JsonResource) Name() string

Name gets the name of the resource

func (*JsonResource) Set added in v0.13.9

func (r *JsonResource) Set(ctx context.Context, tenantID string, value any) error

Set takes a JSON-serializable value, marshals it, and stores it for the given tenantID

func (*JsonResource) URL added in v0.13.9

func (r *JsonResource) URL() string

URL gets the configured Redis URL

type KeyGenFunc

type KeyGenFunc func() (string, error)

KeyGenFunc defines a function used by store to generate a key

type Monotonic

type Monotonic struct {
	ClientContext
	// contains filtered or unexported fields
}

func NewMonotonic

func NewMonotonic(
	cfg RedisConfig,
	name string,
	opts ...MonotonicOption,
) Monotonic

func (*Monotonic) Del

func (c *Monotonic) Del(ctx context.Context, tenantIDOrWallet string) error

func (*Monotonic) IncrN

func (c *Monotonic) IncrN(ctx context.Context, tenantIDOrWallet string, n int64) (int64, error)

func (*Monotonic) Name

func (c *Monotonic) Name() string

func (*Monotonic) SetGT

func (c *Monotonic) SetGT(ctx context.Context, tenantIDOrWallet string, cas int64) (int64, error)

func (*Monotonic) SetRefresher

func (c *Monotonic) SetRefresher(refresh CountRefresh) CountRefresh

func (*Monotonic) URL

func (c *Monotonic) URL() string

type MonotonicOption

type MonotonicOption func(*Monotonic)

type RedisClient

type RedisClient interface {
	Do(ctx context.Context, args ...any) *redis.Cmd
	Del(ctx context.Context, keys ...string) *redis.IntCmd
	Ping(ctx context.Context) *redis.StatusCmd
	Set(ctx context.Context, key string, value any, expiration time.Duration) *redis.StatusCmd
	SetNX(ctx context.Context, key string, value any, expiration time.Duration) *redis.BoolCmd
	Get(ctx context.Context, key string) *redis.StringCmd
	Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) error
	Close() error
	Scripter
}

func NewRedisClient

func NewRedisClient(cfg RedisConfig) (RedisClient, error)

type RedisConfig

type RedisConfig interface {
	GetClusterOptions() (*redis.ClusterOptions, error)
	GetOptions() (*redis.Options, error)
	Namespace() string
	IsCluster() bool
	URL() string
}

func FromEnvOrFatal

func FromEnvOrFatal() RedisConfig

ReadClusterConfigOrFatal assumes conventional service env vars and populates a ClusterConfig or Fatals out

type Resource

type Resource struct {
	ClientContext
	// contains filtered or unexported fields
}

func (*Resource) Limited

func (r *Resource) Limited(ctx context.Context, tenantID string) bool

Limited returns true if the limit is enabled. The current limit is retrieved from upstream if necessary using the Limiter method.

func (*Resource) Name

func (r *Resource) Name() string

func (*Resource) URL

func (r *Resource) URL() string

type ResourceCounter

type ResourceCounter = func(context.Context) (int64, error)

ResourceCounter is a function which accepts a context and counts the number of reources in the database.

type ResourceLimiter

type ResourceLimiter = func(context.Context, *Resource, string) (int64, error)

ResourceLimiter is a function which accepts a resource and retrieves the latest caps limit setting from upstream.

type ResourceOption

type ResourceOption func(*Resource)

func WithRefreshCount

func WithRefreshCount(refreshCount int64) ResourceOption

func WithRefreshTTL

func WithRefreshTTL(refreshTTL time.Duration) ResourceOption

func WithResourceLimiter

func WithResourceLimiter(resourceLimiter ResourceLimiter) ResourceOption

type ScriptRunner

type ScriptRunner interface {
	Run(ctx context.Context, c redis.Scripter, keys []string, args ...any) *redis.Cmd
}

type Scripter

type Scripter interface {
	Eval(ctx context.Context, script string, keys []string, args ...any) *redis.Cmd
	EvalSha(ctx context.Context, sha1 string, keys []string, args ...any) *redis.Cmd
	ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd
	ScriptLoad(ctx context.Context, script string) *redis.StringCmd
}

type ScripterClient

type ScripterClient interface {
	Scripter
	SetNX(ctx context.Context, key string, value any, expiration time.Duration) *redis.BoolCmd
	Del(ctx context.Context, keys ...string) *redis.IntCmd
}

type SessionSerialiser

type SessionSerialiser interface {
	Serialise(s *sessions.Session) ([]byte, error)
	Deserialise(b []byte, s *sessions.Session) error
}

SessionSerialiser provides an interface for serialise/deserialise a session

type SizeResource

type SizeResource struct {
	*Resource
}

func NewSizeResource

func NewSizeResource(
	cfg RedisConfig,
	name string,
	opts ...ResourceOption,
) (*SizeResource, error)

If limit is less than zero then the limit is disabled and all methods are noops.

func (*SizeResource) Available

func (sr *SizeResource) Available(ctx context.Context, tenantID string) (int64, error)

Available gets current size limit.

Jump to

Keyboard shortcuts

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