lru

package
v1.19.3 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2023 License: LGPL-3.0 Imports: 2 Imported by: 0

README

lru

Package lru provides a goroutine safe LRU cache implementation based on "github.com/golang/groupcache/lru".

Usage
// Creates a ready-to-use cache
cache := lru.NewCache(MaxCachedFileNum, MaxCachedSize, func(key, object interface{}) {
	// Jobs to do on evicted
})
// Caches an object
cache.Add(Key, CachedObj, CachedObjSize)
// Gets a cached object
cachedObj, ok := cache.Get(Key)

Documentation

Overview

Package lru provides a goroutine safe LRU cache implementation based on "github.com/golang/groupcache/lru".

Basic example:

// Creates a cache
cache := lru.NewCache(MaxCachedFileNum, MaxCachedSize, func(key, object interface{}) {
	// Jobs to do on evicted
})
// Caches an object
cache.Add(Key, CachedObj, CachedObjSize)
// Gets a cached object
cachedObj, ok := cache.Get(Key)

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 goroutine safe LRU cache base on "github.com/golang/groupcache/lru".

func NewCache

func NewCache(maxEntries int, maxCachedSize int64, onEvicted func(key, object interface{})) *Cache

NewCache creates a ready-to-use Cache.

maxEntries: Limit of cached objects, LRU eviction will be triggered when reached.
maxCachedSize: Limit of total cached objects' size in bytes, LRU eviction will be triggered when reached.
onEvicted: Optionally specificies a callback function to be executed when an entry is purged from the cache.

func (*Cache) Add

func (c *Cache) Add(key, object interface{}, objectSize int64)

Add adds an object to the cache, LRU eviction will be triggered if limit reached after adding.

key: Key of the cached object.
object: Object to be cached.
objectSize: Size in bytes of the cached object.

func (*Cache) Clear

func (c *Cache) Clear()

Clear purges all cached objects from the cache.

func (*Cache) CurCachedSize

func (c *Cache) CurCachedSize() (size int64)

CurCachedSize returns the total cached objects' size in bytes.

func (*Cache) Get

func (c *Cache) Get(key interface{}) (object interface{}, ok bool)

Get looks up a key's object from the cache. It returns true and the object if found, false and nil otherwise.

func (*Cache) Remove

func (c *Cache) Remove(key interface{})

Remove removes a key's object from the cache.

func (*Cache) RemoveCachedObjects

func (c *Cache) RemoveCachedObjects(keys []interface{})

RemoveCachedObjects removes objects specified in `keys` from the cache.

Jump to

Keyboard shortcuts

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