2021-02-12 16:33:16 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/patrickmn/go-cache"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewStore returns a memory store
|
|
|
|
func NewStore(opts ...Option) Store {
|
|
|
|
return &memoryStore{
|
|
|
|
opts: NewOptions(opts...),
|
|
|
|
store: cache.New(cache.NoExpiration, 5*time.Minute),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Connect(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Disconnect(ctx context.Context) error {
|
|
|
|
m.store.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type memoryStore struct {
|
|
|
|
store *cache.Cache
|
2021-04-25 11:58:24 +03:00
|
|
|
opts Options
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) key(prefix, key string) string {
|
|
|
|
return filepath.Join(prefix, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) exists(prefix, key string) error {
|
|
|
|
key = m.key(prefix, key)
|
|
|
|
|
|
|
|
_, found := m.store.Get(key)
|
|
|
|
if !found {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, ok := r.([]byte)
|
|
|
|
if !ok {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
allKeys := make([]string, len(allItems))
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
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 12:11:55 +03:00
|
|
|
allKeys[i] = strings.TrimPrefix(k, prefix)
|
|
|
|
if allKeys[i][0] == '/' {
|
|
|
|
allKeys[i] = allKeys[i][1:]
|
2021-07-15 11:59:35 +03:00
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Init(opts ...Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) String() string {
|
|
|
|
return "memory"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Name() string {
|
|
|
|
return m.opts.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Exists(ctx context.Context, key string, opts ...ExistsOption) error {
|
2021-07-14 17:11:37 +03:00
|
|
|
options := NewExistsOptions(opts...)
|
|
|
|
if options.Namespace == "" {
|
|
|
|
options.Namespace = m.opts.Namespace
|
|
|
|
}
|
|
|
|
return m.exists(options.Namespace, key)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Read(ctx context.Context, key string, val interface{}, opts ...ReadOption) error {
|
2021-07-14 17:11:37 +03:00
|
|
|
options := NewReadOptions(opts...)
|
|
|
|
if options.Namespace == "" {
|
|
|
|
options.Namespace = m.opts.Namespace
|
|
|
|
}
|
|
|
|
return m.get(options.Namespace, key, val)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Write(ctx context.Context, key string, val interface{}, opts ...WriteOption) error {
|
2021-07-14 17:11:37 +03:00
|
|
|
options := NewWriteOptions(opts...)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Delete(ctx context.Context, key string, opts ...DeleteOption) error {
|
2021-07-14 17:11:37 +03:00
|
|
|
options := NewDeleteOptions(opts...)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) Options() Options {
|
|
|
|
return m.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryStore) List(ctx context.Context, opts ...ListOption) ([]string, error) {
|
2021-07-14 17:11:37 +03:00
|
|
|
options := NewListOptions(opts...)
|
|
|
|
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
|
|
|
|
}
|