diff --git a/ocis-pkg/store/cache.go b/ocis-pkg/store/cache.go new file mode 100644 index 00000000000..060c4a161e8 --- /dev/null +++ b/ocis-pkg/store/cache.go @@ -0,0 +1,18 @@ +package cache + +import ( + "github.com/owncloud/ocis/v2/ocis-pkg/cache" + "go-micro.dev/v4/store" +) + +// Create returns a configured key-value micro store +// +// Each microservice (or whatever piece is using the store) should use the +// options available in the interface's operations to choose the right database +// and table to prevent collisions with other microservices. +// Recommended approach is to use "services" or "ocis-pkg" for the database, +// and "services//" or "ocis-pkg//" for the package name. +// Deprecated: use "github.com/owncloud/ocis/v2/ocis-pkg/store" Create +func Create(opts ...store.Option) store.Store { + return cache.Create(opts...) +} diff --git a/ocis-pkg/store/etcd/etcd.go b/ocis-pkg/store/etcd/etcd.go new file mode 100644 index 00000000000..123907f4122 --- /dev/null +++ b/ocis-pkg/store/etcd/etcd.go @@ -0,0 +1,12 @@ +package etcd + +import ( + "github.com/owncloud/ocis/v2/ocis-pkg/cache/etcd" + "go-micro.dev/v4/store" +) + +// Create a new go-micro store backed by etcd +// Deprecated: use "github.com/owncloud/ocis/v2/ocis-pkg/store/etcd" NewEtcdStore +func NewEtcdStore(opts ...store.Option) store.Store { + return etcd.NewEtcdStore(opts...) +} diff --git a/ocis-pkg/store/memory/memstore.go b/ocis-pkg/store/memory/memstore.go new file mode 100644 index 00000000000..8fadab801d1 --- /dev/null +++ b/ocis-pkg/store/memory/memstore.go @@ -0,0 +1,44 @@ +package memory + +import ( + "context" + + "github.com/owncloud/ocis/v2/ocis-pkg/cache/memory" + "go-micro.dev/v4/store" +) + +// Prepare a context to be used with the memory implementation. The context +// is used to set up custom parameters to the specific implementation. +// In this case, you can configure the maximum capacity for the MemStore +// implementation as shown below. +// ``` +// cache := NewMemStore( +// +// store.WithContext( +// NewContext( +// ctx, +// map[string]interface{}{ +// "maxCap": 50, +// }, +// ), +// ), +// +// ) +// ``` +// +// Available options for the MemStore are: +// * "maxCap" -> 512 (int) The maximum number of elements the cache will hold. +// Adding additional elements will remove old elements to ensure we aren't over +// the maximum capacity. +// +// For convenience, this can also be used for the MultiMemStore. +// Deprecated: use "github.com/owncloud/ocis/v2/ocis-pkg/store/memory" NewContext +func NewContext(ctx context.Context, storeParams map[string]interface{}) context.Context { + return memory.NewContext(ctx, storeParams) +} + +// Create a new MemStore instance +// Deprecated: use "github.com/owncloud/ocis/v2/ocis-pkg/store/memory" NewMemStore +func NewMemStore(opts ...store.Option) store.Store { + return memory.NewMemStore(opts...) +} diff --git a/ocis-pkg/store/memory/multimemstore.go b/ocis-pkg/store/memory/multimemstore.go new file mode 100644 index 00000000000..c35875d3b18 --- /dev/null +++ b/ocis-pkg/store/memory/multimemstore.go @@ -0,0 +1,13 @@ +package memory + +import ( + "github.com/owncloud/ocis/v2/ocis-pkg/cache/memory" + "go-micro.dev/v4/store" +) + +// Create a new MultiMemStore. A new MemStore will be mapped based on the options. +// A default MemStore will be mapped if no Database and Table aren't used. +// Deprecated: use "github.com/owncloud/ocis/v2/ocis-pkg/store/memory" NewMultiMemStore +func NewMultiMemStore(opts ...store.Option) store.Store { + return memory.NewMultiMemStore(opts...) +} diff --git a/ocis-pkg/store/options.go b/ocis-pkg/store/options.go new file mode 100644 index 00000000000..ad86fcd51b4 --- /dev/null +++ b/ocis-pkg/store/options.go @@ -0,0 +1,59 @@ +package cache + +import ( + "context" + "time" + + "go-micro.dev/v4/store" +) + +type typeContextKey struct{} + +// Store determines the implementation: +// - "memory", for a in-memory implementation, which is also the default if noone matches +// - "noop", for a noop store (it does nothing) +// - "etcd", for etcd +// - "nats-js" for nats-js, needs to have TTL configured at creation +// - "redis", for redis +// - "redis-sentinel", for redis-sentinel +// - "ocmem", custom in-memory implementation, with fixed size and optimized prefix +// and suffix search +func Store(val string) store.Option { + return func(o *store.Options) { + if o.Context == nil { + o.Context = context.Background() + } + + o.Context = context.WithValue(o.Context, typeContextKey{}, val) + } +} + +type sizeContextKey struct{} + +// Size configures the maximum capacity of the cache for the "ocmem" implementation, +// in number of items that the cache can hold per table. +// You can use 5000 to make the cache hold up to 5000 elements. +// The parameter only affects to the "ocmem" implementation, the rest will ignore it. +// If an invalid value is used, the default of 512 will be used instead. +func Size(val int) store.Option { + return func(o *store.Options) { + if o.Context == nil { + o.Context = context.Background() + } + + o.Context = context.WithValue(o.Context, sizeContextKey{}, val) + } +} + +type ttlContextKey struct{} + +// TTL is the time to live for documents stored in the store +func TTL(val time.Duration) store.Option { + return func(o *store.Options) { + if o.Context == nil { + o.Context = context.Background() + } + + o.Context = context.WithValue(o.Context, ttlContextKey{}, val) + } +}