2019-06-12 08:46:20 +02:00
|
|
|
// Package memory is a in-memory store store
|
2019-06-11 18:49:34 +02:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-06-12 13:45:42 +02:00
|
|
|
"github.com/micro/go-micro/config/options"
|
2019-06-19 23:04:13 +02:00
|
|
|
"github.com/micro/go-micro/data/store"
|
2019-06-11 18:49:34 +02:00
|
|
|
)
|
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
type memoryStore struct {
|
2019-06-11 18:49:34 +02:00
|
|
|
options.Options
|
|
|
|
|
|
|
|
sync.RWMutex
|
|
|
|
values map[string]*memoryRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
type memoryRecord struct {
|
2019-06-12 08:46:20 +02:00
|
|
|
r *store.Record
|
2019-06-11 18:49:34 +02:00
|
|
|
c time.Time
|
|
|
|
}
|
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
func (m *memoryStore) Dump() ([]*store.Record, error) {
|
2019-06-11 18:49:34 +02:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
var values []*store.Record
|
2019-06-11 18:49:34 +02:00
|
|
|
|
|
|
|
for _, v := range m.values {
|
|
|
|
// get expiry
|
|
|
|
d := v.r.Expiry
|
|
|
|
t := time.Since(v.c)
|
|
|
|
|
|
|
|
// expired
|
|
|
|
if d > time.Duration(0) && t > d {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-11 06:51:55 +02:00
|
|
|
|
|
|
|
// update expiry
|
|
|
|
v.r.Expiry -= t
|
|
|
|
v.c = time.Now()
|
|
|
|
|
2019-06-11 18:49:34 +02:00
|
|
|
values = append(values, v.r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return values, nil
|
|
|
|
}
|
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
func (m *memoryStore) Read(key string) (*store.Record, error) {
|
2019-06-11 18:49:34 +02:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
v, ok := m.values[key]
|
|
|
|
if !ok {
|
2019-06-12 08:46:20 +02:00
|
|
|
return nil, store.ErrNotFound
|
2019-06-11 18:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// get expiry
|
|
|
|
d := v.r.Expiry
|
|
|
|
t := time.Since(v.c)
|
|
|
|
|
|
|
|
// expired
|
|
|
|
if d > time.Duration(0) && t > d {
|
2019-06-12 08:46:20 +02:00
|
|
|
return nil, store.ErrNotFound
|
2019-06-11 18:49:34 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 06:51:55 +02:00
|
|
|
// update expiry
|
|
|
|
v.r.Expiry -= t
|
|
|
|
v.c = time.Now()
|
|
|
|
|
2019-06-11 18:49:34 +02:00
|
|
|
return v.r, nil
|
|
|
|
}
|
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
func (m *memoryStore) Write(r *store.Record) error {
|
2019-06-11 18:49:34 +02:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
// set the record
|
|
|
|
m.values[r.Key] = &memoryRecord{
|
|
|
|
r: r,
|
|
|
|
c: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
func (m *memoryStore) Delete(key string) error {
|
2019-06-11 18:49:34 +02:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
// delete the value
|
|
|
|
delete(m.values, key)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
// NewStore returns a new store.Store
|
|
|
|
func NewStore(opts ...options.Option) store.Store {
|
2019-06-11 18:49:34 +02:00
|
|
|
options := options.NewOptions(opts...)
|
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
return &memoryStore{
|
2019-06-11 18:49:34 +02:00
|
|
|
Options: options,
|
|
|
|
values: make(map[string]*memoryRecord),
|
|
|
|
}
|
|
|
|
}
|