cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: BSD-2-Clause, BSD-2-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package cache provides interface and implementation of a cache algorithms.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a 'cache map'.

func NewCache

func NewCache(cacher Cacher) *Cache

NewCache creates a new 'cache map'. The cacher is optional and may be nil.

func (*Cache) Capacity

func (r *Cache) Capacity() int

Capacity returns cache capacity.

func (*Cache) Close

func (r *Cache) Close(force bool)

Close closes the 'cache map'. All 'Cache' method is no-op after 'cache map' is closed. All 'cache node' will be evicted from 'cacher'.

If 'force' is true then all 'cache node' will be forcefully released even if the 'node ref' is not zero.

func (*Cache) Delete

func (r *Cache) Delete(ns, key uint64, delFunc func()) bool

Delete removes and ban 'cache node' with the given namespace and key. A banned 'cache node' will never be inserted into the 'cache tree'. Ban only attributed to the particular 'cache node', so when a 'cache node' is recreated it will not be banned.

If delFunc is not nil, then it will be executed if such 'cache node' doesn't exist or once the 'cache node' is released.

Delete return true is such 'cache node' exist.

func (*Cache) Evict

func (r *Cache) Evict(ns, key uint64) bool

Evict evicts 'cache node' with the given namespace and key. This will simply call Cacher.Evict.

Evict return true is such 'cache node' exist.

func (*Cache) EvictAll

func (r *Cache) EvictAll()

EvictAll evicts all 'cache node'. This will simply call Cacher.EvictAll.

func (*Cache) EvictNS

func (r *Cache) EvictNS(ns uint64)

EvictNS evicts 'cache node' with the given namespace. This will simply call Cacher.Evict on all nodes with the given namespace.

func (*Cache) Get

func (r *Cache) Get(ns, key uint64, setFunc func() (size int, value Value)) *Handle

Get gets 'cache node' with the given namespace and key. If cache node is not found and setFunc is not nil, Get() will atomically creates the 'cache node' by calling setFunc. Otherwise Get() will returns nil.

The returned 'cache handle' should be released after use by calling Release method.

func (*Cache) GetStats

func (r *Cache) GetStats() Stats

GetStats returns cache statistics.

func (*Cache) Nodes

func (r *Cache) Nodes() int

Nodes returns number of 'cache node' in the map.

func (*Cache) SetCapacity

func (r *Cache) SetCapacity(capacity int)

SetCapacity sets cache capacity.

func (*Cache) Size

func (r *Cache) Size() int

Size returns sums of 'cache node' size in the map.

type Cacher

type Cacher interface {
	// Capacity returns cache capacity.
	Capacity() int

	// SetCapacity sets cache capacity.
	SetCapacity(capacity int)

	// Promote promotes the 'cache node'.
	Promote(n *Node)

	// Ban evicts the 'cache node' and prevent subsequent 'promote'.
	Ban(n *Node)

	// Evict evicts the 'cache node'.
	Evict(n *Node)
}

Cacher provides interface to implements a caching functionality. An implementation must be safe for concurrent use.

func NewLRU

func NewLRU(capacity int) Cacher

NewLRU create a new LRU-cache.

type Handle

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

Handle is a 'cache handle' of a 'cache node'.

func (*Handle) Release

func (h *Handle) Release()

Release releases this 'cache handle'. It is safe to call release multiple times.

func (*Handle) Value

func (h *Handle) Value() Value

Value returns the value of the 'cache node'.

type NamespaceGetter

type NamespaceGetter struct {
	Cache *Cache
	NS    uint64
}

NamespaceGetter provides convenient wrapper for namespace.

func (*NamespaceGetter) Get

func (g *NamespaceGetter) Get(key uint64, setFunc func() (size int, value Value)) *Handle

Get simply calls Cache.Get() method.

type Node

type Node struct {
	CacheData unsafe.Pointer
	// contains filtered or unexported fields
}

Node is a 'cache node'.

func (*Node) GetHandle

func (n *Node) GetHandle() *Handle

GetHandle returns a handle for this 'cache node'.

func (*Node) Key

func (n *Node) Key() uint64

Key returns this 'cache node' key.

func (*Node) NS

func (n *Node) NS() uint64

NS returns this 'cache node' namespace.

func (*Node) Ref

func (n *Node) Ref() int32

Ref returns this 'cache node' ref counter.

func (*Node) Size

func (n *Node) Size() int

Size returns this 'cache node' size.

func (*Node) Value

func (n *Node) Value() Value

Value returns this 'cache node' value.

type Stats

type Stats struct {
	Buckets     int
	Nodes       int64
	Size        int64
	GrowCount   int32
	ShrinkCount int32
	HitCount    int64
	MissCount   int64
	SetCount    int64
	DelCount    int64
}

type Value

type Value interface{}

Value is a 'cache-able object'. It may implement util.Releaser, if so the Release method will be called once object is released.

Jump to

Keyboard shortcuts

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