mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-18 05:35:10 +00:00
Streams tenancy (#91)
* fixup streams stuff * use cache instead of pg in streams * hash out issuer streams test * fix compile error
This commit is contained in:
55
pkg/cache/cache.go
vendored
55
pkg/cache/cache.go
vendored
@@ -8,8 +8,30 @@ import (
|
||||
"github.com/micro/micro/v3/service/store"
|
||||
)
|
||||
|
||||
func Get(key string, val interface{}) error {
|
||||
recs, err := store.Read(key, store.ReadLimit(1))
|
||||
type Cache interface {
|
||||
Get(key string, val interface{}) error
|
||||
Put(key string, val interface{}, expires time.Time) error
|
||||
Delete(key string) error
|
||||
}
|
||||
|
||||
type cache struct {
|
||||
Store store.Store
|
||||
}
|
||||
|
||||
var (
|
||||
DefaultCache = New(nil)
|
||||
)
|
||||
|
||||
func New(st store.Store) Cache {
|
||||
return &cache{st}
|
||||
}
|
||||
|
||||
func (c *cache) Get(key string, val interface{}) error {
|
||||
if c.Store == nil {
|
||||
c.Store = store.DefaultStore
|
||||
}
|
||||
|
||||
recs, err := c.Store.Read(key, store.ReadLimit(1))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -22,19 +44,40 @@ func Get(key string, val interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Put(key string, val interface{}, expires time.Time) error {
|
||||
func (c *cache) Put(key string, val interface{}, expires time.Time) error {
|
||||
if c.Store == nil {
|
||||
c.Store = store.DefaultStore
|
||||
}
|
||||
b, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
expiry := expires.Sub(time.Now())
|
||||
return store.Write(&store.Record{
|
||||
if expiry < time.Duration(0) {
|
||||
expiry = time.Duration(0)
|
||||
}
|
||||
return c.Store.Write(&store.Record{
|
||||
Key: key,
|
||||
Value: b,
|
||||
Expiry: expiry,
|
||||
})
|
||||
}
|
||||
|
||||
func Delete(key string) error {
|
||||
return store.Delete(key)
|
||||
func (c *cache) Delete(key string) error {
|
||||
if c.Store == nil {
|
||||
c.Store = store.DefaultStore
|
||||
}
|
||||
return c.Store.Delete(key)
|
||||
}
|
||||
|
||||
func Get(key string, val interface{}) error {
|
||||
return DefaultCache.Get(key, val)
|
||||
}
|
||||
|
||||
func Put(key string, val interface{}, expires time.Time) error {
|
||||
return DefaultCache.Put(key, val, expires)
|
||||
}
|
||||
|
||||
func Delete(key string) error {
|
||||
return DefaultCache.Delete(key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user