cache/store.go

23 lines
433 B
Go

package cache
import (
"bytes"
"encoding/gob"
"time"
"github.com/dgraph-io/badger/v3"
)
func (c *CacheRepository) Store(id string, data interface{}, ttl *time.Duration) error {
var buff bytes.Buffer
enc := gob.NewEncoder(&buff)
enc.Encode(data)
return c.cache.Update(func(txn *badger.Txn) error {
e := badger.NewEntry([]byte(id), buff.Bytes())
if ttl != nil {
e = e.WithTTL(*ttl)
}
return txn.SetEntry(e)
})
}