2024-04-22 08:47:50 +03:00
|
|
|
package memory
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2024-11-26 12:18:17 +03:00
|
|
|
"sync/atomic"
|
2021-02-12 16:33:16 +03:00
|
|
|
"time"
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
cache "github.com/patrickmn/go-cache"
|
|
|
|
"go.unistack.org/micro/v3/options"
|
|
|
|
"go.unistack.org/micro/v3/store"
|
2021-02-12 16:33:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewStore returns a memory store
|
2024-04-22 08:47:50 +03:00
|
|
|
func NewStore(opts ...store.Option) store.Store {
|
2021-02-12 16:33:16 +03:00
|
|
|
return &memoryStore{
|
2024-04-22 08:47:50 +03:00
|
|
|
opts: store.NewOptions(opts...),
|
2021-02-12 16:33:16 +03:00
|
|
|
store: cache.New(cache.NoExpiration, 5*time.Minute),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Connect(ctx context.Context) error {
|
2024-11-26 12:18:17 +03:00
|
|
|
if m.opts.LazyConnect {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return m.connect(ctx)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Disconnect(ctx context.Context) error {
|
|
|
|
m.store.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type memoryStore struct {
|
2024-11-26 12:18:17 +03:00
|
|
|
funcRead store.FuncRead
|
|
|
|
funcWrite store.FuncWrite
|
|
|
|
funcExists store.FuncExists
|
|
|
|
funcList store.FuncList
|
|
|
|
funcDelete store.FuncDelete
|
|
|
|
store *cache.Cache
|
|
|
|
opts store.Options
|
|
|
|
isConnected atomic.Int32
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) key(prefix, key string) string {
|
2022-11-07 14:53:58 +03:00
|
|
|
return prefix + m.opts.Separator + key
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) exists(prefix, key string) error {
|
|
|
|
key = m.key(prefix, key)
|
|
|
|
_, found := m.store.Get(key)
|
|
|
|
if !found {
|
2024-04-22 08:47:50 +03:00
|
|
|
return store.ErrNotFound
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) get(prefix, key string, val interface{}) error {
|
|
|
|
key = m.key(prefix, key)
|
|
|
|
|
|
|
|
r, found := m.store.Get(key)
|
|
|
|
if !found {
|
2024-04-22 08:47:50 +03:00
|
|
|
return store.ErrNotFound
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buf, ok := r.([]byte)
|
|
|
|
if !ok {
|
2024-04-22 08:47:50 +03:00
|
|
|
return store.ErrNotFound
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
return m.opts.Codec.Unmarshal(buf, val)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) delete(prefix, key string) {
|
|
|
|
key = m.key(prefix, key)
|
|
|
|
m.store.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) list(prefix string, limit, offset uint) []string {
|
|
|
|
allItems := m.store.Items()
|
2021-07-15 22:53:12 +03:00
|
|
|
allKeys := make([]string, 0, len(allItems))
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
for k := range allItems {
|
2021-07-15 11:59:35 +03:00
|
|
|
if !strings.HasPrefix(k, prefix) {
|
2021-02-12 16:33:16 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-07-15 22:53:12 +03:00
|
|
|
k = strings.TrimPrefix(k, prefix)
|
|
|
|
if k[0] == '/' {
|
|
|
|
k = k[1:]
|
2021-07-15 11:59:35 +03:00
|
|
|
}
|
2021-07-15 22:53:12 +03:00
|
|
|
allKeys = append(allKeys, k)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
2021-07-15 22:53:12 +03:00
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
if limit != 0 || offset != 0 {
|
|
|
|
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
|
|
|
|
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
|
|
|
|
end := len(allKeys)
|
|
|
|
if limit > 0 {
|
|
|
|
calcLimit := int(offset + limit)
|
|
|
|
if calcLimit < end {
|
|
|
|
end = calcLimit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if int(offset) >= end {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return allKeys[offset:end]
|
|
|
|
}
|
|
|
|
return allKeys
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (m *memoryStore) Init(opts ...store.Option) error {
|
2021-02-12 16:33:16 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.opts)
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
|
|
|
|
m.funcRead = m.fnRead
|
|
|
|
m.funcWrite = m.fnWrite
|
|
|
|
m.funcExists = m.fnExists
|
|
|
|
m.funcList = m.fnList
|
|
|
|
m.funcDelete = m.fnDelete
|
|
|
|
|
|
|
|
m.opts.Hooks.EachNext(func(hook options.Hook) {
|
|
|
|
switch h := hook.(type) {
|
|
|
|
case store.HookRead:
|
|
|
|
m.funcRead = h(m.funcRead)
|
|
|
|
case store.HookWrite:
|
|
|
|
m.funcWrite = h(m.funcWrite)
|
|
|
|
case store.HookExists:
|
|
|
|
m.funcExists = h(m.funcExists)
|
|
|
|
case store.HookList:
|
|
|
|
m.funcList = h(m.funcList)
|
|
|
|
case store.HookDelete:
|
|
|
|
m.funcDelete = h(m.funcDelete)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) String() string {
|
|
|
|
return "memory"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Name() string {
|
|
|
|
return m.opts.Name
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (m *memoryStore) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
2024-11-26 12:18:17 +03:00
|
|
|
if m.opts.LazyConnect {
|
|
|
|
if err := m.connect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
return m.funcExists(ctx, key, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) fnExists(ctx context.Context, key string, opts ...store.ExistsOption) error {
|
|
|
|
options := store.NewExistsOptions(opts...)
|
2021-07-14 17:11:37 +03:00
|
|
|
if options.Namespace == "" {
|
|
|
|
options.Namespace = m.opts.Namespace
|
|
|
|
}
|
|
|
|
return m.exists(options.Namespace, key)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (m *memoryStore) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
2024-11-26 12:18:17 +03:00
|
|
|
if m.opts.LazyConnect {
|
|
|
|
if err := m.connect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
return m.funcRead(ctx, key, val, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) fnRead(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
|
|
|
options := store.NewReadOptions(opts...)
|
2021-07-14 17:11:37 +03:00
|
|
|
if options.Namespace == "" {
|
|
|
|
options.Namespace = m.opts.Namespace
|
|
|
|
}
|
|
|
|
return m.get(options.Namespace, key, val)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (m *memoryStore) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
2024-11-26 12:18:17 +03:00
|
|
|
if m.opts.LazyConnect {
|
|
|
|
if err := m.connect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
return m.funcWrite(ctx, key, val, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) fnWrite(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
|
|
|
options := store.NewWriteOptions(opts...)
|
2021-07-14 17:11:37 +03:00
|
|
|
if options.Namespace == "" {
|
|
|
|
options.Namespace = m.opts.Namespace
|
|
|
|
}
|
2021-07-15 11:59:35 +03:00
|
|
|
if options.TTL == 0 {
|
|
|
|
options.TTL = cache.NoExpiration
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
key = m.key(options.Namespace, key)
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
buf, err := m.opts.Codec.Marshal(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
m.store.Set(key, buf, options.TTL)
|
2021-02-12 16:33:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (m *memoryStore) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
2024-11-26 12:18:17 +03:00
|
|
|
if m.opts.LazyConnect {
|
|
|
|
if err := m.connect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
return m.funcDelete(ctx, key, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) fnDelete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
|
|
|
options := store.NewDeleteOptions(opts...)
|
2021-07-14 17:11:37 +03:00
|
|
|
if options.Namespace == "" {
|
|
|
|
options.Namespace = m.opts.Namespace
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
m.delete(options.Namespace, key)
|
2021-02-12 16:33:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (m *memoryStore) Options() store.Options {
|
2021-02-12 16:33:16 +03:00
|
|
|
return m.opts
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (m *memoryStore) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
2024-11-26 12:18:17 +03:00
|
|
|
if m.opts.LazyConnect {
|
|
|
|
if err := m.connect(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
return m.funcList(ctx, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) fnList(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
|
|
|
options := store.NewListOptions(opts...)
|
2021-07-14 17:11:37 +03:00
|
|
|
if options.Namespace == "" {
|
|
|
|
options.Namespace = m.opts.Namespace
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
keys := m.list(options.Namespace, options.Limit, options.Offset)
|
2021-02-12 16:33:16 +03:00
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
if len(options.Prefix) > 0 {
|
2021-02-12 16:33:16 +03:00
|
|
|
var prefixKeys []string
|
|
|
|
for _, k := range keys {
|
2021-07-14 17:11:37 +03:00
|
|
|
if strings.HasPrefix(k, options.Prefix) {
|
2021-02-12 16:33:16 +03:00
|
|
|
prefixKeys = append(prefixKeys, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keys = prefixKeys
|
|
|
|
}
|
|
|
|
|
2021-07-14 17:11:37 +03:00
|
|
|
if len(options.Suffix) > 0 {
|
2021-02-12 16:33:16 +03:00
|
|
|
var suffixKeys []string
|
|
|
|
for _, k := range keys {
|
2021-07-14 17:11:37 +03:00
|
|
|
if strings.HasSuffix(k, options.Suffix) {
|
2021-02-12 16:33:16 +03:00
|
|
|
suffixKeys = append(suffixKeys, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keys = suffixKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|
2024-11-26 12:18:17 +03:00
|
|
|
|
|
|
|
func (m *memoryStore) connect(ctx context.Context) error {
|
|
|
|
m.isConnected.CompareAndSwap(0, 1)
|
|
|
|
return nil
|
|
|
|
}
|