lrustore

package module
v0.0.0-...-7619f97 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package lrustore provides a datastore wrapper that limits the number of items stored to the specified capacity. When at capacity, the oldest items are removed in LRU to make root for new items. Putting and getting items maintains LRU order.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRUStore

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

LRUStore is a LRU-cache that stores keys in memory and values in a datastore.

func New

func New(ctx context.Context, dstore ds.Datastore, capacity int) (*LRUStore, error)

New creates a new LRUStore instance. The context is only used cancel a call to this function while it is accessing the data store.

func (*LRUStore) Batch

func (ls *LRUStore) Batch(ctx context.Context) (ds.Batch, error)

Batch implements the Batching interface.

func (*LRUStore) Cap

func (ls *LRUStore) Cap() int

Cap returns the LRUStore capacity. Storing more than this number of items results in discarding oldest items.

func (*LRUStore) Clear

func (ls *LRUStore) Clear(ctx context.Context) error

Clear purges all stored items from the LRUStore.

func (*LRUStore) Close

func (ls *LRUStore) Close() error

Close syncs the LRUStore entries but does not close the underlying datastore. This is because LRUStore wraps an existing datastore and does not construct it, and the wrapped datastore may be in use elsewhere.

func (*LRUStore) Delete

func (ls *LRUStore) Delete(ctx context.Context, key ds.Key) error

Delete implements datastore interface.

func (*LRUStore) DiskUsage

func (ls *LRUStore) DiskUsage(ctx context.Context) (uint64, error)

DiskUsage implements the PersistentDatastore interface.

func (*LRUStore) Get

func (ls *LRUStore) Get(ctx context.Context, key ds.Key) ([]byte, error)

Get implements datastore interface.

func (*LRUStore) GetSize

func (ls *LRUStore) GetSize(ctx context.Context, key ds.Key) (int, error)

GetSize implements datastore interface.

func (*LRUStore) Has

func (ls *LRUStore) Has(ctx context.Context, key ds.Key) (bool, error)

Has implements datastore interface. Has determines if the item is present in the LRUStore without moving the item to the newest LRU position.

func (*LRUStore) Len

func (ls *LRUStore) Len() int

Len returns the number of items in the LRUStore

func (*LRUStore) Put

func (ls *LRUStore) Put(ctx context.Context, key ds.Key, val []byte) error

Put implements datastore interface.

func (*LRUStore) Query

func (ls *LRUStore) Query(ctx context.Context, q dsq.Query) (dsq.Results, error)

Query implements datastore interface.

func (*LRUStore) Resize

func (ls *LRUStore) Resize(ctx context.Context, newSize int) (int, error)

Resize changes the LRUStore capacity. If the capacity is decreased below the number of items in the LRUStore, then oldest items are discarded until the LRUStore is filled to the new lower capacity. Returns the number of items evicted from the LRUStore.

func (*LRUStore) SetOnEvicted

func (ls *LRUStore) SetOnEvicted(onEvicted func(context.Context, ds.Key))

SetOnEvicted provides a function to call when an item is being evicted from the LRUStore. The onEvicted function is called before the value is removed from the datastore, so if needed the value can still be looked up. The onEvicted function is called while the LRUStore is exclusively locked, blocking any other cache function. Calling a LRUStore function from withing the onEvicted function will cause a deadlock.

func (*LRUStore) Sync

func (ls *LRUStore) Sync(ctx context.Context, key ds.Key) error

Sync implements datastore interface.

Jump to

Keyboard shortcuts

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