cache

package module
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: MIT Imports: 9 Imported by: 1

README

go-cache

Build Status

常用的缓存组件

RedisCache

封装了一些常用的redis函数,保证所有缓存均需要指定ttl,并支持前缀形式、压缩以及二级缓存的处理。

指定缓存Key的前缀
// 创建一个redis的client
client := newRedisClient()
opts := []goCache.RedisCacheOption{
    goCache.RedisCachePrefixOption("prefix:"),
}
c := goCache.NewRedisCache(client, opts...)
指定数据压缩
// 创建一个redis的client
client := newRedisClient()
opts := []goCache.RedisCacheOption{
    goCache.RedisCachePrefixOption("prefix:"),
}
c := goCache.NewRedisCache(client, opts...)
// 大于10KB以上的数据压缩
// 适用于数据量较大,而且数据内容重复较多的场景
minCompressSize := 10 * 1024
return goCache.NewCompressRedisCache(
    client,
    minCompressSize,
    goCache.RedisCachePrefixOption("prefix:"),
)
Redis Session

用于elton中session的redis缓存。

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

MultilevelCache

使用lru+redis来组合多层缓存。

// 创建一个redis的client
client := newRedisClient()
c := goCache.NewRedisCache(client)
opts := []goCache.MultilevelCacheOption{
    goCache.MultilevelCacheRedisOption(c),
    goCache.MultilevelCacheLRUSizeOption(1024),
    goCache.MultilevelCacheTTLOption(5 * time.Minute),
    goCache.MultilevelCachePrefixOption("prefix:"),
}
return goCache.NewMultilevelCache(opts...)

Documentation

Index

Constants

View Source
const (
	// CompressNone compress none
	CompressNone byte = iota
	// Compressed compress
	Compressed
)

Variables

View Source
var ErrKeyIsNil = errors.New("key is nil")

Functions

func NewComprsser added in v0.2.0

func NewComprsser(opts CompressorOptions) *compressor

NewComprsser returns a new compressor

func NewLZ4Compressor added in v1.4.0

func NewLZ4Compressor(minCompressLength int) *compressor

NewLZ4Compressor returns a new lz4 compressor

func NewMultilevelCache

func NewMultilevelCache(opts ...MultilevelCacheOption) *lruttl.L2Cache

NewMultilevelCache returns a new multilevel cache, it will panic if Cache is nil or TTL is < one second

func NewSnappyCompressor added in v0.2.0

func NewSnappyCompressor(minCompressLength int) *compressor

NewSnappyCompressor returns a new snappy compressor

func NewZSTDCompressor added in v1.3.0

func NewZSTDCompressor(minCompressLength int) *compressor

NewZSTDCompressor returns a new zstd compressor

Types

type CompressorOptions added in v0.2.0

type CompressorOptions struct {
	// MinCompressLength is the min length to compress
	MinCompressLength int
	// Encode compress encode function
	Encode func([]byte) ([]byte, error)
	// Decode compress decode function
	Decode func([]byte) ([]byte, error)
	// Marshal marshal function
	Marshal func(v interface{}) ([]byte, error)
	// Unmarshal unmarshal function
	Unmarshal func(data []byte, v interface{}) error
}

type Done

type Done func() error

Done done function

type MultilevelCacheOption added in v0.2.1

type MultilevelCacheOption func(opt *multilevelCacheOptions)

func MultilevelCacheLRUSizeOption added in v0.2.1

func MultilevelCacheLRUSizeOption(size int) MultilevelCacheOption

MultilevelCacheLRUSizeOption sets lru size option

func MultilevelCacheMarshalOption added in v1.1.0

func MultilevelCacheMarshalOption(fn lruttl.L2CacheMarshal) MultilevelCacheOption

MultilevelCacheMarshalOption sets marshal function option

func MultilevelCachePrefixOption added in v0.2.1

func MultilevelCachePrefixOption(prefix string) MultilevelCacheOption

MultilevelCachePrefixOption sets prefix option

func MultilevelCacheRedisOption added in v0.2.1

func MultilevelCacheRedisOption(c *RedisCache) MultilevelCacheOption

MultilevelCacheRedisOption sets redis option

func MultilevelCacheTTLOption added in v0.2.1

func MultilevelCacheTTLOption(ttl time.Duration) MultilevelCacheOption

MultilevelCacheTTLOption sets ttl option

func MultilevelCacheUnmarshalOption added in v1.1.0

func MultilevelCacheUnmarshalOption(fn lruttl.L2CacheUnmarshal) MultilevelCacheOption

MultilevelCacheUnmarshalOption sets unmarshal function option

type RedisCache added in v0.0.2

type RedisCache struct {
	// contains filtered or unexported fields
}

RedisCache redis cache

func NewCompressRedisCache added in v0.3.2

func NewCompressRedisCache(c redis.UniversalClient, compressMinLength int, opts ...RedisCacheOption) *RedisCache

NewCompressRedisCache the same as NewSnappyRedisCache

func NewLZ4RedisCache added in v1.4.1

func NewLZ4RedisCache(c redis.UniversalClient, compressMinLength int, opts ...RedisCacheOption) *RedisCache

NewLZ4RedisCache returns a redis cache with lz4 compressor

func NewRedisCache

func NewRedisCache(c redis.UniversalClient, opts ...RedisCacheOption) *RedisCache

NewRedisCache returns a new redis cache

func NewSnappyRedisCache added in v1.3.0

func NewSnappyRedisCache(c redis.UniversalClient, compressMinLength int, opts ...RedisCacheOption) *RedisCache

NewSnappyRedisCache returns a redis cache with snappy compressor

func NewZSTDRedisCache added in v1.3.0

func NewZSTDRedisCache(c redis.UniversalClient, compressMinLength int, opts ...RedisCacheOption) *RedisCache

NewZSTDRedisCache returns a redis cache with zstd compressor

func (*RedisCache) Del added in v0.0.2

func (c *RedisCache) Del(ctx context.Context, key string) (int64, error)

Del deletes data from cache

func (*RedisCache) Get added in v0.0.2

func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, error)

Get gets value from cache

func (*RedisCache) GetAndDel added in v0.0.2

func (c *RedisCache) GetAndDel(ctx context.Context, key string) ([]byte, error)

GetAndDel gets value and deletes it remove cache

func (*RedisCache) GetIgnoreNilErr added in v0.0.2

func (c *RedisCache) GetIgnoreNilErr(ctx context.Context, key string) ([]byte, error)

GetIgnoreNilErr gets value from cache and ignore nil err

func (*RedisCache) GetStruct added in v0.0.2

func (c *RedisCache) GetStruct(ctx context.Context, key string, value interface{}) error

GetStruct gets cache and unmarshal to struct

func (*RedisCache) GetStructAndTTL added in v1.5.0

func (c *RedisCache) GetStructAndTTL(ctx context.Context, key string, value interface{}) (time.Duration, error)

GetStructAndTTL gets cache, unmarshal to struct and return the ttl of it. The cache should be set by SetStructWithTTL

func (*RedisCache) GetStructWithDone added in v0.2.0

func (c *RedisCache) GetStructWithDone(ctx context.Context, key string, value interface{}) (Done, error)

GetStructWithDone gets data from redis and unmarshal to struct, it returns a done function to delete the data.

func (*RedisCache) IncWith added in v0.0.2

func (c *RedisCache) IncWith(ctx context.Context, key string, value int64, ttl ...time.Duration) (int64, error)

IncWith inc the value of key from cache

func (*RedisCache) Lock added in v0.0.2

func (c *RedisCache) Lock(ctx context.Context, key string, ttl ...time.Duration) (bool, error)

Lock the key for ttl, ii will return true, nil if success

func (*RedisCache) LockWithDone added in v0.0.2

func (c *RedisCache) LockWithDone(ctx context.Context, key string, ttl ...time.Duration) (bool, Done, error)

LockWithDone locks the key for ttl and return done function to delete the lock

func (*RedisCache) Set added in v0.0.2

func (c *RedisCache) Set(ctx context.Context, key string, value interface{}, ttl ...time.Duration) error

Set sets data to cache, if ttl is not nil, it will use default ttl

func (*RedisCache) SetStruct added in v0.0.2

func (c *RedisCache) SetStruct(ctx context.Context, key string, value interface{}, ttl ...time.Duration) error

SetStruct marshals struct to bytes and sets to cache, if it will use default ttl if ttl is nil

func (*RedisCache) SetStructWithTTL added in v1.5.0

func (c *RedisCache) SetStructWithTTL(ctx context.Context, key string, value interface{}, ttl time.Duration) error

SetStructWithTTL adds expiredAt field to data, marshals data to bytes and sets to cache

func (*RedisCache) TTL added in v0.4.0

func (c *RedisCache) TTL(ctx context.Context, key string) (time.Duration, error)

TTL returns the ttl of the key

type RedisCacheOption added in v0.2.0

type RedisCacheOption func(c *RedisCache)

RedisCacheOption redis cache option

func RedisCacheMarshalOption added in v0.2.0

func RedisCacheMarshalOption(fn func(v interface{}) ([]byte, error)) RedisCacheOption

RedisCacheMarshalOption redis cache marshal option

func RedisCachePrefixOption added in v0.2.0

func RedisCachePrefixOption(prefix string) RedisCacheOption

RedisCachePrefixOption redis cache prefix option

func RedisCacheTTLOption added in v0.2.0

func RedisCacheTTLOption(ttl time.Duration) RedisCacheOption

RedisCacheTTLOption redis cache ttl option

func RedisCacheUnmarshalOption added in v0.2.0

func RedisCacheUnmarshalOption(fn func(data []byte, v interface{}) error) RedisCacheOption

RedisCacheUnmarshalOption redis cache unmarshal option

type RedisSession added in v0.0.2

type RedisSession struct {
	// contains filtered or unexported fields
}

func NewRedisSession

func NewRedisSession(c redis.UniversalClient) *RedisSession

NewRedisSession returns a new redis session

func (*RedisSession) Destroy added in v0.0.2

func (rs *RedisSession) Destroy(ctx context.Context, key string) error

Destroy session from redis

func (*RedisSession) Get added in v0.0.2

func (rs *RedisSession) Get(ctx context.Context, key string) ([]byte, error)

Get session from redis, it will not return error if data is not exists

func (*RedisSession) Set added in v0.0.2

func (rs *RedisSession) Set(ctx context.Context, key string, data []byte, ttl time.Duration) error

Set session to redis

func (*RedisSession) SetPrefix added in v0.0.2

func (rs *RedisSession) SetPrefix(prefix string)

SetPrefix sets prefix for redis session's key

Jump to

Keyboard shortcuts

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