Add memory data store

This commit is contained in:
Asim Aslam 2019-06-11 17:49:34 +01:00
parent 8e4e710e15
commit 7727b359c8
2 changed files with 100 additions and 3 deletions

View File

@ -30,5 +30,5 @@ type Data interface {
type Record struct { type Record struct {
Key string Key string
Value []byte Value []byte
Expiration time.Duration Expiry time.Duration
} }

97
data/memory/memory.go Normal file
View File

@ -0,0 +1,97 @@
// Package memory is a in-memory data store
package memory
import (
"sync"
"time"
"github.com/micro/go-micro/data"
"github.com/micro/go-micro/options"
)
type memoryData struct {
options.Options
sync.RWMutex
values map[string]*memoryRecord
}
type memoryRecord struct {
r *data.Record
c time.Time
}
func (m *memoryData) Dump() ([]*data.Record, error) {
m.RLock()
defer m.RUnlock()
var values []*data.Record
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
}
values = append(values, v.r)
}
return values, nil
}
func (m *memoryData) Read(key string) (*data.Record, error) {
m.RLock()
defer m.RUnlock()
v, ok := m.values[key]
if !ok {
return nil, data.ErrNotFound
}
// get expiry
d := v.r.Expiry
t := time.Since(v.c)
// expired
if d > time.Duration(0) && t > d {
return nil, data.ErrNotFound
}
return v.r, nil
}
func (m *memoryData) Write(r *data.Record) error {
m.Lock()
defer m.Unlock()
// set the record
m.values[r.Key] = &memoryRecord{
r: r,
c: time.Now(),
}
return nil
}
func (m *memoryData) Delete(key string) error {
m.Lock()
defer m.Unlock()
// delete the value
delete(m.values, key)
return nil
}
// NewData returns a new data.Data
func NewData(opts ...options.Option) data.Data {
options := options.NewOptions(opts...)
return &memoryData{
Options: options,
values: make(map[string]*memoryRecord),
}
}