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 (
|
2020-01-09 00:23:14 +02:00
|
|
|
"strings"
|
2019-06-11 18:49:34 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-01-30 13:39:00 +02:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2019-06-11 18:49:34 +02:00
|
|
|
)
|
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
type memoryStore struct {
|
2019-12-16 16:38:51 +02:00
|
|
|
options store.Options
|
2019-06-11 18:49:34 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-08 14:11:31 +02:00
|
|
|
func (m *memoryStore) Init(opts ...store.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.options)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-23 23:05:39 +02:00
|
|
|
func (m *memoryStore) List() ([]*store.Record, error) {
|
2019-06-11 18:49:34 +02:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2019-12-03 21:59:44 +02:00
|
|
|
//nolint:prealloc
|
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)
|
|
|
|
|
2019-07-11 06:58:20 +02:00
|
|
|
if d > time.Duration(0) {
|
2019-07-11 08:13:58 +02:00
|
|
|
// expired
|
|
|
|
if t > d {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// update expiry
|
2019-07-11 06:58:20 +02:00
|
|
|
v.r.Expiry -= t
|
|
|
|
v.c = time.Now()
|
|
|
|
}
|
2019-07-11 06:51:55 +02:00
|
|
|
|
2019-06-11 18:49:34 +02:00
|
|
|
values = append(values, v.r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return values, nil
|
|
|
|
}
|
|
|
|
|
2020-01-09 00:23:14 +02:00
|
|
|
func (m *memoryStore) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
2019-06-11 18:49:34 +02:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2020-01-09 00:23:14 +02:00
|
|
|
var options store.ReadOptions
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2019-06-11 18:49:34 +02:00
|
|
|
|
2020-01-09 00:23:14 +02:00
|
|
|
var vals []*memoryRecord
|
|
|
|
|
2020-02-03 10:16:02 +02:00
|
|
|
if options.Prefix {
|
2020-01-09 00:23:14 +02:00
|
|
|
for _, v := range m.values {
|
|
|
|
if !strings.HasPrefix(v.r.Key, key) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vals = append(vals, v)
|
|
|
|
}
|
2020-02-03 10:16:02 +02:00
|
|
|
} else if options.Suffix {
|
|
|
|
for _, v := range m.values {
|
|
|
|
if !strings.HasSuffix(v.r.Key, key) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vals = append(vals, v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v, ok := m.values[key]
|
|
|
|
if !ok {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
vals = []*memoryRecord{v}
|
2020-01-09 00:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:prealloc
|
|
|
|
var records []*store.Record
|
2019-10-11 15:08:50 +02:00
|
|
|
|
2020-01-09 00:23:14 +02:00
|
|
|
for _, v := range vals {
|
2019-10-11 15:08:50 +02:00
|
|
|
// get expiry
|
|
|
|
d := v.r.Expiry
|
|
|
|
t := time.Since(v.c)
|
|
|
|
|
|
|
|
// expired
|
|
|
|
if d > time.Duration(0) {
|
|
|
|
if t > d {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
// update expiry
|
|
|
|
v.r.Expiry -= t
|
|
|
|
v.c = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
records = append(records, v.r)
|
2019-07-11 06:58:20 +02:00
|
|
|
}
|
2019-07-11 06:51:55 +02:00
|
|
|
|
2019-10-11 15:08:50 +02:00
|
|
|
return records, nil
|
2019-06-11 18:49:34 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 00:23:14 +02:00
|
|
|
func (m *memoryStore) Write(r *store.Record) error {
|
2019-06-11 18:49:34 +02:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-01-09 00:23:14 +02:00
|
|
|
// set the record
|
|
|
|
m.values[r.Key] = &memoryRecord{
|
|
|
|
r: r,
|
|
|
|
c: time.Now(),
|
2019-06-11 18:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-09 00:23:14 +02:00
|
|
|
func (m *memoryStore) Delete(key string) error {
|
2019-06-11 18:49:34 +02:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-01-09 00:23:14 +02:00
|
|
|
// delete the value
|
|
|
|
delete(m.values, key)
|
2019-06-11 18:49:34 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-10 21:13:55 +02:00
|
|
|
func (m *memoryStore) String() string {
|
|
|
|
return "memory"
|
|
|
|
}
|
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
// NewStore returns a new store.Store
|
2019-12-16 16:38:51 +02:00
|
|
|
func NewStore(opts ...store.Option) store.Store {
|
|
|
|
var options store.Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2019-06-11 18:49:34 +02:00
|
|
|
|
2019-06-12 08:46:20 +02:00
|
|
|
return &memoryStore{
|
2019-12-16 16:38:51 +02:00
|
|
|
options: options,
|
2019-06-11 18:49:34 +02:00
|
|
|
values: make(map[string]*memoryRecord),
|
|
|
|
}
|
|
|
|
}
|