cache

package
v0.0.0-...-6fe5f88 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package storage defines redis storage.

Index

Constants

View Source
const B64JSONPrefix = "ey"

B64JSONPrefix stand for `{"` in base64.

Variables

View Source
var (
	HashSha256    = "sha256"
	HashMurmur32  = "murmur32"
	HashMurmur64  = "murmur64"
	HashMurmur128 = "murmur128"
)

Defines algorithm constant.

View Source
var ErrKeyNotFound = errors.New("key not found")

ErrKeyNotFound is a standard error for when a key is not found in the storage engine.

View Source
var ErrRedisIsDown = errors.New("storage: Redis is either down or ws not configured")

ErrRedisIsDown is returned when we can't communicate with redis.

Functions

func ConnectToRedisV2

func ConnectToRedisV2(ctx context.Context, config *Config)

func Connected

func Connected() bool

Connected returns true if we are connected to redis.

func DisableRedis

func DisableRedis(ok bool)

DisableRedis very handy when testsing it allows to dynamically enable/disable talking with redisW.

func GenerateToken

func GenerateToken(orgID, keyID, hashAlgorithm string) (string, error)

GenerateToken generate token, if hashing algorithm is empty, use legacy key generation.

func HashKey

func HashKey(in string) string

HashKey return hash the give string and return.

func HashStr

func HashStr(in string) string

HashStr return hash the give string and return.

func NewRedisClusterPoolV2

func NewRedisClusterPoolV2(isCache bool, config *Config) redis.UniversalClient

NewRedisClusterPool create a redis cluster pool.

func TokenHashAlgo

func TokenHashAlgo(token string) string

TokenHashAlgo ...

func TokenOrg

func TokenOrg(token string) string

TokenOrg ...

Types

type Config

type Config struct {
	Host                  string
	Port                  int
	Addrs                 []string
	MasterName            string
	Username              string
	Password              string
	Database              int
	MaxIdle               int
	MaxActive             int
	Timeout               int
	EnableCluster         bool
	UseSSL                bool
	SSLInsecureSkipVerify bool
}

Config defines options for redis cluster.

type Handler

type Handler interface {
	GetKey(ctx context.Context, key string) (string, error)
	GetMultiKey(ctx context.Context, keys []string) ([]string, error)
	GetRawKey(ctx context.Context, key string) (string, error)
	SetKey(ctx context.Context, key, value string, ttl int64) error
	SetRawKey(ctx context.Context, key, value string, ttl int64) error
	SetExp(ctx context.Context, key string, ttl int64) error
	GetExp(ctx context.Context, key string) (int64, error)
	GetKeys(ctx context.Context, pattern string) []string
	DeleteKey(ctx context.Context, key string) bool
	DeleteAllKeys(ctx context.Context) bool
	DeleteRawKey(ctx context.Context, key string) bool
	Connect() bool
	GetKeysAndValues(ctx context.Context) map[string]string
	GetKeysAndValuesWithFilter(ctx context.Context, pattern string) map[string]string
	DeleteKeys(ctx context.Context, keys []string) bool
	Decrement(ctx context.Context, key string)
	IncrememntWithExpire(ctx context.Context, key string, ttl int64) int64
	SetRollingWindow(ctx context.Context, key string, per int64, val string, pipeline bool) (int, []interface{})
	GetRollingWindow(ctx context.Context, key string, per int64, pipeline bool) (int, []interface{})
	GetSet(ctx context.Context, key string) (map[string]string, error)
	AddToSet(ctx context.Context, key, value string)
	GetAndDeleteSet(ctx context.Context, key string) []interface{}
	RemoveFromSet(ctx context.Context, key, value string)
	DeleteScanMatch(ctx context.Context, pattern string) bool
	GetKeyPrefix() string
	AddToSortedSet(ctx context.Context, key, value string, score float64)
	GetSortedSetRange(ctx context.Context, key, min, max string) ([]string, []float64, error)
	RemoveSortedSetRange(ctx context.Context, key, min, max string) error
	GetListRange(ctx context.Context, key string, start, stop int64) ([]string, error)
	RemoveFromList(ctx context.Context, key, value string) error
	AppendToSet(ctx context.Context, key, value string)
	Exists(ctx context.Context, key string) (bool, error)
}

Handler is a standard interface to a cache backend

type RedisClusterV2

type RedisClusterV2 struct {
	KeyPrefix string
	HashKeys  bool
	IsCache   bool
}

func (*RedisClusterV2) AddToSet

func (r *RedisClusterV2) AddToSet(ctx context.Context, keyName, value string)

AddToSet add value to key set.

func (*RedisClusterV2) AddToSortedSet

func (r *RedisClusterV2) AddToSortedSet(ctx context.Context, keyName, value string, score float64)

AddToSortedSet adds value with given score to sorted set identified by keyName.

func (*RedisClusterV2) AppendToSet

func (r *RedisClusterV2) AppendToSet(ctx context.Context, keyName, value string)

AppendToSet append a value to the key set.

func (*RedisClusterV2) AppendToSetPipelined

func (r *RedisClusterV2) AppendToSetPipelined(ctx context.Context, key string, values [][]byte)

AppendToSetPipelined append values to redis pipeline.

func (*RedisClusterV2) Close

func (r *RedisClusterV2) Close() error

func (*RedisClusterV2) Connect

func (r *RedisClusterV2) Connect() bool

Connect will establish a connection this is always true because we are dynamically using redis.

func (*RedisClusterV2) Decrement

func (r *RedisClusterV2) Decrement(ctx context.Context, keyName string)

Decrement will decrement a key in redis.

func (*RedisClusterV2) DeleteAllKeys

func (r *RedisClusterV2) DeleteAllKeys(ctx context.Context) bool

DeleteAllKeys will remove all keys from the database.

func (*RedisClusterV2) DeleteKey

func (r *RedisClusterV2) DeleteKey(ctx context.Context, keyName string) bool

DeleteKey will remove a key from the database.

func (*RedisClusterV2) DeleteKeys

func (r *RedisClusterV2) DeleteKeys(ctx context.Context, keys []string) bool

DeleteKeys will remove a group of keys in bulk.

func (*RedisClusterV2) DeleteRawKey

func (r *RedisClusterV2) DeleteRawKey(ctx context.Context, keyName string) bool

DeleteRawKey will remove a key from the database without prefixing, assumes user knows what they are doing.

func (*RedisClusterV2) DeleteScanMatch

func (r *RedisClusterV2) DeleteScanMatch(ctx context.Context, pattern string) bool

DeleteScanMatch will remove a group of keys in bulk.

func (*RedisClusterV2) Exists

func (r *RedisClusterV2) Exists(ctx context.Context, keyName string) (bool, error)

Exists check if keyName exists.

func (*RedisClusterV2) GetAndDeleteSet

func (r *RedisClusterV2) GetAndDeleteSet(ctx context.Context, keyName string) []interface{}

GetAndDeleteSet get and delete a key.

func (*RedisClusterV2) GetExp

func (r *RedisClusterV2) GetExp(ctx context.Context, keyName string) (int64, error)

GetExp return the expiry of the given key.

func (*RedisClusterV2) GetKey

func (r *RedisClusterV2) GetKey(ctx context.Context, keyName string) (string, error)

GetKey will retrieve a key from the database.

func (*RedisClusterV2) GetKeyPrefix

func (r *RedisClusterV2) GetKeyPrefix() string

GetKeyPrefix returns storage key prefix.

func (*RedisClusterV2) GetKeyTTL

func (r *RedisClusterV2) GetKeyTTL(ctx context.Context, keyName string) (ttl int64, err error)

GetKeyTTL return ttl of the given key.

func (*RedisClusterV2) GetKeys

func (r *RedisClusterV2) GetKeys(ctx context.Context, filter string) []string

GetKeys will return all keys according to the filter (filter is a prefix - e.g. tyk.keys.*).

func (*RedisClusterV2) GetKeysAndValues

func (r *RedisClusterV2) GetKeysAndValues(ctx context.Context) map[string]string

GetKeysAndValues will return all keys and their values - not to be used lightly.

func (*RedisClusterV2) GetKeysAndValuesWithFilter

func (r *RedisClusterV2) GetKeysAndValuesWithFilter(ctx context.Context, filter string) map[string]string

GetKeysAndValuesWithFilter will return all keys and their values with a filter.

func (*RedisClusterV2) GetListRange

func (r *RedisClusterV2) GetListRange(ctx context.Context, keyName string, from, to int64) ([]string, error)

GetListRange gets range of elements of list identified by keyName.

func (*RedisClusterV2) GetMultiKey

func (r *RedisClusterV2) GetMultiKey(ctx context.Context, keys []string) ([]string, error)

func (*RedisClusterV2) GetRawKey

func (r *RedisClusterV2) GetRawKey(ctx context.Context, keyName string) (string, error)

GetRawKey return the value of the given key.

func (RedisClusterV2) GetRollingWindow

func (r RedisClusterV2) GetRollingWindow(ctx context.Context, keyName string, per int64, pipeline bool) (int, []interface{})

GetRollingWindow return rolling window.

func (*RedisClusterV2) GetSet

func (r *RedisClusterV2) GetSet(ctx context.Context, keyName string) (map[string]string, error)

GetSet return key set value.

func (*RedisClusterV2) GetSortedSetRange

func (r *RedisClusterV2) GetSortedSetRange(ctx context.Context, keyName, scoreFrom, scoreTo string) ([]string, []float64, error)

GetSortedSetRange gets range of elements of sorted set identified by keyName.

func (*RedisClusterV2) IncrememntWithExpire

func (r *RedisClusterV2) IncrememntWithExpire(ctx context.Context, keyName string, expire int64) int64

IncrememntWithExpire will increment a key in redis.

func (*RedisClusterV2) IsMemberOfSet

func (r *RedisClusterV2) IsMemberOfSet(ctx context.Context, keyName, value string) bool

IsMemberOfSet return whether the given value belong to key set.

func (*RedisClusterV2) Publish

func (r *RedisClusterV2) Publish(ctx context.Context, channel, message string) error

Publish publish a message to the specify channel.

func (*RedisClusterV2) RemoveFromList

func (r *RedisClusterV2) RemoveFromList(ctx context.Context, keyName, value string) error

RemoveFromList delete a value from a list identified with the keyName.

func (*RedisClusterV2) RemoveFromSet

func (r *RedisClusterV2) RemoveFromSet(ctx context.Context, keyName, value string)

RemoveFromSet remove a value from key set.

func (*RedisClusterV2) RemoveSortedSetRange

func (r *RedisClusterV2) RemoveSortedSetRange(ctx context.Context, keyName, scoreFrom, scoreTo string) error

RemoveSortedSetRange removes range of elements from sorted set identified by keyName.

func (*RedisClusterV2) SetExp

func (r *RedisClusterV2) SetExp(ctx context.Context, keyName string, timeout time.Duration) error

SetExp set expiry of the given key.

func (*RedisClusterV2) SetKey

func (r *RedisClusterV2) SetKey(ctx context.Context, keyName, session string, timeout time.Duration) error

SetKey will create (or update) a key value in the store.

func (*RedisClusterV2) SetRawKey

func (r *RedisClusterV2) SetRawKey(ctx context.Context, keyName, session string, timeout time.Duration) error

SetRawKey set the value of the given key.

func (*RedisClusterV2) SetRollingWindow

func (r *RedisClusterV2) SetRollingWindow(
	ctx context.Context,
	keyName string,
	per int64,
	valueOverride string,
	pipeline bool,
) (int, []interface{})

SetRollingWindow will append to a sorted set in redis and extract a timed window of values.

func (*RedisClusterV2) StartPubSubHandler

func (r *RedisClusterV2) StartPubSubHandler(ctx context.Context, channel string, callback func(interface{})) error

StartPubSubHandler will listen for a signal and run the callback for every subscription and message event.

type RedisOptsV2

type RedisOptsV2 redis.UniversalOptions

RedisOpts is the overridden type of redis.UniversalOptions. simple() and cluster() functions are not public in redis library. Therefore, they are redefined here to use in the creation of a new redis cluster logic. We don't want to use redis.NewUniversalClient() logic.

Jump to

Keyboard shortcuts

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