mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-24 10:07:04 +02:00
7a6a2b6373
* feat: redis implementation of cache
* Revert "feat: redis implementation of cache"
This reverts commit 4b3f25c1fc
.
* feat: redis implementation of cache
67 lines
1.1 KiB
Go
67 lines
1.1 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type memCache struct {
|
|
opts Options
|
|
sync.RWMutex
|
|
|
|
items map[string]Item
|
|
}
|
|
|
|
func (c *memCache) Get(ctx context.Context, key string) (interface{}, time.Time, error) {
|
|
c.RWMutex.RLock()
|
|
defer c.RWMutex.RUnlock()
|
|
|
|
item, found := c.items[key]
|
|
if !found {
|
|
return nil, time.Time{}, ErrKeyNotFound
|
|
}
|
|
if item.Expired() {
|
|
return nil, time.Time{}, ErrItemExpired
|
|
}
|
|
|
|
return item.Value, time.Unix(0, item.Expiration), nil
|
|
}
|
|
|
|
func (c *memCache) Put(ctx context.Context, key string, val interface{}, d time.Duration) error {
|
|
var e int64
|
|
if d == DefaultExpiration {
|
|
d = c.opts.Expiration
|
|
}
|
|
if d > 0 {
|
|
e = time.Now().Add(d).UnixNano()
|
|
}
|
|
|
|
c.RWMutex.Lock()
|
|
defer c.RWMutex.Unlock()
|
|
|
|
c.items[key] = Item{
|
|
Value: val,
|
|
Expiration: e,
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *memCache) Delete(ctx context.Context, key string) error {
|
|
c.RWMutex.Lock()
|
|
defer c.RWMutex.Unlock()
|
|
|
|
_, found := c.items[key]
|
|
if !found {
|
|
return ErrKeyNotFound
|
|
}
|
|
|
|
delete(c.items, key)
|
|
return nil
|
|
}
|
|
|
|
func (m *memCache) String() string {
|
|
return "memory"
|
|
}
|