micro/store/scope/scope.go
Jake Sanders 1b4e881d74
Rewrite the store interface (#1335)
* WIP store rewrite

* Fix memory store tests

* Store hard expiry times rather than duration!

* Clarify memory test

* Add limit to store interface

* Implement suffix option

* Don't return nils from noop store

* Fix syncmap

* Start fixing store service

* wip service and cache

* Use _ for special characters in cockroachdb namespace

* Improve cockroach namespace comment

* Use service name as default store namespace

* Fixes

* Implement Store Scope

* Start fixing etcd

* implement read and write with expiry and prefix

* Fix etcd tests

* Fix cockroach store

* Fix cloudflare interface

* Fix certmagic / cloudflare store

* comment lint

* cache isn't implemented yet

* Only prepare DB staements once

Co-authored-by: Ben Toogood <ben@micro.mu>
Co-authored-by: ben-toogood <bentoogood@gmail.com>
2020-03-12 13:41:30 +00:00

52 lines
1.1 KiB
Go

package scope
import (
"fmt"
"github.com/micro/go-micro/v2/store"
)
// Scope extends the store, applying a prefix to each request
type Scope struct {
store.Store
prefix string
}
// NewScope returns an initialised scope
func NewScope(s store.Store, prefix string) Scope {
return Scope{Store: s, prefix: prefix}
}
func (s *Scope) Options() store.Options {
o := s.Store.Options()
o.Prefix = s.prefix
return o
}
func (s *Scope) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
key = fmt.Sprintf("%v/%v", s.prefix, key)
return s.Store.Read(key, opts...)
}
func (s *Scope) Write(r *store.Record, opts ...store.WriteOption) error {
r.Key = fmt.Sprintf("%v/%v", s.prefix, r.Key)
return s.Store.Write(r, opts...)
}
func (s *Scope) Delete(key string, opts ...store.DeleteOption) error {
key = fmt.Sprintf("%v/%v", s.prefix, key)
return s.Store.Delete(key, opts...)
}
func (s *Scope) List(opts ...store.ListOption) ([]string, error) {
var lops store.ListOptions
for _, o := range opts {
o(&lops)
}
key := fmt.Sprintf("%v/%v", s.prefix, lops.Prefix)
opts = append(opts, store.ListPrefix(key))
return s.Store.List(opts...)
}