Newer
Older
package cachemdl
import (
"errors"
"time"
)
const (
// TypeFastCache indicates fast cache as cache storage
TypeFastCache = iota + 1
// TypeRedisCache indicates redis server as cache storage. Use this in grid mode.
TypeRedisCache
)
var (
// ErrInvalidCacheType indicates provided cache type is not supported
ErrInvalidCacheType = errors.New("invalid cache type provided")
)
// Cacher provides access to underlying cache, make sure all caches implement these methods.
//
// The return types of data can be different. Ex. In case of redis cache it is `string`. The caller needs to handle this with the help of Type() method.
//
// Ex.
// (Cacher{}).Type() == TypeRedisCache { // your implementation }
type Cacher interface {
// SET
Set(key string, val interface{})
SetWithExpiration(key string, val interface{}, exp time.Duration)
SetNoExpiration(key string, val interface{})
// GET
Get(key string) (interface{}, bool)
// GetAll() map[string]interface{}
// DELETE
Delete(key string)
// GetItemsCount
GetItemsCount() int
}
// GetCacheInstance returns a cache instance, panics if invalid cache type is provided
func GetCacheInstance(cfg *CacheConfig) Cacher {
switch cfg.Type {
case TypeFastCache:
cfg.FastCache.Setup(cfg.FastCache.MaxEntries, cfg.FastCache.Expiration, cfg.FastCache.CleanupTime)
cfg.RedisCache.Setup(cfg.RedisCache.Addr, cfg.RedisCache.Password, cfg.RedisCache.Prefix, cfg.RedisCache.DB, cfg.RedisCache.Expiration)
default:
panic(ErrInvalidCacheType)
}
}
// CacheConfig -
type CacheConfig struct {
Type int
FastCache *FastCacheHelper
RedisCache *RedisCache