mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 11:15:12 +00:00
41 lines
741 B
Go
41 lines
741 B
Go
// Cache provides a simple marshaling layer on top of the store
|
|
package cache
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/micro/micro/v3/service/store"
|
|
)
|
|
|
|
func Get(key string, val interface{}) error {
|
|
recs, err := store.Read(key, store.ReadLimit(1))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(recs) == 0 {
|
|
return store.ErrNotFound
|
|
}
|
|
if err := json.Unmarshal(recs[0].Value, val); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Put(key string, val interface{}, expires time.Time) error {
|
|
b, err := json.Marshal(val)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
expiry := expires.Sub(time.Now())
|
|
return store.Write(&store.Record{
|
|
Key: key,
|
|
Value: b,
|
|
Expiry: expiry,
|
|
})
|
|
}
|
|
|
|
func Delete(key string) error {
|
|
return store.Delete(key)
|
|
}
|