db

package
v0.0.0-...-4706bde Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package db package defines interfaces to implement a caching database. It also custom storage backends such as skip lists. The caching server supports multiple storage backends along with different eviction policies. So it is important to provide a shared behavior for the storage itself and the policies. To implement a caching strategy (i.e.: db + policy), one will have to define a Database and a CachePolicy structs which implement the relevant interfaces respectively. They will then need to define a third structure which combines both. For example, a Cache server using LRU policy with HashMap as backend. It is also possible to just define one structure which implements all the required interfaces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CMap

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

func NewCmap

func NewCmap(size int) *CMap

func (*CMap) DeleteEntry

func (c *CMap) DeleteEntry(key string) int

func (*CMap) GetBucket

func (c *CMap) GetBucket(key string) map[string]*Entry

type Cache

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

Cache is the storage of our cache server. For now, we apply global lock for simplicity. Later on, we could average RWLocks and fine-grained locking strategy. This means that there is no need for mutexes at the eviction struct side.

func NewCache

func NewCache(maxItem int64, evictionPolicyType string) (*Cache, error)

func (*Cache) Delete

func (c *Cache) Delete(keys ...string) int

Delete delete keys and return the number of removed keys

func (*Cache) Get

func (c *Cache) Get(key string) (string, bool)

Get retrieves the value associated with the provided key from the cache. It returns the value along with a boolean flag indicating if the value was found in the cache or not.

func (*Cache) Set

func (c *Cache) Set(key string, value string)

func (*Cache) Size

func (c *Cache) Size() int64

Size returns the current size of the cache. This size is not computed to reduce costs on system calls due to locking/unlocking mutexes. We use an atomic variable instead.

type Entry

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

func NewEntry

func NewEntry(key string, value string) *Entry

type Eviction

type Eviction interface {
	// Refresh refreshes an existing elements
	Refresh(key string)

	// Evict evicts an item and return it key.
	// An error is returned if the key is not found.
	// This method should not return an error.
	Evict() string

	// Add a new element not already managed by this policy
	Add(key string)

	// Delete remove an element from the policy metadata.
	Delete(key string)
}

Eviction defines how entries are evicted.

func CreateEvictionPolicy

func CreateEvictionPolicy(evictionType string) (Eviction, error)

CreateEvictionPolicy is a factory method for eviction policies.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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