Commit 48262fe4 authored by Akshay Bharambe's avatar Akshay Bharambe
Browse files

Add: Cacher interface and cache types

1. Add: Cacher interface and cache types. Fastcache implements this interface.
parent 87767f32
Branches
Tags
2 merge requests!134Mep release271219,!130Add: Redis as a cache to support grid mode
Showing with 57 additions and 0 deletions
package cachemdl
import (
"errors"
"time"
"github.com/patrickmn/go-cache"
)
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 supportd
ErrInvalidCacheType = errors.New("invalid cache type provided")
)
// Cacher provides access to underlying cache, make sure all caches implement these methods.
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)
// Purge()
// 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(10000, cache.NoExpiration, time.Minute*10)
return cfg.FastCache
default:
panic(ErrInvalidCacheType)
}
}
// CacheConfig -
type CacheConfig struct {
Type int
FastCache *FastCacheHelper
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment