lruttl

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2020 License: Apache-2.0 Imports: 4 Imported by: 2

README

lru-ttl

Build Status

LRU cache with ttl. It's useful for short ttl cache.

cache := New(1000, 60 * time.Second)
cache.Add("tree.xie", "my data")
data, ok := cache.Get("tree.xie")
cache.Remove("tree.xie")
cache.Add("tree.xie", "my data", time.Second)

Documentation

Overview

Package lru implements an LRU cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	MaxEntries int
	TTL        time.Duration
	// contains filtered or unexported fields
}

func New

func New(maxEntries int, defaultTTL time.Duration) *Cache

New creates a new cache with rw mutex

func NewWithoutRWMutex added in v0.1.0

func NewWithoutRWMutex(maxEntries int, defaultTTL time.Duration) *Cache

NewWithoutRWMutex create a new cache without rw mutex

func (*Cache) Add

func (c *Cache) Add(key Key, value interface{}, ttl ...time.Duration)

Add adds a value to the cache.

func (*Cache) ForEach added in v0.2.0

func (c *Cache) ForEach(fn func(key Key, value interface{}))

ForEach for each function

func (*Cache) Get

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

Get gets a key's value from the cache.

func (*Cache) Keys added in v0.2.0

func (c *Cache) Keys() []Key

Keys get all keys of cache

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache.

func (*Cache) Remove

func (c *Cache) Remove(key Key)

Remove removes the key's value from the cache.

type Key added in v0.2.0

type Key interface{}

A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators

type LRUCache added in v0.2.0

type LRUCache struct {
	// MaxEntries is the maximum number of cache entries before
	// an item is evicted. Zero means no limit.
	MaxEntries int

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

LRUCache is an LRU cache. It is not safe for concurrent access.

func NewLRU added in v0.2.0

func NewLRU(maxEntries int) *LRUCache

New creates a new LRUCache. If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller.

func (*LRUCache) Add added in v0.2.0

func (c *LRUCache) Add(key Key, value interface{})

Add adds a value to the cache.

func (*LRUCache) Clear added in v0.2.0

func (c *LRUCache) Clear()

Clear purges all stored items from the cache.

func (*LRUCache) ForEach added in v0.2.0

func (c *LRUCache) ForEach(fn func(key Key, value interface{}))

ForEach for each function

func (*LRUCache) Get added in v0.2.0

func (c *LRUCache) Get(key Key) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (*LRUCache) Len added in v0.2.0

func (c *LRUCache) Len() int

Len returns the number of items in the cache.

func (*LRUCache) Remove added in v0.2.0

func (c *LRUCache) Remove(key Key)

Remove removes the provided key from the cache.

func (*LRUCache) RemoveOldest added in v0.2.0

func (c *LRUCache) RemoveOldest()

RemoveOldest removes the oldest item from the cache.

Jump to

Keyboard shortcuts

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