redis

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

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-section: Redis Configuration

View Source
const (
	Nil = redis.Nil
)

Variables

This section is empty.

Functions

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 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.

This func looks for following prop:

"redis.address"
"redis.port"
"redis.username"
"redis.password"
"redis.database"

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 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 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) 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]) (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.

Types

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

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 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() error

Acquire lock.

func (*RLock) TryLock added in v0.1.16

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

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
}

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