|
@@ -2,7 +2,6 @@ package cache
|
|
|
|
|
|
import (
|
|
|
"github.com/hashicorp/golang-lru/v2/expirable"
|
|
|
- "github.com/mailru/easyjson"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -10,36 +9,40 @@ type ttlCache struct {
|
|
|
buf *expirable.LRU[string, []byte]
|
|
|
}
|
|
|
|
|
|
+//goland:noinspection GoUnusedExportedFunction
|
|
|
func NewTTLCache(size int, ttl time.Duration) Cache {
|
|
|
c := &ttlCache{}
|
|
|
c.buf = expirable.NewLRU[string, []byte](size, nil, ttl)
|
|
|
return c
|
|
|
}
|
|
|
|
|
|
-func (c *ttlCache) Get(elm Element) bool {
|
|
|
- buf, ok := c.buf.Get(elm.Key())
|
|
|
+func (c *ttlCache) Get(elm Element) error {
|
|
|
+ var k = elm.Key()
|
|
|
+
|
|
|
+ buf, ok := c.buf.Get(k)
|
|
|
if !ok {
|
|
|
- return false
|
|
|
+ return ErrorNotFound
|
|
|
}
|
|
|
|
|
|
- if err := easyjson.Unmarshal(buf, elm); err != nil {
|
|
|
- return false
|
|
|
+ if err := elm.Unmarshal(buf); err != nil {
|
|
|
+ c.buf.Remove(k)
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
- return true
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
-func (c *ttlCache) Set(elm Element) bool {
|
|
|
+func (c *ttlCache) Set(elm Element) error {
|
|
|
var k = elm.Key()
|
|
|
|
|
|
- buf, err := easyjson.Marshal(elm)
|
|
|
+ buf, err := elm.Marshal()
|
|
|
if err != nil {
|
|
|
c.buf.Remove(k)
|
|
|
- return false
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
c.buf.Add(k, buf)
|
|
|
- return true
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
func (c *ttlCache) String() string {
|