memory

package
v2.25.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContext

func NewContext(ctx context.Context, storeParams map[string]interface{}) context.Context

NewContext prepares 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.

func NewMemStore

func NewMemStore(opts ...store.Option) store.Store

NewMemStore creates a new MemStore instance

func NewMultiMemStore

func NewMultiMemStore(opts ...store.Option) store.Store

NewMultiMemStore creates 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.

Types

type MemStore

type MemStore struct {
	// contains filtered or unexported fields
}

MemStore is a in-memory store implementation using radix tree for fast prefix and suffix searches. Insertions are expected to be a bit slow due to the data structures, but searches are expected to be fast, including exact key search, as well as prefix and suffix searches (based on the number of elements to be returned). Prefix+suffix search isn't optimized and will depend on how many items we need to skip. It's also recommended to use reasonable limits when using prefix or suffix searches because we'll need to traverse the data structures to provide the results. The traversal will stop a soon as we have the required number of results, so it will be faster if we use a short limit.

The overall performance will depend on how the radix trees are built. The number of elements won't directly affect the performance but how the keys are dispersed. The more dispersed the keys are, the faster the search will be, regardless of the number of keys. This happens due to the number of hops we need to do to reach the target element. This also mean that if the keys are too similar, the performance might be slower than expected even if the number of elements isn't too big.

func (*MemStore) Close

func (m *MemStore) Close() error

Close closes the store

func (*MemStore) Delete

func (m *MemStore) Delete(key string, opts ...store.DeleteOption) error

Delete removes the record based on the key. It won't return any error if it's missing

Database and Table options aren't supported

func (*MemStore) Init

func (m *MemStore) Init(opts ...store.Option) error

Init initializes the MemStore. If the MemStore was used, this will reset all the internal structures and the new options (passed as parameters) will be used.

func (*MemStore) Len

func (m *MemStore) Len() (int, bool)

Len returns the number of items in the store

func (*MemStore) List

func (m *MemStore) List(opts ...store.ListOption) ([]string, error)

List the keys currently used in the MemStore

All options are supported except Database and Table

For prefix and prefix+suffix options, the keys will be returned in alphabetical order. For the suffix option (just suffix, no prefix), the keys will be returned in alphabetical order after reversing the keys. This means, reverse all the keys and then sort them alphabetically. This just affects the sorting order; the keys will be returned as expected. This means that ["aboz", "caaz", "ziuz"] will be sorted as ["caaz", "aboz", "ziuz"]

func (*MemStore) Options

func (m *MemStore) Options() store.Options

Options returns the options being used

func (*MemStore) Read

func (m *MemStore) Read(key string, opts ...store.ReadOption) ([]*store.Record, error)

Read the key from the MemStore. A list of records will be returned even if you're asking for the exact key (only one record is expected in that case).

Reading the exact element will move such element to the last position of the eviction list. This WON'T apply for prefix and / or suffix reads.

This method guarantees that no expired element will be returned. For the case of exact read, the element will be removed and a "not found" error will be returned. For prefix and suffix reads, all the elements that we traverse through will be removed. This includes the elements we need to skip as well as the elements that might have gotten into the the result. Note that the elements that are over the limit won't be touched

All read options are supported except Database and Table.

For prefix and prefix+suffix options, the records will be returned in alphabetical order on the keys. For the suffix option (just suffix, no prefix), the records will be returned in alphabetical order after reversing the keys. This means, reverse all the keys and then sort them alphabetically. This just affects the sorting order; the keys will be returned as expected. This means that ["aboz", "caaz", "ziuz"] will be sorted as ["caaz", "aboz", "ziuz"] for the key "z" as suffix.

Note that offset are supported but not recommended. There is no direct access to the record X. We'd need to skip all the records until we reach the specified offset, which could be problematic. Performance for prefix and suffix searches should be good assuming we limit the number of results we need to return.

func (*MemStore) String

func (m *MemStore) String() string

String returns the name of the store implementation

func (*MemStore) Write

func (m *MemStore) Write(r *store.Record, opts ...store.WriteOption) error

Write the record in the MemStore. Note that Database and Table options will be ignored. Expiration options will take the following precedence: TTL option > expiration option > TTL record

New elements will take the last position in the eviction list. Updating an element will also move the element to the last position.

Although not recommended, new elements might be inserted with an already-expired date

type MultiMemStore

type MultiMemStore struct {
	// contains filtered or unexported fields
}

MultiMemStore is a in-memory store implementation using multiple MemStore to provide support for multiple databases and tables. Each table will be mapped to its own MemStore, which will be completely isolated from the rest. In particular, each MemStore will have its own capacity, so it's possible to have 10 MemStores with full capacity (512 by default)

The options will be the same for all MemStores unless they're explicitly initialized otherwise.

Since each MemStore is isolated, the required synchronization caused by concurrency will be minimal if the threads use different tables

func (*MultiMemStore) Close

func (m *MultiMemStore) Close() error

Close closes the store

func (*MultiMemStore) Delete

func (m *MultiMemStore) Delete(key string, opts ...store.DeleteOption) error

Delete the matching records in the target MemStore based on the Database and Table values from the options. A default MemStore will be used if no Database and Table options are provided.

Matching records from other Tables won't be affected. In fact, we won't access to other Tables

func (*MultiMemStore) Init

func (m *MultiMemStore) Init(opts ...store.Option) error

Init initializes the mapped MemStore based on the Database and Table values from the options with the same options. The target MemStore will be reinitialized if needed.

func (*MultiMemStore) List

func (m *MultiMemStore) List(opts ...store.ListOption) ([]string, error)

List the keys in the target MemStore based on the Database and Table values from the options. A default MemStore will be used if no Database and Table options are provided. The list options will be forwarded to the target MemStore.

func (*MultiMemStore) Options

func (m *MultiMemStore) Options() store.Options

Options returns the options used to create the MultiMemStore. Specific options for each MemStore aren't available

func (*MultiMemStore) Read

func (m *MultiMemStore) Read(key string, opts ...store.ReadOption) ([]*store.Record, error)

Read the matching records in the target MemStore based on the Database and Table values from the options. A default MemStore will be used if no Database and Table options are provided. The read options will be forwarded to the target MemStore.

The expectations regarding the results (sort order, eviction policies, etc) will be the same as the target MemStore

func (*MultiMemStore) String

func (m *MultiMemStore) String() string

String returns the name of the store implementation

func (*MultiMemStore) Write

func (m *MultiMemStore) Write(r *store.Record, opts ...store.WriteOption) error

Write the record in the target MemStore based on the Database and Table values from the options. A default MemStore will be used if no Database and Table options are provided. The write options will be forwarded to the target MemStore

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL