redis

package
v0.4.13 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Redis integration.

Index

Constants

View Source
const (

	// misoconfig-prop: enable Redis client | false
	PropRedisEnabled = "redis.enabled"

	// misoconfig-prop: Redis server host | localhost
	PropRedisAddress = "redis.address"

	// misoconfig-prop: Redis server port | 6379
	PropRedisPort = "redis.port"

	// misoconfig-prop: username
	PropRedisUsername = "redis.username"

	// misoconfig-prop: password
	PropRedisPassword = "redis.password"

	// misoconfig-prop: database | 0
	PropRedisDatabase = "redis.database"

	// misoconfig-prop: max connection pool size | `10 * runtime.GOMAXPROCS` or `64` whichever is greater
	// misoconfig-doc-only
	PropRedisMaxPoolSize = "redis.max-pool-size"

	// misoconfig-prop: minimum idle connection counts | 4
	PropRedisMinIdleConns = "redis.min-idle-conns"

	// misoconfig-prop: add timing hook to redis client | true
	PropRedisWithTimingHook = "redis.with-timing-hook"

	// misoconfig-prop: slow command log threshold (for timing hook) | 20ms
	PropRedisSlowLogThreshold = "redis.slow-log-threshold"
)

misoconfig-section: Redis Configuration

View Source
const (
	Nil = redis.Nil
)

Variables

This section is empty.

Functions

func BLPop added in v0.3.2

func BLPop(rail miso.Rail, timeout time.Duration, key string) ([]string, bool, error)

func BLPopJson added in v0.3.2

func BLPopJson[T any](rail miso.Rail, timeout time.Duration, key string) ([]T, bool, error)

func BRPop added in v0.3.2

func BRPop(rail miso.Rail, timeout time.Duration, key string) ([]string, bool, error)

func BRPopJson added in v0.3.2

func BRPopJson[T any](rail miso.Rail, timeout time.Duration, key string) ([]T, bool, error)

func Decr added in v0.3.2

func Decr(rail miso.Rail, key string) (after int64, er error)

func DecrBy added in v0.3.2

func DecrBy(rail miso.Rail, key string, v int64) (after int64, er error)

func Eval added in v0.3.2

func Eval(rail miso.Rail, script string, keys []string, args ...interface{}) (any, error)

func Exists added in v0.3.1

func Exists(rail miso.Rail, key string) (bool, error)

func Expire added in v0.3.1

func Expire(rail miso.Rail, key string, exp time.Duration) (bool, error)

func Get added in v0.3.1

func Get(rail miso.Rail, key string) (string, bool, error)

func GetJson added in v0.3.1

func GetJson[T any](rail miso.Rail, key string) (T, bool, error)

func GetRedis

func GetRedis() *redis.Client

Get Redis client

Must call InitRedis(...) method before this method.

func GetStr

func GetStr(key string) (string, error)

Get String

func Incr added in v0.3.2

func Incr(rail miso.Rail, key string) (after int64, er error)

func IncrBy added in v0.3.2

func IncrBy(rail miso.Rail, key string, v int64) (after int64, er error)

func InitRedis

func InitRedis(rail miso.Rail, p RedisConnParam) (*redis.Client, error)

Initialize redis client

If redis client has been initialized, current func call will be ignored

func InitRedisFromProp

func InitRedisFromProp(rail miso.Rail) (*redis.Client, error)

Initialize redis client from configuration

If redis client has been initialized, current func call will be ignored.

func IsNil added in v0.1.9

func IsNil(err error) bool

func IsRLockNotObtainedErr

func IsRLockNotObtainedErr(err error) bool

Check whether the error is 'redislock.ErrNotObtained'

func LPop added in v0.3.2

func LPop(rail miso.Rail, key string) (string, bool, error)

func LPopJson added in v0.3.2

func LPopJson[T any](rail miso.Rail, key string) (T, bool, error)

func LPush added in v0.3.2

func LPush(rail miso.Rail, key string, v any) error

func LPushJson added in v0.3.2

func LPushJson(rail miso.Rail, key string, v any) error

func NewRQueue added in v0.4.10

func NewRQueue[T any](key string) rqueue[T]

func NewRateLimiter added in v0.2.0

func NewRateLimiter(name string, max int, period time.Duration) *rateLimiter

func NewTopic added in v0.2.8

func NewTopic[T any](topic string) *rtopic[T]

func ObtainRLocker

func ObtainRLocker() *redislock.Client

Obtain a locker

func RLockExec

func RLockExec(ec miso.Rail, key string, runnable Runnable, op ...rLockOption) error

Lock and run the runnable using Redis

The maximum time wait for the lock is 1s, retry every 5ms.

May return 'redislock.ErrNotObtained' when it fails to obtain the lock.

func RLockRun

func RLockRun[T any](rail miso.Rail, key string, runnable LRunnable[T], op ...rLockOption) (T, error)

Lock and run the runnable using Redis

The maximum time wait for the lock is 1s, retry every 5ms.

May return 'redislock:ErrNotObtained' when it fails to obtain the lock.

func RPop added in v0.3.2

func RPop(rail miso.Rail, key string) (string, bool, error)

func RPopJson added in v0.3.2

func RPopJson[T any](rail miso.Rail, key string) (T, bool, error)

func RPush added in v0.3.2

func RPush(rail miso.Rail, key string, v any) error

func RPushJson added in v0.3.2

func RPushJson(rail miso.Rail, key string, v any) error

func Scan added in v0.3.2

func Scan(rail miso.Rail, pat string, scanLimit int64, f func(key string) error) error

func Set added in v0.3.1

func Set(rail miso.Rail, key string, val any, exp time.Duration) error

func SetJson added in v0.3.1

func SetJson(rail miso.Rail, key string, val any, exp time.Duration) error

func SetNX added in v0.3.1

func SetNX(rail miso.Rail, key string, val any, exp time.Duration) (bool, error)

func SetNXJson added in v0.3.1

func SetNXJson(rail miso.Rail, key string, val any, exp time.Duration) (bool, error)

func WithBackoff added in v0.2.17

func WithBackoff(backoff time.Duration) rLockOption

Types

type GroupCache added in v0.4.2

type GroupCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewGroupCache added in v0.4.2

func NewGroupCache[K comparable, V any](conf RCacheConfig, cacheName func(k string) string) *GroupCache[K, V]

func (*GroupCache[K, V]) Get added in v0.4.2

func (g *GroupCache[K, V]) Get(k string) *RCacheV2[K, V]

type JsonSerializer

type JsonSerializer struct {
}

func (JsonSerializer) Deserialize

func (j JsonSerializer) Deserialize(ptr any, v string) error

func (JsonSerializer) Serialize

func (j JsonSerializer) Serialize(t any) (string, error)

type LRunnable

type LRunnable[T any] func() (T, error)

type RCache

type RCache[T any] struct {
	ValueSerializer Serializer // serializer / deserializer
	// contains filtered or unexported fields
}

Redis Cache implementation.

RCache internal isn't backed by an actual redis HSet. Cache name is simply the prefix for each key, and each key is stored independently.

Use NewRCache(...) to instantiate.

func NewRCache

func NewRCache[T any](name string, conf RCacheConfig) RCache[T]

Create new RCache

Use NewRCacheV2 for complex key type.

func (*RCache[T]) Del

func (r *RCache[T]) Del(rail miso.Rail, key string) error

func (*RCache[T]) DelAll

func (r *RCache[T]) DelAll(rail miso.Rail) error

func (*RCache[T]) Exists

func (r *RCache[T]) Exists(rail miso.Rail, key string) (bool, error)

func (*RCache[T]) Get

func (r *RCache[T]) Get(rail miso.Rail, key string) (T, bool, error)

Get from cache

func (*RCache[T]) GetElse added in v0.2.4

func (r *RCache[T]) GetElse(rail miso.Rail, key string, supplier func() (T, bool, error)) (T, bool, error)

Get from cache else run supplier

func (*RCache[T]) GetVal added in v0.2.13

func (r *RCache[T]) GetVal(rail miso.Rail, key string) (T, error)

Get from cache

func (*RCache[T]) GetValElse added in v0.2.13

func (r *RCache[T]) GetValElse(rail miso.Rail, key string, supplier func() (T, error)) (T, error)

Get from cache else run supplier

func (*RCache[T]) Put

func (r *RCache[T]) Put(rail miso.Rail, key string, t T) error

func (*RCache[T]) RefreshTTL added in v0.2.1

func (r *RCache[T]) RefreshTTL(rail miso.Rail, key string) error

func (*RCache[T]) ScanAll added in v0.2.17

func (r *RCache[T]) ScanAll(rail miso.Rail, f func(keys []string) error) error

ScanAll is O(N) where N is the total number of keys in the redis database.

Use with caution.

type RCacheConfig

type RCacheConfig struct {
	//expire time for each entry
	Exp time.Duration

	// Disable use of distributed lock to synchronize access to the key in the cache.
	//
	// Most of the operations are atomic except Get(...) with supplier callback.
	// If your are loading the cache manually using Put(...), then you probably don't need synchronization at all.
	NoSync bool
}

Configuration of RCache.

type RCacheV2 added in v0.4.2

type RCacheV2[K any, T any] struct {
	// contains filtered or unexported fields
}

Redis Cache implementation.

RCache internal isn't backed by an actual redis HSet. Cache name is simply the prefix for each key, and each key is stored independently.

Use [NewRCacheV2] to instantiate.

func NewRCacheV2 added in v0.2.6

func NewRCacheV2[K any, T any](name string, conf RCacheConfig) *RCacheV2[K, T]

Create new RCache.

K type must either be string or implements fmt.Stringer, if not, it panics.

func (*RCacheV2[K, T]) Del added in v0.4.2

func (r *RCacheV2[K, T]) Del(rail miso.Rail, k K) error

func (*RCacheV2[K, T]) DelAll added in v0.4.2

func (r *RCacheV2[K, T]) DelAll(rail miso.Rail) error

func (*RCacheV2[K, T]) Exists added in v0.4.2

func (r *RCacheV2[K, T]) Exists(rail miso.Rail, k K) (bool, error)

func (*RCacheV2[K, T]) Get added in v0.4.2

func (r *RCacheV2[K, T]) Get(rail miso.Rail, k K) (T, bool, error)

func (*RCacheV2[K, T]) GetElse added in v0.4.2

func (r *RCacheV2[K, T]) GetElse(rail miso.Rail, k K, supplier func() (T, bool, error)) (T, bool, error)

func (*RCacheV2[K, T]) GetVal added in v0.4.2

func (r *RCacheV2[K, T]) GetVal(rail miso.Rail, k K) (T, error)

func (*RCacheV2[K, T]) GetValElse added in v0.4.2

func (r *RCacheV2[K, T]) GetValElse(rail miso.Rail, k K, supplier func() (T, error)) (T, error)

func (*RCacheV2[K, T]) Put added in v0.4.2

func (r *RCacheV2[K, T]) Put(rail miso.Rail, k K, t T) error

func (*RCacheV2[K, T]) RefreshTTL added in v0.4.2

func (r *RCacheV2[K, T]) RefreshTTL(rail miso.Rail, k K) error

func (*RCacheV2[K, T]) ScanAll added in v0.4.2

func (r *RCacheV2[K, T]) ScanAll(rail miso.Rail, f func(keys []string) error) error

type RLock

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

RLock

func NewCustomRLock

func NewCustomRLock(rail miso.Rail, key string, config RLockConfig) *RLock

Create customized RLock.

func NewRLock

func NewRLock(rail miso.Rail, key string) *RLock

Create new RLock with default backoff configuration (5ms backoff window, 6000 attempts, i.e., retry for 30s).

func NewRLockf

func NewRLockf(rail miso.Rail, keyPattern string, args ...any) *RLock

Create new RLock with default backoff configuration (5ms backoff window, 6000 attempts, i.e., retry for 30s).

func (*RLock) Lock

func (r *RLock) Lock(op ...rLockOption) error

Acquire lock.

Use WithBackoff to modify default configuration.

func (*RLock) TryLock added in v0.1.16

func (r *RLock) TryLock(op ...rLockOption) (locked bool, err error)

Try Lock.

Use WithBackoff to modify default configuration.

func (*RLock) Unlock

func (r *RLock) Unlock() error

Attempt to Unlock.

If the lock is not obtained, method call will be ignored.

type RLockConfig

type RLockConfig struct {
	// Backoff duration.
	//
	// This is not an exact configuration. Linear back off strategy is used with a window size of 5ms, which means RLock will attempt to acquire the lock every 5ms.
	//
	// The number of times we may attempt to acquire the lock is called steps, which is by default 600 (that's 30s = 600 * 5ms).
	// When BackoffDuration is provided, this duration is divided by 5ms to convert to steps and then used by RLock.
	BackoffDuration time.Duration
}

RLock Configuration.

type RedisConnParam

type RedisConnParam struct {
	Address         string
	Port            string
	Username        string
	Password        string
	Db              int
	MaxConnPoolSize int
	MinIdleConns    int
}

type Runnable

type Runnable func() error

type Serializer

type Serializer interface {
	Serialize(t any) (string, error)
	Deserialize(ptr any, v string) error
}

Value serializer / deserializer.

Jump to

Keyboard shortcuts

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