Documentation ¶
Overview ¶
Copyright 2022 tree xie
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2022 tree xie ¶
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- Constants
- Variables
- func Get[T any](ctx context.Context, c *Cache, key string) (*T, error)
- type Cache
- func (c *Cache) Close(ctx context.Context) error
- func (c *Cache) Delete(ctx context.Context, key string) error
- func (c *Cache) Get(ctx context.Context, key string, value any) error
- func (c *Cache) GetAndTTL(ctx context.Context, key string, value any) (time.Duration, error)
- func (c *Cache) GetBytes(ctx context.Context, key string) ([]byte, error)
- func (c *Cache) GetBytesAndTTL(ctx context.Context, key string) ([]byte, time.Duration, error)
- func (c *Cache) Set(ctx context.Context, key string, value any, ttl ...time.Duration) error
- func (c *Cache) SetBytes(ctx context.Context, key string, value []byte, ttl ...time.Duration) error
- type CacheOption
- func CacheCleanWindowOption(cleanWindow time.Duration) CacheOption
- func CacheCompressorOption(compressor Compressor) CacheOption
- func CacheHardMaxCacheSizeOption(hardMaxCacheSize int) CacheOption
- func CacheKeyPrefixOption(keyPrefix string) CacheOption
- func CacheMaxEntrySizeOption(maxEntrySize int) CacheOption
- func CacheMultiTTLOption(ttlList []time.Duration) CacheOption
- func CacheOnRemoveOption(onRemove func(key string)) CacheOption
- func CacheSecondaryStoreOption(store Store) CacheOption
- func CacheShardsOption(shards int) CacheOption
- func CacheSnappyOption(minCompressLength int) CacheOption
- func CacheStoreOption(store Store) CacheOption
- func CacheZSTDOption(minCompressLength, level int) CacheOption
- type Compressor
- type CompressorOption
- type Done
- type Marshaler
- type Option
- type RedisCache
- func (c *RedisCache) Del(ctx context.Context, key string) (int64, error)
- func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, error)
- func (c *RedisCache) GetAndDel(ctx context.Context, key string) ([]byte, error)
- func (c *RedisCache) GetIgnoreNilErr(ctx context.Context, key string) ([]byte, error)
- func (c *RedisCache) GetStruct(ctx context.Context, key string, value any) error
- func (c *RedisCache) GetStructAndTTL(ctx context.Context, key string, value any) (time.Duration, error)
- func (c *RedisCache) GetStructWithDone(ctx context.Context, key string, value any) (Done, error)
- func (c *RedisCache) IncWith(ctx context.Context, key string, value int64, ttl ...time.Duration) (int64, error)
- func (c *RedisCache) Lock(ctx context.Context, key string, ttl ...time.Duration) (bool, error)
- func (c *RedisCache) LockWithDone(ctx context.Context, key string, ttl ...time.Duration) (bool, Done, error)
- func (c *RedisCache) Set(ctx context.Context, key string, value any, ttl ...time.Duration) error
- func (c *RedisCache) SetStruct(ctx context.Context, key string, value any, ttl ...time.Duration) error
- func (c *RedisCache) SetStructWithTTL(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *RedisCache) TTL(ctx context.Context, key string) (time.Duration, error)
- type RedisCacheOption
- type RedisSession
- type Store
- type Unmarshaler
Constants ¶
const ( // CompressNone compress none CompressNone byte = iota // Compressed compress Compressed )
Variables ¶
var ErrIsNil = errors.New("Data is nil")
var ErrKeyIsNil = errors.New("Key is nil")
Functions ¶
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
func New ¶
func New(ttl time.Duration, opts ...CacheOption) (*Cache, error)
New creates a new cache with default ttl
func (*Cache) GetAndTTL ¶
GetAndTTL gets the value from cache and unmarshals it, and returns the ttl of value
func (*Cache) GetBytesAndTTL ¶
GetBytesAndTTL gets the data from cache and the ttl of data
type CacheOption ¶
type CacheOption func(opt *Option)
CacheOption cache option
func CacheCleanWindowOption ¶
func CacheCleanWindowOption(cleanWindow time.Duration) CacheOption
CacheCleanWindowOption set clean window for bigcache store
func CacheCompressorOption ¶
func CacheCompressorOption(compressor Compressor) CacheOption
CacheCompressorOption set compressor for store, the data will be compressed if matched
func CacheHardMaxCacheSizeOption ¶
func CacheHardMaxCacheSizeOption(hardMaxCacheSize int) CacheOption
CacheHardMaxCacheSizeOption set hard max cache size for bigcache store
func CacheKeyPrefixOption ¶
func CacheKeyPrefixOption(keyPrefix string) CacheOption
CacheKeyPrefixOption set key prefix for store
func CacheMaxEntrySizeOption ¶
func CacheMaxEntrySizeOption(maxEntrySize int) CacheOption
CacheMaxEntrySizeOption set max entry size for bigcache store
func CacheMultiTTLOption ¶ added in v2.0.1
func CacheMultiTTLOption(ttlList []time.Duration) CacheOption
CacheMultiTTLOption set multi ttl for store
func CacheOnRemoveOption ¶
func CacheOnRemoveOption(onRemove func(key string)) CacheOption
CacheOnRemoveOption set on remove function for bigcache store
func CacheSecondaryStoreOption ¶
func CacheSecondaryStoreOption(store Store) CacheOption
CacheSecondaryStoreOption set secondary store for cache, it is slower with greater capacity
func CacheShardsOption ¶
func CacheShardsOption(shards int) CacheOption
CacheShardsOption set shards for bigcache store
func CacheSnappyOption ¶
func CacheSnappyOption(minCompressLength int) CacheOption
CacheSnappyOption set snappy compress for store
func CacheStoreOption ¶
func CacheStoreOption(store Store) CacheOption
CacheStoreOption set custom store for cache, the bigcache store will be used as default
func CacheZSTDOption ¶
func CacheZSTDOption(minCompressLength, level int) CacheOption
CacheZSTDOption set zstd compress for store
type Compressor ¶
type Compressor interface { // Match tests the data should be compressed Match(size int) (matched bool) // Encode encodes the data Encode(data []byte) ([]byte, error) // Decode decodes the data Decode(data []byte) ([]byte, error) }
Compressor is the interface that support encode and decode function for compression.
Match returns the size is matched for compress
func NewCompressor ¶
func NewCompressor(opt CompressorOption) Compressor
NewCompressor creates a new compressor
func NewSnappyCompressor ¶
func NewSnappyCompressor(minCompressLength int) Compressor
NewSnappyCompressor creates a snappy compressor
func NewZSTDCompressor ¶
func NewZSTDCompressor(minCompressLength, level int) Compressor
NewZSTDCompressor creates a zstd compressor
type CompressorOption ¶
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache redis cache
func NewRedisCache ¶
func NewRedisCache(c redis.UniversalClient, opts ...RedisCacheOption) *RedisCache
NewRedisCache returns a new redis cache
func (*RedisCache) GetIgnoreNilErr ¶
GetIgnoreNilErr gets value from cache and ignore nil err
func (*RedisCache) GetStructAndTTL ¶
func (c *RedisCache) GetStructAndTTL(ctx context.Context, key string, value any) (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 ¶
GetStructWithDone gets data from redis and unmarshal to struct, it returns a done function to delete the data.
func (*RedisCache) IncWith ¶
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) LockWithDone ¶
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) SetStruct ¶
func (c *RedisCache) SetStruct(ctx context.Context, key string, value any, 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 ¶
func (c *RedisCache) SetStructWithTTL(ctx context.Context, key string, value any, ttl time.Duration) error
SetStructWithTTL adds expiredAt field to data, marshals data to bytes and sets to cache
type RedisCacheOption ¶
type RedisCacheOption func(c *RedisCache)
RedisCacheOption redis cache option
func RedisCachePrefixOption ¶
func RedisCachePrefixOption(prefix string) RedisCacheOption
func RedisCacheTTLOption ¶
func RedisCacheTTLOption(ttl time.Duration) RedisCacheOption
type RedisSession ¶
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 ¶
func (rs *RedisSession) Destroy(ctx context.Context, key string) error
Destroy session from redis
func (*RedisSession) SetPrefix ¶
func (rs *RedisSession) SetPrefix(prefix string)
SetPrefix sets prefix for redis session's key
type Store ¶
type Store interface { // Set sets data to store, the value should be copy before save to store Set(ctx context.Context, key string, value []byte, ttl time.Duration) error // Get gets data from store Get(ctx context.Context, key string) ([]byte, error) // Delete deletes data form store Delete(ctx context.Context, key string) error // Close closes the store Close(ctx context.Context) error }
Store interface for cache
func NewRedisStore ¶
func NewRedisStore(client redis.UniversalClient) Store