2021-03-24 23:33:28 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/patrickmn/go-cache"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CacheBackend struct {
|
2021-03-26 17:59:53 +08:00
|
|
|
cacheVal *cache.Cache
|
|
|
|
cacheHsh *cache.Cache
|
|
|
|
errCast error
|
2021-03-24 23:33:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *CacheBackend) Get(key string) (res string, err error) {
|
2021-03-26 17:59:53 +08:00
|
|
|
return b.get(b.cacheVal, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *CacheBackend) GetHash(key string) (res string, err error) {
|
|
|
|
return b.get(b.cacheHsh, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *CacheBackend) Set(key, value string, ttl time.Duration) error {
|
|
|
|
log.Info("Updating cache cacheVal with key", key, "and ttl", ttl)
|
|
|
|
b.cacheVal.Set(key, value, ttl)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *CacheBackend) SetHash(key, value string, ttl time.Duration) error {
|
|
|
|
log.Info("Updating cache cacheHsh with key", key, "and ttl", ttl)
|
|
|
|
b.cacheHsh.Set(key, value, ttl)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *CacheBackend) get(c *cache.Cache, key string) (res string, err error) {
|
|
|
|
var cName string
|
|
|
|
if c == b.cacheVal {
|
|
|
|
cName = "val"
|
|
|
|
} else if c == b.cacheHsh {
|
|
|
|
cName = "hsh"
|
|
|
|
} else {
|
|
|
|
cName = "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
r1, r2, r3 := c.GetWithExpiration(key)
|
|
|
|
log.Info("Requesting cache >", cName, "(", c, ")", "< with key", key, "with result:", r1, "=>", r2, "=>", r3)
|
|
|
|
|
|
|
|
if v, ok := c.Get(key); ok {
|
2021-03-24 23:33:28 +08:00
|
|
|
if s, ok := v.(string); ok {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
return "", b.errCast
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|