v0.0.0-...-772bc38 Latest Latest

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

Go to latest
Published: May 8, 2023 License: MulanPSL-2.0 Imports: 19 Imported by: 0



Package storage defines redis storage.



View Source
const B64JSONPrefix = "ey"

B64JSONPrefix stand for `{"` in base64.


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.


func ConnectToRedis

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

ConnectToRedis starts a go routine that periodically tries to connect to redis.

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 NewRedisClusterPool

func NewRedisClusterPool(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 ...


type AnalyticsHandler

type AnalyticsHandler interface {
	Connect() bool
	AppendToSetPipelined(string, [][]byte)
	GetAndDeleteSet(string) []interface{}
	SetExp(string, time.Duration) error // Set key expiration
	GetExp(string) (int64, error)       // Returns expiry of a key

AnalyticsHandler defines the interface for analytics.

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(string) (string, error) // Returned string is expected to be a JSON object (user.SessionState)
	GetMultiKey([]string) ([]string, error)
	GetRawKey(string) (string, error)
	SetKey(string, string, int64) error // Second input string is expected to be a JSON object (user.SessionState)
	SetRawKey(string, string, int64) error
	SetExp(string, int64) error   // Set key expiration
	GetExp(string) (int64, error) // Returns expiry of a key
	GetKeys(string) []string
	DeleteKey(string) bool
	DeleteAllKeys() bool
	DeleteRawKey(string) bool
	Connect() bool
	GetKeysAndValues() map[string]string
	GetKeysAndValuesWithFilter(string) map[string]string
	DeleteKeys([]string) bool
	IncrememntWithExpire(string, int64) int64
	SetRollingWindow(key string, per int64, val string, pipeline bool) (int, []interface{})
	GetRollingWindow(key string, per int64, pipeline bool) (int, []interface{})
	GetSet(string) (map[string]string, error)
	AddToSet(string, string)
	GetAndDeleteSet(string) []interface{}
	RemoveFromSet(string, string)
	DeleteScanMatch(string) bool
	GetKeyPrefix() string
	AddToSortedSet(string, string, float64)
	GetSortedSetRange(string, string, string) ([]string, []float64, error)
	RemoveSortedSetRange(string, string, string) error
	GetListRange(string, int64, int64) ([]string, error)
	RemoveFromList(string, string) error
	AppendToSet(string, string)
	Exists(string) (bool, error)

Handler is a standard interface to a storage backend, used by AuthorisationManager to read and write key values to the backend.

type RedisCluster

type RedisCluster struct {
	KeyPrefix string
	HashKeys  bool
	IsCache   bool

RedisCluster is a storage manager that uses the redis database.

func (*RedisCluster) AddToSet

func (r *RedisCluster) AddToSet(keyName, value string)

AddToSet add value to key set.

func (*RedisCluster) AddToSortedSet

func (r *RedisCluster) AddToSortedSet(keyName, value string, score float64)

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

func (*RedisCluster) AppendToSet

func (r *RedisCluster) AppendToSet(keyName, value string)

AppendToSet append a value to the key set.

func (*RedisCluster) AppendToSetPipelined

func (r *RedisCluster) AppendToSetPipelined(key string, values [][]byte)

AppendToSetPipelined append values to redis pipeline.

func (*RedisCluster) Connect

func (r *RedisCluster) Connect() bool

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

func (*RedisCluster) Decrement

func (r *RedisCluster) Decrement(keyName string)

Decrement will decrement a key in redis.

func (*RedisCluster) DeleteAllKeys

func (r *RedisCluster) DeleteAllKeys() bool

DeleteAllKeys will remove all keys from the database.

func (*RedisCluster) DeleteKey

func (r *RedisCluster) DeleteKey(keyName string) bool

DeleteKey will remove a key from the database.

func (*RedisCluster) DeleteKeys

func (r *RedisCluster) DeleteKeys(keys []string) bool

DeleteKeys will remove a group of keys in bulk.

func (*RedisCluster) DeleteRawKey

func (r *RedisCluster) DeleteRawKey(keyName string) bool

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

func (*RedisCluster) DeleteScanMatch

func (r *RedisCluster) DeleteScanMatch(pattern string) bool

DeleteScanMatch will remove a group of keys in bulk.

func (*RedisCluster) Exists

func (r *RedisCluster) Exists(keyName string) (bool, error)

Exists check if keyName exists.

func (*RedisCluster) GetAndDeleteSet

func (r *RedisCluster) GetAndDeleteSet(keyName string) []interface{}

GetAndDeleteSet get and delete a key.

func (*RedisCluster) GetExp

func (r *RedisCluster) GetExp(keyName string) (int64, error)

GetExp return the expiry of the given key.

func (*RedisCluster) GetKey

func (r *RedisCluster) GetKey(keyName string) (string, error)

GetKey will retrieve a key from the database.

func (*RedisCluster) GetKeyPrefix

func (r *RedisCluster) GetKeyPrefix() string

GetKeyPrefix returns storage key prefix.

func (*RedisCluster) GetKeyTTL

func (r *RedisCluster) GetKeyTTL(keyName string) (ttl int64, err error)

GetKeyTTL return ttl of the given key.

func (*RedisCluster) GetKeys

func (r *RedisCluster) GetKeys(filter string) []string

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

func (*RedisCluster) GetKeysAndValues

func (r *RedisCluster) GetKeysAndValues() map[string]string

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

func (*RedisCluster) GetKeysAndValuesWithFilter

func (r *RedisCluster) GetKeysAndValuesWithFilter(filter string) map[string]string

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

func (*RedisCluster) GetListRange

func (r *RedisCluster) GetListRange(keyName string, from, to int64) ([]string, error)

GetListRange gets range of elements of list identified by keyName.

func (*RedisCluster) GetMultiKey

func (r *RedisCluster) GetMultiKey(keys []string) ([]string, error)

GetMultiKey gets multiple keys from the database.

func (*RedisCluster) GetRawKey

func (r *RedisCluster) GetRawKey(keyName string) (string, error)

GetRawKey return the value of the given key.

func (RedisCluster) GetRollingWindow

func (r RedisCluster) GetRollingWindow(keyName string, per int64, pipeline bool) (int, []interface{})

GetRollingWindow return rolling window.

func (*RedisCluster) GetSet

func (r *RedisCluster) GetSet(keyName string) (map[string]string, error)

GetSet return key set value.

func (*RedisCluster) GetSortedSetRange

func (r *RedisCluster) GetSortedSetRange(keyName, scoreFrom, scoreTo string) ([]string, []float64, error)

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

func (*RedisCluster) IncrememntWithExpire

func (r *RedisCluster) IncrememntWithExpire(keyName string, expire int64) int64

IncrememntWithExpire will increment a key in redis.

func (*RedisCluster) IsMemberOfSet

func (r *RedisCluster) IsMemberOfSet(keyName, value string) bool

IsMemberOfSet return whether the given value belong to key set.

func (*RedisCluster) Publish

func (r *RedisCluster) Publish(channel, message string) error

Publish publish a message to the specify channel.

func (*RedisCluster) RemoveFromList

func (r *RedisCluster) RemoveFromList(keyName, value string) error

RemoveFromList delete an value from a list idetinfied with the keyName.

func (*RedisCluster) RemoveFromSet

func (r *RedisCluster) RemoveFromSet(keyName, value string)

RemoveFromSet remove a value from key set.

func (*RedisCluster) RemoveSortedSetRange

func (r *RedisCluster) RemoveSortedSetRange(keyName, scoreFrom, scoreTo string) error

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

func (*RedisCluster) SetExp

func (r *RedisCluster) SetExp(keyName string, timeout time.Duration) error

SetExp set expiry of the given key.

func (*RedisCluster) SetKey

func (r *RedisCluster) SetKey(keyName, session string, timeout time.Duration) error

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

func (*RedisCluster) SetRawKey

func (r *RedisCluster) SetRawKey(keyName, session string, timeout time.Duration) error

SetRawKey set the value of the given key.

func (*RedisCluster) SetRollingWindow

func (r *RedisCluster) SetRollingWindow(
	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 (*RedisCluster) StartPubSubHandler

func (r *RedisCluster) StartPubSubHandler(channel string, callback func(interface{})) error

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

type RedisOpts

type RedisOpts redis.UniversalOptions

RedisOpts is the overridden type of redis.UniversalOptions. simple() and cluster() functions are not public in redis library. Therefore, they are redefined in here to use in creation of 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