cache

package
v0.0.0-...-65ae5a0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Cache is a cache of documents, used for primary access to documents.

Writes/Deletes to the cache are queued and eventually written/removed to/from the storage via the txQueue.

Reads from the cache are performed directly.

To initialise a new Cache use the NewCache function which accepts a type of Queue.

func NewCache

func NewCache(ctx context.Context, txQueue *Queue) *Cache

NewCache returns a new cache.

func (*Cache) Delete

func (c *Cache) Delete(id string) error

Delete deletes a document from the cache. It will lock the cache and release it when the function returns.

func (*Cache) Filter

func (c *Cache) Filter(collection string, query Query) []*document.Document

Filter filters documents in the cache.

func (*Cache) GetByID

func (c *Cache) GetByID(id string) *document.Document

GetByID gets a document from the cache by ID. It will lock the cache and release it when the function returns.

func (*Cache) Put

func (c *Cache) Put(d *document.Document, blackhole bool) error

Put puts a document into the cache.

type Condition

type Condition struct {
	Field    string   `json:"field"`
	Operator Operator `json:"operator"`
	Value    string   `json:"value"`
}

Condition is a condition to use in a query.

type Element

type Element struct {
	Condition *Condition
	Query     *Query
}

func (*Element) UnmarshalJSON

func (e *Element) UnmarshalJSON(data []byte) error

UnmarshalJSON overrides the default UnmarshalJSON for Element

type Event

type Event struct {
	// Operation is the operation that was performed on the document.
	Operation Operation
	// Document is the document that was written to storage.
	Document *document.Document
}

Event is an event that is emitted when a document is written to storage.

type Operation

type Operation int

Operation is the type of operation to perform on a document.

const (
	// OperationCreate creates a document.
	OperationCreate Operation = iota
	// OperationUpdate updates a document.
	OperationUpdate
	// OperationDelete deletes a document.
	OperationDelete
)

type Operator

type Operator string

Operator is the type of operator to use in a condition.

const (
	Equals   Operator = "equals"
	Contains Operator = "contains"
)

type Query

type Query struct {
	And []Element `json:"and,omitempty"`
	Or  []Element `json:"or,omitempty"`
}

type Queue

type Queue struct {
	Storage storage.Storage

	sync.RWMutex
	// contains filtered or unexported fields
}

Queue is a queue of events to be processed by the cache so they can eventually be written to the storage.

If the maximum number of attempts is reached, the event is discarded.

The Queue will finish processing all events when receiving cancel signal and stop safely.

func NewQueue

func NewQueue(storage storage.Storage) *Queue

NewQueue returns a new queue.

func (*Queue) Push

func (q *Queue) Push(event Event)

Push pushes a write event to the queue.

func (*Queue) Start

func (q *Queue) Start(ctx context.Context)

Start starts the queue.

func (*Queue) WaitForShutdown

func (q *Queue) WaitForShutdown()

WaitForShutdown waits for the queue to finish processing all events.

Jump to

Keyboard shortcuts

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