Categorygithub.com/vicanso/go-cache/v2
modulepackage
2.4.0
Repository: https://github.com/vicanso/go-cache.git
Documentation: pkg.go.dev

# README

go-cache

Build Status

常用的缓存库,提供各类常用的缓存函数,通过简单的组合则可实现支持TTL、数据压缩的多级缓存

Store

默认提供了两种常用的支持ttl的store:

  • bigcache: 基于bigcache的内存store,但仅支持实例初始化时指定ttl,不可每个key设置不同的ttl
  • redis: 基于redis的store,支持实例化时指定默认的ttl,并可针对不同的key设置不同的ttl

示例

c, err := cache.New(
    10*time.Minute,
    // 数据清除时间窗口(仅用于bigcache),默认为1秒
    cache.CacheCleanWindowOption(5*time.Second),
    // 最大的entry大小,用于缓存的初始大小,默认为500(仅用于bigcache)
    cache.CacheMaxEntrySizeOption(1024),
    // 最大使用的内存大小(MB),默认为0不限制(仅用于bigcache)
    cache.CacheHardMaxCacheSizeOption(10),
    // 指定key的前缀
    cache.CacheKeyPrefixOption("prefix:"),
    // 指定二级缓存
    cache.CacheSecondaryStoreOption(cache.NewRedisStore(redisClient)),
    // 指定不同的缓存使用不同的ttl
    cache.CacheMultiTTLOption([]time.Duration{
        // 一级缓存有效期
        time.Minute,
        // 二级缓存有效期
        10 * time.Minute,
    }),
)

RedisCache

封装了一些常用的redis函数,保证所有缓存均需要指定ttl,并支持指定前缀。

指定缓存Key的前缀

// 创建一个redis的client
client := newRedisClient()
opts := []goCache.RedisCacheOption{
    goCache.RedisCachePrefixOption("prefix:"),
}
c := goCache.NewRedisCache(client, opts...)

Redis Session

用于elton中session的redis缓存。

// 创建一个redis的client
client := newRedisClient()
ss := goCache.NewRedisSession(client)
// 设置前缀
ss.SetPrefix(redisConfig.Prefix + "ss:")

# Packages

No description provided by the author

# Functions

CacheCleanWindowOption set clean window for bigcache store.
CacheCompressorOption set compressor for store, the data will be compressed if matched.
CacheHardMaxCacheSizeOption set hard max cache size for bigcache store.
CacheKeyPrefixOption set key prefix for store.
CacheMaxEntrySizeOption set max entry size for bigcache store.
CacheMultiTTLOption set multi ttl for store.
CacheOnRemoveOption set on remove function for bigcache store.
CacheSecondaryStoreOption set secondary store for cache, it is slower with greater capacity.
CacheShardsOption set shards for bigcache store.
CacheSnappyOption set snappy compress for store.
CacheStoreOption set custom store for cache, the bigcache store will be used as default.
CacheZSTDOption set zstd compress for store.
No description provided by the author
New creates a new cache with default ttl.
NewCompressor creates a new compressor.
NewRedisCache returns a new redis cache.
NewRedisSession returns a new redis session.
No description provided by the author
NewSnappyCompressor creates a snappy compressor.
NewZSTDCompressor creates a zstd compressor.
No description provided by the author
No description provided by the author

# Constants

Compressed compress.
CompressNone compress none.

# Variables

No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
RedisCache redis cache.
No description provided by the author

# Interfaces

Compressor is the interface that support encode and decode function for compression.
No description provided by the author
Store interface for cache.
No description provided by the author

# Type aliases

CacheOption cache option.
Done done function.
RedisCacheOption redis cache option.