lrucache

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: Apache-2.0 Imports: 2 Imported by: 3

Documentation

Overview

Package lrucache provides reference-count-aware lru cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {

	// OnEvicted optionally specifies a callback function to be
	// executed when an entry is purged from the cache.
	OnEvicted func(key string, value interface{})
	// contains filtered or unexported fields
}

Cache is "groupcache/lru"-like cache. The difference is that "groupcache/lru" immediately finalizes theevicted contents using OnEvicted callback but our version strictly tracks the reference counts of contents and calls OnEvicted when nobody refers to the evicted contents.

func New

func New(maxEntries int) *Cache

New creates new cache.

func (*Cache) Add

func (c *Cache) Add(key string, value interface{}) (cachedValue interface{}, done func(), added bool)

Add adds object to the cache and returns the cached contents with incrementing the reference count. If the specified content already exists in the cache, this sets `added` to false and returns "already cached" content (i.e. doesn't replace the content with the new one). Client must call `done` callback to decrease the counter when the value will no longer be used.

func (*Cache) Get

func (c *Cache) Get(key string) (value interface{}, done func(), ok bool)

Get retrieves the specified object from the cache and increments the reference counter of the target content. Client must call `done` callback to decrease the reference count when the value will no longer be used.

func (*Cache) Remove

func (c *Cache) Remove(key string)

Remove removes the specified contents from the cache. OnEvicted callback will be called when nobody refers to the removed content.

Jump to

Keyboard shortcuts

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