redis

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 9 Imported by: 7

Documentation

Index

Constants

View Source
const (
	// DefaultExpiry is used when Mutex Duration is 0
	DefaultExpiry = 6 * time.Second
	// DefaultTries is used when Mutex Duration is 0
	DefaultTries = 16
	// DefaultDelay is used when Mutex Delay is 0
	DefaultDelay = 512 * time.Millisecond
	// DefaultFactor is used when Mutex Factor is 0
	DefaultFactor = 0.01
)

Variables

View Source
var (
	// ErrKeyNotSet returns if the key was not set properly
	ErrKeyNotSet = errors.New("key not set")
	// RedisKeyIndex holds a searchable index of keys
	RedisKeyIndex = make(map[string]Key)
	// RedisKeys is a slice of all the explicit keys
	RedisKeys = []Key{
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
		{/* contains filtered or unexported fields */},
	}
)
View Source
var (
	// ErrFailed is returned when lock cannot be acquired
	ErrFailed = errors.New("failed to acquire lock")
)

Functions

func NewRedisMock added in v1.3.0

func NewRedisMock()

NewRedisMock returns a fake redis pool for testing

Types

type Key added in v1.3.0

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

Key holds an explicit keys data

func NewKey

func NewKey(name string) *Key

NewKey returns a key from the index or nil if it doesnt exist

func (*Key) Delete added in v1.3.0

func (r *Key) Delete() (err error)

Delete deletes a key

func (*Key) Get added in v1.3.0

func (r *Key) Get() (result []byte, err error)

Get gets a key, automatically handles hash types

func (*Key) Set added in v1.3.0

func (r *Key) Set(data []byte) (err error)

Set sets a key, handles hash types and expiry

func (*Key) SetKey added in v1.3.0

func (r *Key) SetKey(ids ...string) *Key

SetKey populates the fields in a key and sets the hash id

func (*Key) String added in v1.3.0

func (r *Key) String() string

return a string version of the key

type Keyer added in v1.3.0

type Keyer interface {
	SetKey(ids ...string) *Key
	Get() (result []byte, err error)
	Set(data []byte) (err error)
	Delete() (err error)
	String() string
}

Keyer describes the explicit key functions

type Locker

type Locker interface {
	Lock(string) error
	Unlock(string) bool
}

Locker interface with Lock returning an error when lock cannot be aquired

type Mutex

type Mutex struct {
	Expiry time.Duration // Duration for which the lock is valid, DefaultExpiry if 0

	Tries int           // Number of attempts to acquire lock before admitting failure, DefaultTries if 0
	Delay time.Duration // Delay between two attempts to acquire lock, DefaultDelay if 0

	Factor float64 // Drift factor, DefaultFactor if 0

	Quorum int // Quorum for the lock, set to len(addrs)/2+1 by NewMutex()
	// contains filtered or unexported fields
}

A Mutex is a mutual exclusion lock.

func NewMutex

func NewMutex(genericNodes []Pool) *Mutex

NewMutex initializes a new regex on a redis pool

func (*Mutex) Lock

func (m *Mutex) Lock(key string) error

Lock will put a lock key in redis In case it returns an error on failure, you may retry to acquire the lock by calling this method again.

func (*Mutex) Unlock

func (m *Mutex) Unlock(key string) bool

Unlock will delete the lock key It returns the status of the unlock

type Pool

type Pool interface {
	Get() redis.Conn
	Close() error
}

Pool is a generic connection pool

type Redis

type Redis struct {
	// Redis address and max pool connections
	Protocol       string
	Address        string
	MaxIdle        int
	MaxConnections int
}

Redis holds connection options for redis

func (*Redis) NewRedisCache

func (r *Redis) NewRedisCache()

NewRedisCache creates a new pool

type Store added in v1.3.0

type Store struct {
	Pool  Pool
	Mutex *Mutex
	Mock  *redigomock.Conn
}

Store holds a handle to the Redis pool

var (
	// Cache holds a store
	Cache Store
	// ErrCacheMiss is an error for cache misses
	ErrCacheMiss = errors.New("cache: key not found")
)

func (*Store) Delete added in v1.3.0

func (c *Store) Delete(key ...interface{}) (err error)

Delete will delete a key

func (*Store) Expire added in v1.3.0

func (c *Store) Expire(key string, timeout uint) (err error)

Expire will set expire on a redis key

func (*Store) Flush added in v1.3.0

func (c *Store) Flush() (err error)

Flush will call flushall and delete all keys

func (*Store) Get added in v1.3.0

func (c *Store) Get(key string) ([]byte, error)

Get will retrieve a key

func (*Store) HGet added in v1.3.0

func (c *Store) HGet(key string, value string) ([]byte, error)

HGet will retrieve a hash

func (*Store) HMSet added in v1.3.0

func (c *Store) HMSet(key string, value string, result []byte) (err error)

HMSet will set a hash

func (*Store) Incr added in v1.3.0

func (c *Store) Incr(key string) (result int, err error)

Incr will increment a redis key

func (*Store) Lock added in v1.3.0

func (c *Store) Lock(key string) error

Lock our shared mutex

func (*Store) Set added in v1.3.0

func (c *Store) Set(key string, result []byte) (err error)

Set will set a single record

func (*Store) SetEx added in v1.3.0

func (c *Store) SetEx(key string, timeout uint, result []byte) (err error)

SetEx will set a single record with an expiration

func (*Store) Unlock added in v1.3.0

func (c *Store) Unlock(key string) bool

Unlock our shared mutex

type Storer added in v1.3.0

type Storer interface {
	Lock(key string) error
	Unlock(key string) bool
	Get(key string) (result []byte, err error)
	HGet(key string, value string) (result []byte, err error)
	Set(key string, result []byte) (err error)
	SetEx(key string, timeout uint, result []byte) (err error)
	HMSet(key string, value string, result []byte) (err error)
	Delete(key ...interface{}) (err error)
	Flush() (err error)
	Incr(key string) (result int, err error)
	Expire(key string, timeout uint) (err error)
}

Storer defines custom methods for redis operations

Jump to

Keyboard shortcuts

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