Documentation ¶
Index ¶
- Variables
- func EnableRedisSingleFlightOption(c *DCache)
- func SetNowFunc(f func() time.Time)
- type CacheDuration
- type DCache
- func (c *DCache) Close()
- func (c *DCache) Get(ctx context.Context, key string, target any, expire time.Duration, ...) error
- func (c *DCache) GetWithTtl(ctx context.Context, key string, target any, read ReadWithTtlFunc, ...) (err error)
- func (c *DCache) Invalidate(ctx context.Context, key string) (err error)
- func (c *DCache) Ping(ctx context.Context) error
- func (c *DCache) Set(ctx context.Context, key string, val any, ttl time.Duration) (err error)
- func (c *DCache) SetMemCacheMaxTTLSeconds(ttl int64) error
- type DacheOption
- type ReadFunc
- type ReadWithTtlFunc
- type ValueBytesExpiredAt
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTimeout is timeout error ErrTimeout = errors.New("timeout") // ErrInternal should never happen ErrInternal = errors.New("internal") // ErrNotPointer value passed to get functions is not a pointer. ErrNotPointer = errors.New("value is not a pointer") // ErrTypeMismatch value passed to get functions is not a pointer. ErrTypeMismatch = errors.New("value type mismatches cached type") )
Functions ¶
func EnableRedisSingleFlightOption ¶ added in v0.3.0
func EnableRedisSingleFlightOption(c *DCache)
WithRedisSingleFlight enables single flight for Redis.
func SetNowFunc ¶
SetNowFunc is a helper function to replace time.Now(), usually used for testing.
Types ¶
type CacheDuration ¶
const ( SuperFast CacheDuration = CacheDuration(time.Second) VeryFast CacheDuration = CacheDuration(time.Second * 2) Fast CacheDuration = CacheDuration(time.Second * 5) Normal CacheDuration = CacheDuration(time.Second * 10) Slow CacheDuration = CacheDuration(time.Second * 30) VerySlow CacheDuration = CacheDuration(time.Minute * 1) SuperSlow CacheDuration = CacheDuration(time.Minute * 5) SuperDuperSlow CacheDuration = CacheDuration(time.Hour * 1) SuperSuperDuperSlow CacheDuration = CacheDuration(time.Hour * 5) NeverExpire CacheDuration = CacheDuration(0) )
func (CacheDuration) ToDuration ¶
func (c CacheDuration) ToDuration() time.Duration
type DCache ¶ added in v0.0.3
type DCache struct {
// contains filtered or unexported fields
}
DCache implements cache.
func NewDCache ¶ added in v0.0.3
func NewDCache( appName string, primaryClient redis.UniversalClient, inMemCache *freecache.Cache, readInterval time.Duration, enableStats bool, enableTracer bool, options ...DacheOption, ) (*DCache, error)
NewDCache creates a new cache client with in-memory cache if not @p inMemCache not nil. Cache MUST be explicitly closed by calling Close(). It will also register several Prometheus metrics to the default register. @p readInterval specify the duration between each read per key.
func (*DCache) Close ¶ added in v0.0.3
func (c *DCache) Close()
Close terminates redis pubsub gracefully
func (*DCache) Get ¶ added in v0.0.3
func (c *DCache) Get(ctx context.Context, key string, target any, expire time.Duration, read ReadFunc, noCache bool, noStore bool) error
Get will read the value from cache if exists or call read() to retrieve the value and cache it in both the memory and Redis by @p ttl. Inputs: @p key: Key used in cache @p value: A pointer to the memory piece of the type of the value.
For example, if we are caching string, then target must be of type *string. if we caching a null-able string, using *string to represent it, then the target must be of type **string, i.e., pointer to the pointer of string.
@p ttl: Expiration of cache key @p read: Actual call that hits underlying data source. @p noCache: The response value will be fetched through @p read(). The new value will be
cached, unless @p noStore is specified.
@p noStore: The response value will not be saved into the cache.
func (*DCache) GetWithTtl ¶ added in v0.0.3
func (c *DCache) GetWithTtl(ctx context.Context, key string, target any, read ReadWithTtlFunc, noCache bool, noStore bool) (err error)
GetWithTtl will read the value from cache if exists or call @p read to retrieve the value and cache it in both the memory and Redis by the ttl returned in @p read. Inputs: @p key: Key used in cache @p value: A pointer to the memory piece of the type of the value.
For example, if we are caching string, then target must be of type *string. if we caching a null-able string, using *string to represent it, then the target must be of type **string, i.e., pointer to the pointer of string.
@p read: Actual call that hits underlying data source that also returns a ttl for cache. @p noCache: The response value will be fetched through @p read(). The new value will be
cached, unless @p noStore is specified.
@p noStore: The response value will not be saved into the cache.
func (*DCache) Invalidate ¶ added in v0.0.3
Invalidate explicitly invalidates a cache key Inputs: key - key to invalidate
func (*DCache) Set ¶ added in v0.0.3
Set explicitly set a cache key to a val Inputs: key - key to set val - val to set ttl - ttl of key
func (*DCache) SetMemCacheMaxTTLSeconds ¶ added in v0.1.4
type DacheOption ¶ added in v0.3.0
type DacheOption func(*DCache)
type ReadWithTtlFunc ¶
ReadWithTtlFunc is the actual call to underlying data source while returning a duration as expire timer
type ValueBytesExpiredAt ¶
type ValueBytesExpiredAt struct { ValueBytes []byte `msgpack:"v,omitempty"` ExpiredAt int64 `msgpack:"e,omitempty"` // UNIX timestamp in Milliseconds. }
ValueBytesExpiredAt is how we store value and expiration time to Redis.