Skip to content

Import path: gitlab.soludian.com/soludian/fountain/libs/cache

cache

go
import "gitlab.soludian.com/soludian/fountain/libs/cache"

Index

func Get

go
func Get[T any](ch Cache, args ...any) *T

func GetBool

go
func GetBool(v any) bool

GetBool converts interface to bool.

func GetFloat64

go
func GetFloat64(v any) float64

GetFloat64 converts interface to float64.

func GetInt

go
func GetInt(v any) int

GetInt converts interface to int.

func GetInt64

go
func GetInt64(v any) int64

GetInt64 converts interface to int64.

func GetString

go
func GetString(v any) string

GetString converts interface to string.

func MapByteToInterface

go
func MapByteToInterface(strM map[string][]byte) map[string]any

func Register

go
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

go
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)
go
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

go
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.NewLRUCache

func NewRandomExpireCache

go
func NewRandomExpireCache(adapter Cache, opts ...RandomExpireCacheOption) Cache

NewRandomExpireCache return random expire cache struct

type CanSetNameInf

go
type CanSetNameInf interface {
    SetName(name string)
}

type Instance

Instance is a function create a new Cache Instance

go
type Instance func() Cache

type RandomExpireCache

RandomExpireCache prevent cache batch invalidation Cache random time offset expired

go
type RandomExpireCache struct {
    Cache
    Offset func() time.Duration
}

func (*RandomExpireCache) Put

go
func (rec *RandomExpireCache) Put(key string, val any, timeout time.Duration) error

Put random time offset expired

type RandomExpireCacheOption

RandomExpireCacheOption implement generate random time offset expired option

go
type RandomExpireCacheOption func(*RandomExpireCache)

Generated by gomarkdoc