add otp service (#88)

This commit is contained in:
Asim Aslam
2021-04-28 10:09:17 +01:00
committed by GitHub
parent 2c8708ad65
commit b96bb863e2
14 changed files with 720 additions and 0 deletions

40
pkg/cache/cache.go vendored Normal file
View File

@@ -0,0 +1,40 @@
// 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)
}