Files
opencloud/vendor/go-micro.dev/v4/store/memory.go
T
Jörn Friedrich Dreyer 076a3b0212 build(deps): bump go-micro to v4.11.4-0.20260929213538-cd412c159a26
Point the go-micro.dev/v4 replace at the latest main-v4 maintenance
branch commit in butonic/go-micro (cd412c15) instead of the v4.11.3
tag. The new base is upstream v4.11.1 plus the registry cache
node-TTL fixes (upstream #2715, #2736, #2740), the concurrent map
access fix (upstream #2794), plus further dependency updates and test
fixes on the maintenance branch.

The branch also modernizes go-micro's own go.mod (go 1.26, lego,
dario.cat/mergo v1, x/deque v2, ...), and MVS pulls the aligned
versions into this module: lego v4.35.2 -> lego/v5 v5.5.2,
go-jose/go-jose/v4 -> v4.1.5, gofrs/flock -> v0.13.1,
mattn/go-isatty -> v0.0.24, go.uber.org/zap -> v1.28.0, x/net ->
0.59.0, miekg/dns -> 1.1.73, moby api/client, ProtonMail/go-crypto
-> 1.5.2, klauspost/compress -> 1.20.1, gorilla/handlers -> 1.5.2,
gobwas/ws -> 1.4.0, nxadm/tail -> 1.4.11, ...

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
2026-10-01 11:14:15 +02:00

306 lines
6.0 KiB
Go

package store
import (
"path/filepath"
"sort"
"strings"
"time"
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
)
// NewMemoryStore returns a memory store.
func NewMemoryStore(opts ...Option) Store {
s := &memoryStore{
options: Options{
Database: "micro",
Table: "micro",
},
store: cache.New(cache.NoExpiration, 5*time.Minute),
}
for _, o := range opts {
o(&s.options)
}
return s
}
type memoryStore struct {
options Options
store *cache.Cache
}
type storeRecord struct {
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)
}
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 {
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
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
}
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)
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
}
func (m *memoryStore) Init(opts ...Option) error {
for _, o := range opts {
o(&m.options)
}
return nil
}
func (m *memoryStore) String() string {
return "memory"
}
func (m *memoryStore) Read(key string, opts ...ReadOption) ([]*Record, error) {
readOpts := ReadOptions{}
for _, o := range opts {
o(&readOpts)
}
prefix := m.prefix(readOpts.Database, readOpts.Table)
var keys []string
// Handle Prefix / suffix
if readOpts.Prefix || readOpts.Suffix {
var filtered []string
for _, kk := range m.list(prefix, 0, 0) {
if readOpts.Prefix && !strings.HasPrefix(kk, key) {
continue
}
if readOpts.Suffix && !strings.HasSuffix(kk, key) {
continue
}
filtered = append(filtered, kk)
}
// sort for deterministic pagination
sort.Strings(filtered)
limit := int(readOpts.Limit)
offset := int(readOpts.Offset)
if limit > len(filtered) {
limit = len(filtered)
}
if offset > len(filtered) {
offset = len(filtered)
}
keys = filtered[offset:limit]
} else {
keys = []string{key}
}
var results []*Record
for _, k := range keys {
r, err := m.get(prefix, k)
if err != nil {
return results, err
}
results = append(results, r)
}
return results, nil
}
func (m *memoryStore) Write(r *Record, opts ...WriteOption) error {
writeOpts := WriteOptions{}
for _, o := range opts {
o(&writeOpts)
}
prefix := m.prefix(writeOpts.Database, writeOpts.Table)
if len(opts) > 0 {
// Copy the record before applying options, or the incoming record will be mutated
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
}
// set
m.set(prefix, r)
return nil
}
func (m *memoryStore) Delete(key string, opts ...DeleteOption) error {
deleteOptions := DeleteOptions{}
for _, o := range opts {
o(&deleteOptions)
}
prefix := m.prefix(deleteOptions.Database, deleteOptions.Table)
m.delete(prefix, key)
return nil
}
func (m *memoryStore) Options() Options {
return m.options
}
func (m *memoryStore) List(opts ...ListOption) ([]string, error) {
listOptions := ListOptions{}
for _, o := range opts {
o(&listOptions)
}
prefix := m.prefix(listOptions.Database, listOptions.Table)
keys := m.list(prefix, listOptions.Limit, listOptions.Offset)
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
}