lru

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

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

Go to latest
Published: Oct 29, 2019 License: Apache-2.0 Imports: 4 Imported by: 1

README

go-lru Go Report Card Build Status

go-lru is an MIT-licensed Go LRU cache bases on GroupCache, with expire time supported

Example

Set key with expire time

cache := NewCache(100) // max entries in cache is 100
cache.Set("a", 1234, 1) // key "a" would be expired after 1 second

Set key without expire time

cache := NewCache(100)
cache.Set("a", 1234) // set key "a" without expire time

API doc

API documentation is available via https://godoc.org/github.com/git-hulk/go-lru

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 is the maximum number of cache entries before
	// an item is evicted. Zero means no limit.
	MaxEntries int

	// OnEvicted optionally specifics 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
}

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

func NewCache

func NewCache(maxEntries int) *Cache

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

func (*Cache) FlushAll

func (c *Cache) FlushAll()

FlushAll remove all the keys in cache

func (*Cache) Get

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

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

func (*Cache) Keys

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

Keys return all the keys in 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 provided key from the cache.

func (*Cache) Set

func (c *Cache) Set(key Key, value interface{}, args ...interface{}) (bool, error)

Set a value to the cache. Key and value is required, and expire time is option, Set("a", 1) or Set("a", 1, 1) is ok.

func (*Cache) TTL

func (c *Cache) TTL(key Key) int64

TTL return the ttl for key. Return -2 when key is not found, return -1 when key without expire time, return > 0 when key with expire time.

type Key

type Key interface{}

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

Jump to

Keyboard shortcuts

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