expirablelru

package module
v0.0.0-...-d453369 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: MPL-2.0 Imports: 4 Imported by: 0

README

expirablelru

expirablelru implements a TTL expiring LRU cache based on the one at https://github.com/hashicorp/golang-lru/pull/68 which in turn is based on https://github.com/hashicorp/golang-lru. All thanks go to @paskal and the hashicorp team.

Only difference is the addition of the AddWithTTL method which allows users to set a TTL per-item.

Documentation

Overview

Package expirablelru implements a TTL expiring LRU cache based on the one at https://github.com/hashicorp/golang-lru/pull/68.

Only difference is the addition of the AddWithTTL method which allows users to set a TTL per-item.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Cache implements a thread safe LRU with expirable entries.

func NewExpirableLRU

func NewExpirableLRU(size int, onEvict EvictCallback, ttl, purgeEvery time.Duration) *Cache

NewExpirableLRU returns a new cache with expirable entries.

Size parameter set to 0 makes cache of unlimited size.

Providing 0 TTL turns expiring off.

Activates deleteExpired by purgeEvery duration. If MaxKeys and TTL are defined and PurgeEvery is zero, PurgeEvery will be set to 5 minutes.

func (*Cache) Add

func (c *Cache) Add(key, value interface{}) (evicted bool)

Add adds a key and a value to the LRU interface

func (*Cache) AddWithTTL

func (c *Cache) AddWithTTL(key, value interface{}, ttl time.Duration) (evicted bool)

AddWithTTL adds a key and a value with a TTL to the LRU interface

func (*Cache) Close

func (c *Cache) Close()

Close cleans the cache and destroys running goroutines

func (*Cache) Contains

func (c *Cache) Contains(key interface{}) (ok bool)

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*Cache) DeleteExpired

func (c *Cache) DeleteExpired()

DeleteExpired clears cache of expired items

func (*Cache) Get

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

Get returns the key value

func (*Cache) GetOldest

func (c *Cache) GetOldest() (key, value interface{}, ok bool)

GetOldest returns the oldest entry

func (*Cache) Keys

func (c *Cache) Keys() []interface{}

Keys returns a slice of the keys in the cache, from oldest to newest.

func (*Cache) Len

func (c *Cache) Len() int

Len return count of items in cache

func (*Cache) Peek

func (c *Cache) Peek(key interface{}) (interface{}, bool)

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

func (*Cache) Purge

func (c *Cache) Purge()

Purge clears the cache completely.

func (*Cache) Remove

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

Remove key from the cache

func (*Cache) RemoveOldest

func (c *Cache) RemoveOldest() (key, value interface{}, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*Cache) Resize

func (c *Cache) Resize(size int) (evicted int)

Resize changes the cache size. size 0 doesn't resize the cache, as it means unlimited.

type EvictCallback

type EvictCallback func(key interface{}, value interface{})

EvictCallback is used to get a callback when a cache entry is evicted

Jump to

Keyboard shortcuts

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