1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/store/store.go

52 lines
1.8 KiB
Go
Raw Normal View History

// Package store is an interface for distributed data storage.
// The design document is located at https://github.com/micro/development/blob/master/design/store.md
2019-06-12 08:46:20 +02:00
package store
2019-05-31 01:43:23 +02:00
import (
"errors"
"time"
)
var (
2022-09-30 16:27:07 +02:00
// ErrNotFound is returned when a key doesn't exist.
2019-05-31 01:43:23 +02:00
ErrNotFound = errors.New("not found")
// DefaultStore is the memory store.
2020-12-29 17:49:26 +02:00
DefaultStore Store = NewStore()
2019-05-31 01:43:23 +02:00
)
2022-09-30 16:27:07 +02:00
// Store is a data storage interface.
2019-06-12 08:46:20 +02:00
type Store interface {
2022-09-30 16:27:07 +02:00
// Init initializes the store. It must perform any required setup on the backing storage implementation and check that it is ready for use, returning any errors.
Init(...Option) error
// Options allows you to view the current options.
Options() Options
// Read takes a single key name and optional ReadOptions. It returns matching []*Record or an error.
Read(key string, opts ...ReadOption) ([]*Record, error)
// Write() writes a record to the store, and returns an error if the record was not written.
Write(r *Record, opts ...WriteOption) error
// Delete removes the record with the corresponding key from the store.
Delete(key string, opts ...DeleteOption) error
// List returns any keys that match, or an empty list with no error if none matched.
List(opts ...ListOption) ([]string, error)
// Close the store
Close() error
2020-04-02 00:27:15 +02:00
// String returns the name of the implementation.
String() string
2019-05-31 01:43:23 +02:00
}
2022-09-30 16:27:07 +02:00
// Record is an item stored or retrieved from a Store.
2019-05-31 01:43:23 +02:00
type Record struct {
2023-04-26 02:16:34 +02:00
// Any associated metadata for indexing
Metadata map[string]interface{} `json:"metadata"`
// The key to store the record
Key string `json:"key"`
// The value within the record
Value []byte `json:"value"`
// Time to expire a record: TODO: change to timestamp
2020-04-09 20:38:43 +02:00
Expiry time.Duration `json:"expiry,omitempty"`
2019-05-31 01:43:23 +02:00
}
2020-12-29 18:50:18 +02:00
func NewStore(opts ...Option) Store {
return NewMemoryStore(opts...)
}