cache

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2020 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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 Cause

func Cause(e error) error

func Error

func Error(e error) error

func PrintTracer

func PrintTracer(tt *Tracer)

Types

type Cache

type Cache struct {
	Config
	// contains filtered or unexported fields
}

func New

func New(p interface{}, cfg Config) *Cache

func (*Cache) Get

func (c *Cache) Get(key Key) (interface{}, error)

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

func (*Cache) Remove

func (c *Cache) Remove(key Key) error

func (*Cache) Update

func (c *Cache) Update(key Key, oper interface{}) (interface{}, error)

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 NewLocalCacher(prefix string, max int) Cacher

func NewRedisCacher

func NewRedisCacher(prefix string, opts *redis.Options) Cacher

func NewRedisClusterCacher

func NewRedisClusterCacher(prefix string, opts *redis.ClusterOptions) Cacher

type CallbackFunc

type CallbackFunc func(event int, err error)

type Config

type Config struct {
	Database Database

	RemoteCache Cacher
	RemoteTTL   time.Duration

	LocalCache Cacher
	LocalTTL   time.Duration

	Lock Locker

	DelCacheOnUpdate bool

	AsyncUpdateCache bool

	Callback CallbackFunc
	Tracer   TraceFunc
}

type Database

type Database interface {
	OnFetch(key Key) (interface{}, error)
	OnUpdate(key Key, oper interface{}) (interface{}, error)
	OnRemove(key Key) error
}

type Event

type Event struct {
	Type  EventType
	Start time.Time
	Took  time.Duration
}

type EventType

type EventType int

func (EventType) String

func (t EventType) String() string

type Key

type Key interface {
	String() string
	Interface() interface{}
}

type LocalCacher

type LocalCacher struct {
	Prefix string
	// contains filtered or unexported fields
}

func (*LocalCacher) Del

func (c *LocalCacher) Del(key Key) error

func (*LocalCacher) Get

func (c *LocalCacher) Get(key Key) ([]byte, error)

func (*LocalCacher) Set

func (c *LocalCacher) Set(key Key, data []byte, ttl time.Duration) 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

func (*RedisCacher) Get

func (r *RedisCacher) Get(key Key) ([]byte, error)

func (*RedisCacher) Set

func (r *RedisCacher) Set(key Key, data []byte, ttl time.Duration) 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

func (*RedisLock) Lock

func (l *RedisLock) Lock(key Key) (rf ReleaseFunc, retErr error)

type RedisLockConfig

type RedisLockConfig struct {
	Prefix string

	Expiry  time.Duration
	Timeout time.Duration

	RetryInterval time.Duration
	RetryTime     int
}

type ReleaseFunc

type ReleaseFunc func()

type TraceFunc

type TraceFunc func(tracer *Tracer)

type Tracer

type Tracer struct {
	Main Event
	Subs []Event
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL