Help us shape the future of Go! Take the 2026 Go Developer Survey

ttlru

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2017 License: MIT Imports: 3 Imported by: 0

README

ttlru

GoDoc Circle CI Coverage Status

Package ttlru provides a simple, goroutine safe, cache with a fixed number of entries. Each entry has a per-cache defined TTL. This TTL is reset on both modification and access of the value. As a result, if the cache is full, and no items have expired, when adding a new item, the item with the soonest expiration will be evicted.

It is based on the LRU implementation in golang-lru: github.com/hashicorp/golang-lru

Which in turn is based on the LRU implementation in groupcache: github.com/golang/groupcache/lru

Documentation

Overview

Package ttlru provides a simple, goroutine safe, cache with a fixed number of entries. Each entry has a per-cache defined TTL. This TTL is reset on both modification and access to the value. As a result, if the cache is full, and no items have expired, when adding a new item, the item with the soonest expiration will be evicted.

It is based on the LRU implementation in golang-lru: github.com/hashicorp/golang-lru

Which in turn is based on the LRU implementation in groupcache: github.com/golang/groupcache/lru

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Set a key with value to the cache. Returns true if an item was
	// evicted.
	Set(key, value interface{}) bool

	// Get an item from the cache by key. Returns the value if it exists,
	// and a bool stating whether or not it existed.
	Get(key interface{}) (interface{}, bool)

	// Keys returns a slice of all the keys in the cache
	Keys() []interface{}

	// Len returns the number of items present in the cache
	Len() int

	// Cap returns the total number of items the cache can retain
	Cap() int

	// Purge removes all items from the cache
	Purge()

	// Del deletes an item from the cache by key. Returns if an item was
	// actually deleted.
	Del(key interface{}) bool
}

func New

func New(cap int, opts ...Option) Cache

New creates a new Cache with cap entries that expire after ttl has elapsed since the item was added, modified or accessed.

type Option

type Option func(*cache)

func WithTTL

func WithTTL(val time.Duration) Option

func WithoutReset

func WithoutReset() Option

Jump to

Keyboard shortcuts

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