tlru

package module
v0.0.0-...-3e18ab8 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 7 Imported by: 4

README

TLRU

Most conventional LRUs maintain a link list of nodes and when user want to expire a node they take the last node fromt he list and evict it from the list. However there is no implmentation for time based expiry of nodes. Even in algorithms where time based expiry is supported each node holds a time field and it needs to be evaluated to see which one has expired. In TLRU we support time based expiry without maintaining time in each individual element.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheNode

type CacheNode struct {
	// Mutext to safeguard this node
	sync.RWMutex

	// Key of the node
	Key string

	// Actual data stored by user
	Value []byte

	// State of the node depicting its in cache or disk
	State NodeState
}

type DiskCacheConfig

type DiskCacheConfig struct {
	// Timeout to evict the nodes which are not used for given interval
	Timeout uint32

	// Number of nodes allowed in the list
	MaxNodes uint32

	// Path where persistent data will be stored
	DiskPath string
	// contains filtered or unexported fields
}

type DualCache

type DualCache struct {
	// Lock to save the map of keys
	sync.RWMutex

	// Config of this dual cache
	DualCacheConfig
	// contains filtered or unexported fields
}

func NewDualCache

func NewDualCache(cfg DualCacheConfig) (*DualCache, error)

NewDualCache : Method to create object of dual cache

func (*DualCache) Add

func (dlc *DualCache) Add(key string, value []byte) error

Add : Add a new node to the cache

func (*DualCache) Get

func (dlc *DualCache) Get(key string) []byte

Get : Get a node from the cache

func (*DualCache) Remove

func (dlc *DualCache) Remove(key string) error

Stop : Stop the cache

func (*DualCache) Start

func (dlc *DualCache) Start() (err error)

Start : Start the Cache

func (*DualCache) Stop

func (dlc *DualCache) Stop() (err error)

Stop : Stop the cache

type DualCacheConfig

type DualCacheConfig struct {
	// Config for memory based caching
	MemConfig MemCacheconfig

	// Config for disk based caching
	DiskConfig DiskCacheConfig

	// Callback Function to write data to disk
	Log func(string)
}

DualCacheConfig : Config params for dual-cache

type MemCacheconfig

type MemCacheconfig struct {
	// Timeout to evict the nodes which are not used for given interval
	Timeout uint32

	// Number of nodes allowed in the list
	MaxNodes uint32
	// contains filtered or unexported fields
}

type NodeState

type NodeState int
const (
	NODE_FREE NodeState = iota
	NODE_CACHED
	NODE_PERSISTED
	NODE_DELETED
)

type TLRU

type TLRU struct {
	// Lock for the TLRU
	sync.Mutex

	// Timeout to evict the nodes which are not used for given interval
	Timeout uint32

	// Timeout to callback application to check if it wants to evict last node
	AppCheckTimeout uint32

	// Number of nodes allowed in the list
	MaxNodes uint32

	// Function called back for eviceted nodes
	Evict func(*list.Element)

	// Function to check if application wants to force evict last node
	AppCheck func() bool
	// contains filtered or unexported fields
}

func New

func New(maxNodes, timeout uint32, evict func(*list.Element), appchecktimeout uint32, appcheck func() bool) (*TLRU, error)

NewTLRU : Create a new Time based LRU

func (*TLRU) Add

func (tlru *TLRU) Add(data any) *list.Element

Add : Add this new node to the lru

func (*TLRU) Refresh

func (tlru *TLRU) Refresh(node *list.Element)

RefreshNode : Request to mark this node as used

func (*TLRU) Remove

func (tlru *TLRU) Remove(node *list.Element)

Remove : Delete this new node from the lru

func (*TLRU) Start

func (tlru *TLRU) Start() error

Start : Init the LRU and start the watchDog thread

func (*TLRU) Stop

func (tlru *TLRU) Stop() error

Stop : Stop the watchdog thread and expire all nodes

Jump to

Keyboard shortcuts

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