yaxc/internal/server/backend_cache.go

27 lines
443 B
Go
Raw Normal View History

2021-03-24 23:33:28 +08:00
package server
import (
"github.com/patrickmn/go-cache"
"time"
)
type CacheBackend struct {
c *cache.Cache
errCast error
}
func (b *CacheBackend) Get(key string) (res string, err error) {
if v, ok := b.c.Get(key); ok {
if s, ok := v.(string); ok {
return s, nil
}
return "", b.errCast
}
return "", nil
}
func (b *CacheBackend) Set(key, value string, ttl time.Duration) error {
b.c.Set(key, value, ttl)
return nil
}