Documentation ¶
Index ¶
Constants ¶
const ( CacheTypeSimple = "simple" CacheTypeFreeCache = "freecache" )
kv cache types
const ( // DefaultRetryCount is the max retry times for lock acquire DefaultRetryCount = 10 // DefaultRetryDelay is upper wait time in millisecond for lock acquire retry DefaultRetryDelay = 200 // ClockDriftFactor is clock drift factor, more information refers to doc ClockDriftFactor = 0.01 // UnlockScript is redis lua script to release a lock UnlockScript = `` /* 155-byte string literal not displayed */ )
Variables ¶
var ( // ErrLockSingleRedis represents error when acquiring lock on a single redis ErrLockSingleRedis = errors.New("set lock on single redis failed") // ErrAcquireLock means acquire lock failed after max retry time ErrAcquireLock = errors.New("failed to require lock") )
Functions ¶
This section is empty.
Types ¶
type CacheOption ¶
type CacheOption func(*CacheOptions)
CacheOption alias to the function that can be used to configure CacheOptions
func WithCacheSize ¶
func WithCacheSize(size int) CacheOption
WithCacheSize sets CacheSize of CacheOptions
func WithCacheType ¶
func WithCacheType(tp string) CacheOption
WithCacheType sets CacheType to CacheOptions
func WithDisableGC ¶
func WithDisableGC(disabled bool) CacheOption
WithDisableGC sets DisableGC to CacheOptions
func WithGCInterval ¶
func WithGCInterval(interval time.Duration) CacheOption
WithGCInterval sets GCInterval of CacheOptions
type CacheOptions ¶
CacheOptions defines optional parameters for configuring kv cache.
type FreeCache ¶
type FreeCache struct {
// contains filtered or unexported fields
}
FreeCache is a wrapper of freecache.Cache
func NewFreeCache ¶
func NewFreeCache(options *CacheOptions) *FreeCache
NewFreeCache returns a new FreeCache instance
type KVCache ¶
type KVCache interface { // Set sets the key value with its expiry in nanoseconds Set(key, val string, expiry int64) (*LockElem, error) // Get queries LockElem from given key Get(key string) (*LockElem, error) // Delete removes the LockElem with given key from storage Delete(key string) // Size returns element count in kv storage Size() int }
KVCache defines interface for redlock key value storage
func NewCacheImpl ¶
func NewCacheImpl(ctx context.Context, opts ...CacheOption) KVCache
NewCacheImpl returns a KVCache implementation based on given cache type
type LockElem ¶
type LockElem struct { Val string `json:"val"` Expiry int64 `json:"expiry"` Ts time.Time `json:"ts"` }
LockElem keeps a lock element
type RedClient ¶
type RedClient struct {
// contains filtered or unexported fields
}
RedClient holds client to redis
type RedLock ¶
type RedLock struct {
// contains filtered or unexported fields
}
RedLock holds the redis lock
func NewRedLock ¶
NewRedLock creates a RedLock
func (*RedLock) Lock ¶
func (r *RedLock) Lock(ctx context.Context, resource string, ttl time.Duration) (time.Duration, error)
Lock acquires a distribute lock, returns - the remaining valid duration that lock is guaranted - error if acquire lock fails
func (*RedLock) SetRetryCount ¶
SetRetryCount sets acquire lock retry count
func (*RedLock) SetRetryDelay ¶
SetRetryDelay sets acquire lock retry max internal in millisecond
type SimpleCache ¶
type SimpleCache struct {
// contains filtered or unexported fields
}
SimpleCache is the most native implementation of KVCache interface
func NewSimpleCache ¶
func NewSimpleCache(ctx context.Context, options *CacheOptions) *SimpleCache
NewSimpleCache creates a new SimpleCache object
func (*SimpleCache) Delete ¶
func (sc *SimpleCache) Delete(key string)
Delete implements KVCache.Delete
func (*SimpleCache) Get ¶
func (sc *SimpleCache) Get(key string) (*LockElem, error)
Get implements KVCache.Get