storage

package
v0.0.0-...-1571e8c Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package storage defines redis storage.

Index

Constants

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

Defines algorithm constant.

View Source
const B64JSONPrefix = "ey"

B64JSONPrefix stand for `{"` in base64.

Variables

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 ConnectToRedis

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

ConnectToRedis starts a goroutine 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 testing 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

func TokenHashAlgo

func TokenHashAlgo(token string) string

TokenHashAlgo ...

func TokenOrg

func TokenOrg(token string) string

TokenOrg ...

Types

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
	Decrement(string)
	IncrementWithExpire(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(key, value string)

AddToSet add value to key set.

func (*RedisCluster) AddToStoredSet

func (r *RedisCluster) AddToStoredSet(key, value string, score float64)

AddToStoredSet adds value with given score to sorted set identified by key.

func (*RedisCluster) AppendToSet

func (r *RedisCluster) AppendToSet(key, 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(key 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(key 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(key string) bool

DeleteRawKey will remove 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(key string) (bool, error)

Exists check if key exists.

func (*RedisCluster) GetAndDeleteSet

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

GetAndDeleteSet get and delete a key.

func (*RedisCluster) GetExp

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

GetExp return the expiry of the given key.

func (*RedisCluster) GetKey

func (r *RedisCluster) GetKey(key 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) GetKeyTLS

func (r *RedisCluster) GetKeyTLS(key string) (ttl int64, err error)

GetKeyTLS 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(key string, from, to int64) ([]string, error)

GetListRange gets range of elements of list identified by key.

func (*RedisCluster) GetMultiKey

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

func (*RedisCluster) GetRawKey

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

GetRawKey return the value of given key.

func (*RedisCluster) GetRollingWindow

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

GetRollingWindow return rolling window.

func (*RedisCluster) GetSet

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

GetSet return key set value.

func (*RedisCluster) GetSortedSetRange

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

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

func (*RedisCluster) IncrementWithExpire

func (r *RedisCluster) IncrementWithExpire(key string, expire int64) int64

IncrementWithExpire will increment a key in redis.

func (*RedisCluster) IsMemberOfSet

func (r *RedisCluster) IsMemberOfSet(key, 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 a message to the specify channel.

func (*RedisCluster) RemoveFromList

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

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

func (*RedisCluster) RemoveFromSet

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

RemoveFromSet remove a value from key set.

func (*RedisCluster) RemoveSortedSetRange

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

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

func (*RedisCluster) SetExp

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

SetExp set expiry of the given key.

func (*RedisCluster) SetKey

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

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

func (*RedisCluster) SetRawKey

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

SetRawKey set the value of the given key.

func (*RedisCluster) SetRollingWindow

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

Jump to

Keyboard shortcuts

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