cache

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const KEY_PREFIX_EXPIRED = "expired:"
View Source
const KEY_PREFIX_LOCK = "locks:"
View Source
const KEY_PREFIX_SHADOW = "shadow:"

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheMap

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

func NewCacheMap

func NewCacheMap(config *schema.CacheConf) *CacheMap

func (*CacheMap) Load

func (c *CacheMap) Load(key interface{}) (value interface{}, result schema.RESULT, lastUpdated *time.Time)

func (*CacheMap) Store

func (c *CacheMap) Store(key interface{}, value interface{}, expireAt *time.Time)

Store any element into sync.Map in golang. If you want to set specific experation time, you need to set expireAt. If not, you can just set expireAt as nil and the duration in cache config will be used.

type CacheRedis

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

func NewCacheRedis

func NewCacheRedis(config *schema.CacheConf) *CacheRedis

func (*CacheRedis) Load

func (c *CacheRedis) Load(ctx context.Context, key string) (value interface{}, result schema.RESULT, lastUpdated *time.Time, err error)

func (*CacheRedis) Store

func (c *CacheRedis) Store(ctx context.Context, key string, value interface{}, expireAt *time.Time) error

Store any element into Redis server. The KEY should be string and the VALUE should be possible to be marshaled into json format before storing. If you want to set specific experation time, you need to set expireAt. If not, you can just set expireAt as nil and the duration in cache config will be used.

type MutexPool

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

Provide a pool which consists of sync.Mutex lock for keys. This pool is implemented based on cacheMap and the lock in this pool is valid within the process (local).

func NewMutexPool

func NewMutexPool(config *schema.CacheConf) (lockPool *MutexPool)

func (*MutexPool) Lock

func (l *MutexPool) Lock(key string)

func (*MutexPool) Unlock

func (l *MutexPool) Unlock(key string)

type RedisLockPool

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

Provide a pool which consists of redsync.Mutex lock for keys. This pool is implemented based on Redsync, CacheMap and Redis Cluster. The lock in this pool is valid within the same redis cluster (distributed). Redsync is a Redis-based distributed mutual exclusion lock implementation for Go as a library. CacheMap is used for caching redsync.Mutex locks to avoid from creating the same lock multiple times. Redis Cluster is used for storing the lock information.

func NewRedisLockPool

func NewRedisLockPool(config *schema.CacheConf) (lockPool *RedisLockPool)

func (*RedisLockPool) Lock

func (l *RedisLockPool) Lock(key string) (err error)

Obtain a lock for given key. After this is successful, no one else can obtain the same lock (the same mutex name) until it is unlocked.

func (*RedisLockPool) TryLock

func (l *RedisLockPool) TryLock(key string) (err error)

Obtain a lock for given key. After this is successful, no one else can obtain the same lock (the same mutex name) until it is unlocked. And also TryLock only attempts to lock m once and returns immediately regardless of success or failure without retrying.

func (*RedisLockPool) Unlock

func (l *RedisLockPool) Unlock(key string, lockError error) (ok bool, err error)

Release the lock and then other processes or threads can obtain a lock. Ok will represent the status of unlocking. lockError is the error which is returned by the Lock or TryLock. If lockError is not nil, Unlock will handle it based on error type. ok will be true and err will be nil if there was no issue on Unlock even though lockError is not nil so, the Unlock does not happen actually.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳