Documentation ¶
Index ¶
- Constants
- Variables
- func Cause(e error) error
- func Error(e error) error
- func PrintTracer(tt *Tracer)
- type Cache
- type CacheError
- type Cacher
- type CallbackFunc
- type Config
- type Database
- type Event
- type EventType
- type Key
- type LocalCacher
- type Locker
- type RedisCacher
- type RedisLock
- type RedisLockConfig
- type ReleaseFunc
- type TraceFunc
- type Tracer
Constants ¶
const ( EventGet = 1 EventUpdate = 2 EventRemove = 3 EventLocalCacheGet = 4 EventLocalCacheHit = 5 EventRemoteCacheGet = 6 EventRemoteCacheHit = 7 EventCacheMiss = 8 EventDatabaseFetch = 9 EventSetRemoteCache = 10 EventAcquireLock = 11 EventRegetAfterLockTimeout = 12 EventRegetAfterAcquireLock = 13 EventUpdateCacheFail = 14 EventUpdateDatabase = 15 EventUpdateCache = 16 EventRemoveDatabase = 17 EventRemoveCache = 18 )
Variables ¶
var ( ErrCacheMiss = errors.New("cache miss") ErrDataNotFound = errors.New("data not found in database") ErrLockContextDeadline = errors.New("lock context deadline") ErrFetchRemoteCache = errors.New("fetch remote cache failed") ErrFetchLocalCache = errors.New("fetch local cache failed") ErrFetchDatabase = errors.New("fetch database failed") ErrAcquireLockFail = errors.New("acquire lock failed") ErrMarshalProtoData = errors.New("marshal proto data failed") ErrMarshalJsonData = errors.New("marshal json data failed") ErrUnmarshalProtoData = errors.New("unmarshal proto data failed") ErrUnmarshalJsonData = errors.New("unmarshal json data failed") ErrTypeNotMatch = errors.New("type not match") ErrUpdateDatabase = errors.New("update database failed") ErrUpdateCahce = errors.New("update cache failed") ErrRemoveDatabase = errors.New("remove database failed") )
Functions ¶
func PrintTracer ¶
func PrintTracer(tt *Tracer)
Types ¶
type Cache ¶
type Cache struct { Config // contains filtered or unexported fields }
func (*Cache) Get ¶
Get will first try to get data from the two-level cache. If the cache does not hit, get the data from the database and flush the data to the cache. The specific steps are: - Get data from the two-level cache, if hit, return directly. - Cache missed, try to acquire lock - Get data from the remote cache again after acquiring the lock, if hit, return - No hit, go to the database to get data - Save data to remote cache
The entire Get does not store data in the local cache. If you need to cache data to local memory, you need to manually call the SaveLocal() function
Note that the "data does not exist in the database" will also be cached. If the data does not exist, the function will return "nil, cachex.ErrDataNotFound". In the cache, the key-value pair for which the data does not exist exists, but the value is empty.
When an error occurs, you can use cachex.Cause to track the specific error type for different error handling logic. There are the following error causes: - ErrFetchLocalCache: An error occurred when calling the local cache interface - ErrFetchRemoteCache: An error occurred when calling the remote cache interface - ErrMarshalProtoFail/ErrMarshalProtoFail: Failed to serialize the object - ErrUnmarshalProtoFail/ErrUnmarshalProtoFail: Failed to deserialize the object - ErrDataNotFound: No data found in the database
type CacheError ¶
type CacheError struct {
// contains filtered or unexported fields
}
func (*CacheError) Error ¶
func (e *CacheError) Error() string
type Cacher ¶
type Cacher interface { Get(key Key) ([]byte, error) Set(key Key, data []byte, ttl time.Duration) error Del(key Key) error }
func NewLocalCacher ¶
func NewRedisCacher ¶
func NewRedisClusterCacher ¶
type CallbackFunc ¶
type LocalCacher ¶
type LocalCacher struct { Prefix string // contains filtered or unexported fields }
func (*LocalCacher) Del ¶
func (c *LocalCacher) Del(key Key) error
type Locker ¶
type Locker interface {
Lock(key Key) (ReleaseFunc, error)
}
type RedisCacher ¶
type RedisCacher struct { Prefix string // contains filtered or unexported fields }
func (*RedisCacher) Del ¶
func (r *RedisCacher) Del(key Key) error
type RedisLock ¶
type RedisLock struct { RedisLockConfig // contains filtered or unexported fields }
func NewRedisClusterLock ¶
func NewRedisClusterLock(cfg *RedisLockConfig, opts *redis.ClusterOptions) *RedisLock
func NewRedisLock ¶
func NewRedisLock(cfg *RedisLockConfig, opts *redis.Options) *RedisLock
type RedisLockConfig ¶
type ReleaseFunc ¶
type ReleaseFunc func()