redis

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	/*
		------------------------------------

		Prop for Redis

		------------------------------------
	*/
	PropRedisEnabled  = "redis.enabled"
	PropRedisAddress  = "redis.address"
	PropRedisPort     = "redis.port"
	PropRedisUsername = "redis.username"
	PropRedisPassword = "redis.password"
	PropRedisDatabase = "redis.database"
)
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 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

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, supplier func() (T, error)) (T, error)

Get from cache else run supplier

Return miso.NoneErr if none is found

func (*RCache[T]) Put

func (r *RCache[T]) Put(rail miso.Rail, key string, t T) 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, 1000 attempts, i.e., retry for 5s).

func NewRLockf

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

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

func (*RLock) Lock

func (r *RLock) Lock() error

Acquire lock.

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 200 (that's 1s = 200 * 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