1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-30 08:06:40 +02:00
go-micro/store/memory.go

302 lines
5.9 KiB
Go
Raw Normal View History

2020-12-29 17:49:26 +02:00
package store
2019-06-11 18:49:34 +02:00
import (
"path/filepath"
"sort"
"strings"
2019-06-11 18:49:34 +02:00
"time"
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
2019-06-11 18:49:34 +02:00
)
2022-09-30 16:27:07 +02:00
// NewMemoryStore returns a memory store.
2020-12-29 18:50:18 +02:00
func NewMemoryStore(opts ...Option) Store {
s := &memoryStore{
2020-12-29 17:49:26 +02:00
options: Options{
Database: "micro",
Table: "micro",
},
store: cache.New(cache.NoExpiration, 5*time.Minute),
}
for _, o := range opts {
o(&s.options)
}
return s
}
2019-06-12 08:46:20 +02:00
type memoryStore struct {
2020-12-29 17:49:26 +02:00
options Options
2019-06-11 18:49:34 +02:00
store *cache.Cache
2019-06-11 18:49:34 +02:00
}
type storeRecord struct {
2023-04-26 02:16:34 +02:00
expiresAt time.Time
metadata map[string]interface{}
key string
value []byte
}
func (m *memoryStore) key(prefix, key string) string {
return filepath.Join(prefix, key)
}
func (m *memoryStore) prefix(database, table string) string {
if len(database) == 0 {
database = m.options.Database
}
if len(table) == 0 {
table = m.options.Table
}
return filepath.Join(database, table)
}
2020-12-29 17:49:26 +02:00
func (m *memoryStore) get(prefix, key string) (*Record, error) {
key = m.key(prefix, key)
var storedRecord *storeRecord
r, found := m.store.Get(key)
if !found {
2020-12-29 17:49:26 +02:00
return nil, ErrNotFound
}
storedRecord, ok := r.(*storeRecord)
if !ok {
return nil, errors.New("Retrieved a non *storeRecord from the cache")
}
// Copy the record on the way out
2020-12-29 17:49:26 +02:00
newRecord := &Record{}
newRecord.Key = strings.TrimPrefix(storedRecord.key, prefix+"/")
newRecord.Value = make([]byte, len(storedRecord.value))
newRecord.Metadata = make(map[string]interface{})
// copy the value into the new record
copy(newRecord.Value, storedRecord.value)
// check if we need to set the expiry
if !storedRecord.expiresAt.IsZero() {
newRecord.Expiry = time.Until(storedRecord.expiresAt)
}
// copy in the metadata
for k, v := range storedRecord.metadata {
newRecord.Metadata[k] = v
}
return newRecord, nil
}
2020-12-29 17:49:26 +02:00
func (m *memoryStore) set(prefix string, r *Record) {
key := m.key(prefix, r.Key)
// copy the incoming record and then
// convert the expiry in to a hard timestamp
i := &storeRecord{}
i.key = r.Key
i.value = make([]byte, len(r.Value))
i.metadata = make(map[string]interface{})
// copy the the value
copy(i.value, r.Value)
// set the expiry
if r.Expiry != 0 {
i.expiresAt = time.Now().Add(r.Expiry)
}
// set the metadata
for k, v := range r.Metadata {
i.metadata[k] = v
}
m.store.Set(key, i, r.Expiry)
}
func (m *memoryStore) delete(prefix, key string) {
key = m.key(prefix, key)
2021-02-10 09:14:49 +02:00
m.store.Delete(key)
}
func (m *memoryStore) list(prefix string, limit, offset uint) []string {
allItems := m.store.Items()
foundKeys := make([]string, 0, len(allItems))
for k := range allItems {
if !strings.HasPrefix(k, prefix+"/") {
continue
}
foundKeys = append(foundKeys, strings.TrimPrefix(k, prefix+"/"))
}
if limit != 0 || offset != 0 {
sort.Slice(foundKeys, func(i, j int) bool { return foundKeys[i] < foundKeys[j] })
min := func(i, j uint) uint {
if i < j {
return i
}
return j
}
return foundKeys[offset:min(limit, uint(len(foundKeys)))]
}
return foundKeys
}
func (m *memoryStore) Close() error {
m.store.Flush()
return nil
}
2020-12-29 17:49:26 +02:00
func (m *memoryStore) Init(opts ...Option) error {
for _, o := range opts {
o(&m.options)
}
return nil
}
func (m *memoryStore) String() string {
return "memory"
}
2019-06-11 18:49:34 +02:00
2020-12-29 17:49:26 +02:00
func (m *memoryStore) Read(key string, opts ...ReadOption) ([]*Record, error) {
readOpts := ReadOptions{}
for _, o := range opts {
o(&readOpts)
}
2019-06-11 18:49:34 +02:00
prefix := m.prefix(readOpts.Database, readOpts.Table)
var keys []string
2019-06-11 18:49:34 +02:00
// Handle Prefix / suffix
if readOpts.Prefix || readOpts.Suffix {
k := m.list(prefix, 0, 0)
limit := int(readOpts.Limit)
offset := int(readOpts.Offset)
if limit > len(k) {
limit = len(k)
}
if offset > len(k) {
offset = len(k)
}
for i := offset; i < limit; i++ {
kk := k[i]
if readOpts.Prefix && !strings.HasPrefix(kk, key) {
continue
}
if readOpts.Suffix && !strings.HasSuffix(kk, key) {
continue
}
keys = append(keys, kk)
2019-07-11 06:58:20 +02:00
}
} else {
keys = []string{key}
}
2020-12-29 17:49:26 +02:00
var results []*Record
for _, k := range keys {
r, err := m.get(prefix, k)
if err != nil {
return results, err
}
results = append(results, r)
2019-06-11 18:49:34 +02:00
}
return results, nil
2019-06-11 18:49:34 +02:00
}
2020-12-29 17:49:26 +02:00
func (m *memoryStore) Write(r *Record, opts ...WriteOption) error {
writeOpts := WriteOptions{}
for _, o := range opts {
o(&writeOpts)
}
2019-06-11 18:49:34 +02:00
prefix := m.prefix(writeOpts.Database, writeOpts.Table)
if len(opts) > 0 {
// Copy the record before applying options, or the incoming record will be mutated
2020-12-29 17:49:26 +02:00
newRecord := Record{}
newRecord.Key = r.Key
newRecord.Value = make([]byte, len(r.Value))
newRecord.Metadata = make(map[string]interface{})
copy(newRecord.Value, r.Value)
newRecord.Expiry = r.Expiry
if !writeOpts.Expiry.IsZero() {
newRecord.Expiry = time.Until(writeOpts.Expiry)
}
if writeOpts.TTL != 0 {
newRecord.Expiry = writeOpts.TTL
}
for k, v := range r.Metadata {
newRecord.Metadata[k] = v
}
m.set(prefix, &newRecord)
return nil
}
2019-10-11 15:08:50 +02:00
// set
m.set(prefix, r)
return nil
2019-06-11 18:49:34 +02:00
}
2020-12-29 17:49:26 +02:00
func (m *memoryStore) Delete(key string, opts ...DeleteOption) error {
deleteOptions := DeleteOptions{}
for _, o := range opts {
o(&deleteOptions)
2019-06-11 18:49:34 +02:00
}
prefix := m.prefix(deleteOptions.Database, deleteOptions.Table)
m.delete(prefix, key)
return nil
2019-06-11 18:49:34 +02:00
}
2020-12-29 17:49:26 +02:00
func (m *memoryStore) Options() Options {
return m.options
}
2020-12-29 17:49:26 +02:00
func (m *memoryStore) List(opts ...ListOption) ([]string, error) {
listOptions := ListOptions{}
2019-12-16 16:38:51 +02:00
for _, o := range opts {
o(&listOptions)
2019-12-16 16:38:51 +02:00
}
prefix := m.prefix(listOptions.Database, listOptions.Table)
keys := m.list(prefix, listOptions.Limit, listOptions.Offset)
2019-06-11 18:49:34 +02:00
if len(listOptions.Prefix) > 0 {
var prefixKeys []string
for _, k := range keys {
if strings.HasPrefix(k, listOptions.Prefix) {
prefixKeys = append(prefixKeys, k)
}
}
keys = prefixKeys
}
if len(listOptions.Suffix) > 0 {
var suffixKeys []string
for _, k := range keys {
if strings.HasSuffix(k, listOptions.Suffix) {
suffixKeys = append(suffixKeys, k)
}
}
keys = suffixKeys
}
return keys, nil
2019-06-11 18:49:34 +02:00
}