Import path:
gitlab.soludian.com/soludian/fountain/libs/cache
cache
import "gitlab.soludian.com/soludian/fountain/libs/cache"Index
- func Get[T any](ch Cache, args ...any) *T
- func GetBool(v any) bool
- func GetFloat64(v any) float64
- func GetInt(v any) int
- func GetInt64(v any) int64
- func GetString(v any) string
- func MapByteToInterface(strM map[string][]byte) map[string]any
- func Register(name string, adapter Instance)
- func Set[T any](ch Cache, data T, duration time.Duration, args ...any)
- type Cache
- type CanSetNameInf
- type Instance
- type RandomExpireCache
- type RandomExpireCacheOption
func Get
func Get[T any](ch Cache, args ...any) *Tfunc GetBool
func GetBool(v any) boolGetBool converts interface to bool.
func GetFloat64
func GetFloat64(v any) float64GetFloat64 converts interface to float64.
func GetInt
func GetInt(v any) intGetInt converts interface to int.
func GetInt64
func GetInt64(v any) int64GetInt64 converts interface to int64.
func GetString
func GetString(v any) stringGetString converts interface to string.
func MapByteToInterface
func MapByteToInterface(strM map[string][]byte) map[string]anyfunc Register
func Register(name string, adapter Instance)Register makes a cache adapter available by the adapter name. If Register is called twice with the same name or if driver is nil, it panics.
func Set
func Set[T any](ch Cache, data T, duration time.Duration, args ...any)type Cache
Cache interface contains all behaviors for cache adapter. usage:
cache.Register("file",cache.NewFileCache) // this operation is run in init method of file.go.
c,err := cache.NewCache("file","{....}")
c.Put("key",value, 3600 * time.Second)
v := c.Peek("key")
c.Incr("counter") // now is 1
c.Incr("counter") // now is 2
count := c.Peek("counter").(int)type Cache interface {
GetDefaultConfig() string
// Check cache existed by key.
Has(key string) bool
// Get a cached value by key.
// Return nil or empty string if no cached value
Get(key string) ([]byte, error)
// GetMulti is a batch version of Get.
GetMulti(keys []string) ([][]byte, error)
// Peek retrieves a cached value by key.
// BuntDB luôn trả về giá trị dưới string (số được trả về một string dưới dạng float64).
Peek(key string) (any, error)
// PeekMulti is a batch version of Get.
// BuntDB luôn trả về giá trị dưới string (số được trả về một string dưới dạng float64).
PeekMulti(keys []string) ([]any, error)
// Set a cached value with key and expire time.
// (memory) Nếu val là object (hoặc con trỏ tới object) nên chuyển đổi sang JSON (hoặc codec khác) trước khi lưu để tránh lỗi không mong muốn.
Put(key string, val any, ttl time.Duration) error
// Set a cached values with key and expire time
PutMulti(values map[string]any, ttl time.Duration) error
Set(key string, val []byte, ttl time.Duration) error
SetMulti(values map[string][]byte, ttl time.Duration) error
// Delete cached value by key.
// Should not return error if key not found
Delete(key string) error
// Increment a cached int value by key, as a counter.
Incr(key string) (int64, error)
// Decrement a cached int value by key, as a counter.
Decr(key string) (int64, error)
// Reset resets the storage and delete all keys.
Reset() error
// Clear all cache with context.
ClearAll(ctx context.Context) error
// Start gc routine based on config string settings.
StartAndGC(config ...string) error
// Close closes the storage and will stop any running garbage
// collectors and open connections.
Close() error
}func NewCache
func NewCache(adapterName string, opts ...cacheOption) (Cache, error)NewCache khởi tạo một đối tượng Cache mới dựa trên tên adapter và các cấu hình được cung cấp.
adapterName: tên của adapter sẽ được sử dụng để khởi tạo Cache. cacheName: tên của Cache. configs: các cấu hình bổ sung cho Cache.
Trả về một đối tượng Cache và lỗi nếu có. Nếu adapterName không tồn tại trong danh sách adapters, hàm sẽ trả về lỗi với thông báo "UnknowCacheAdapter".
Usage:
bm, err := cache.NewCache("memory_cache", cache.WithName("cache_name"), cache.WithConfigRaw(`{"interval":20}`))
* adapter can be: memory, file, memcache, redis, ssdb,
* if wanna usage lru cache, must call lru_cache.NewLRUCachefunc NewRandomExpireCache
func NewRandomExpireCache(adapter Cache, opts ...RandomExpireCacheOption) CacheNewRandomExpireCache return random expire cache struct
type CanSetNameInf
type CanSetNameInf interface {
SetName(name string)
}type Instance
Instance is a function create a new Cache Instance
type Instance func() Cachetype RandomExpireCache
RandomExpireCache prevent cache batch invalidation Cache random time offset expired
type RandomExpireCache struct {
Cache
Offset func() time.Duration
}func (*RandomExpireCache) Put
func (rec *RandomExpireCache) Put(key string, val any, timeout time.Duration) errorPut random time offset expired
type RandomExpireCacheOption
RandomExpireCacheOption implement generate random time offset expired option
type RandomExpireCacheOption func(*RandomExpireCache)Generated by gomarkdoc