package cache
import "github.com/syndtr/goleveldb/leveldb/cache"
Package cache provides interface and implementation of a cache algorithms.
Index ¶
- type Cache
- func NewCache(cacher Cacher) *Cache
- func (r *Cache) Capacity() int
- func (r *Cache) Close() error
- func (r *Cache) CloseWeak() error
- func (r *Cache) Delete(ns, key uint64, onDel func()) bool
- func (r *Cache) Evict(ns, key uint64) bool
- func (r *Cache) EvictAll()
- func (r *Cache) EvictNS(ns uint64)
- func (r *Cache) Get(ns, key uint64, setFunc func() (size int, value Value)) *Handle
- func (r *Cache) Nodes() int
- func (r *Cache) SetCapacity(capacity int)
- func (r *Cache) Size() int
- type Cacher
- type Handle
- type NamespaceGetter
- type Node
- func (n *Node) GetHandle() *Handle
- func (n *Node) Key() uint64
- func (n *Node) NS() uint64
- func (n *Node) Ref() int32
- func (n *Node) Size() int
- func (n *Node) Value() Value
- type Value
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a 'cache map'.
func NewCache ¶
NewCache creates a new 'cache map'. The cacher is optional and may be nil.
func (*Cache) Capacity ¶
Capacity returns cache capacity.
func (*Cache) Close ¶
Close closes the 'cache map' and forcefully releases all 'cache node'.
func (*Cache) CloseWeak ¶
CloseWeak closes the 'cache map' and evict all 'cache node' from cacher, but unlike Close it doesn't forcefully releases 'cache node'.
func (*Cache) Delete ¶
Delete removes and ban 'cache node' with the given namespace and key. A banned 'cache node' will never 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 onDel 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 ¶
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 ¶
EvictNS evicts 'cache node' with the given namespace. This will simply call Cacher.EvictNS.
func (*Cache) Get ¶
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) Nodes ¶
Nodes returns number of 'cache node' in the map.
func (*Cache) SetCapacity ¶
SetCapacity sets cache capacity.
func (*Cache) Size ¶
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) // EvictNS evicts 'cache node' with the given namespace. EvictNS(ns uint64) // EvictAll evicts all 'cache node'. EvictAll() // Close closes the 'cache tree' Close() error }
Cacher provides interface to implements a caching functionality. An implementation must be safe for concurrent use.
func NewLRU ¶
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 ¶
Value returns the value of the 'cache node'.
type NamespaceGetter ¶
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 ¶
Node is a 'cache node'.
func (*Node) GetHandle ¶
GetHandle returns an handle for this 'cache node'.
func (*Node) Key ¶
Key returns this 'cache node' key.
func (*Node) NS ¶
NS returns this 'cache node' namespace.
func (*Node) Ref ¶
Ref returns this 'cache node' ref counter.
func (*Node) Size ¶
Size returns this 'cache node' size.
func (*Node) Value ¶
Value returns this 'cache node' value.
type Value ¶
type Value interface{}
Value is a 'cacheable object'. It may implements util.Releaser, if so the the Release method will be called once object is released.
Source Files ¶
- Version
- v1.0.0 (latest)
- Published
- Feb 22, 2019
- Platform
- linux/amd64
- Imports
- 4 packages
- Last checked
- 2 months ago –
Tools for package owners.