Documentation
¶
Index ¶
- Constants
- Variables
- type Cache
- type GetCachePolicy
- type Item
- type Metrics
- type Option
- func CleanInterval(cleanInterval time.Duration) Option
- func DebugLog(debugLog bool) Option
- func Disabled(disabled bool) Option
- func GetConn(getConn func() redis.Conn) Option
- func GetPolicy(getPolicy GetCachePolicy) Option
- func Namespace(namespace string) Option
- func OnError(onError func(ctx context.Context, err error)) Option
- func OnMetric(onMetric func(key, objectType string, metricType string, count int, ...)) Option
- func RedisTTLFactor(redisTTLFactor int) Option
- func Separator(separator string) Option
- type Options
Constants ¶
View Source
const ( MetricTypeGetMemHit = "get_mem_hit" MetricTypeGetMemMiss = "get_mem_miss" MetricTypeGetMemExpired = "get_mem_expired" MetricTypeGetRedisHit = "get_redis_hit" MetricTypeGetRedisMiss = "get_redis_miss" MetricTypeGetRedisExpired = "get_redis_expired" MetricTypeGetCache = "get_cache" MetricTypeLoad = "load" MetricTypeAsyncLoad = "async_load" MetricTypeSetCache = "set_cache" MetricTypeSetMem = "set_mem" MetricTypeSetRedis = "set_redis" MetricTypeDeleteCache = "del_cache" MetricTypeDeleteMem = "del_mem" MetricTypeDeleteRedis = "del_redis" MetricTypeCount = "count" MetricTypeMemUsage = "mem_usage" )
Variables ¶
View Source
var (
ErrIllegalTTL = errors.New("illegal ttl, must be in whole numbers of seconds, no fractions")
)
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
// GetObject loader function f() will be called in case cache all miss
// suggest to use object_type#id as key or any other pattern which can easily extract object, aggregate metric for same object in onMetric
GetObject(ctx context.Context, key string, obj any, ttl time.Duration, f func() (any, error), opts ...Option) error
Delete(ctx context.Context, key string) error
}
type GetCachePolicy ¶ added in v1.2.0
type GetCachePolicy int
const ( GetPolicyReturnExpired GetCachePolicy = iota + 1 GetPolicyReloadOnExpiry )
type Item ¶
type Item struct {
Object interface{} `json:"object"` // object
Size int `json:"size"` // object size, in bytes.
ExpireAt int64 `json:"expire_at"` // data expiration timestamp. in milliseconds.
}
func (*Item) MarshalJSON ¶ added in v1.2.0
func (*Item) UnmarshalJSON ¶ added in v1.2.0
type Metrics ¶ added in v1.1.14
type Metrics struct {
// contains filtered or unexported fields
}
type Option ¶ added in v1.1.8
type Option func(*Options)
func CleanInterval ¶ added in v1.1.8
func GetPolicy ¶ added in v1.2.0
func GetPolicy(getPolicy GetCachePolicy) Option
func RedisTTLFactor ¶ added in v1.1.8
type Options ¶ added in v1.1.8
type Options struct {
Namespace string
// key should be in format object_type{Separator}id
// can be : or ; or #
Separator string
// clean interval for in-memory cache
CleanInterval time.Duration
// get policy when data is expired, ReturnExpired or ReloadOnExpiry
GetPolicy GetCachePolicy
// will call loader function when disabled id true
Disabled bool
// redis ttl = ttl*RedisTTLFactor, data in redis lives longer than memory cache.
RedisTTLFactor int
// retrieve redis connection
GetConn func() redis.Conn
// metrics
Metric Metrics
// must be provided for cache initialization, handle internal error
OnError func(ctx context.Context, err error)
// enable debug logging for cache operations
DebugLog bool
}
Click to show internal directories.
Click to hide internal directories.
