ttlcache

package module
v0.0.0-...-603e93f Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2019 License: MIT Imports: 2 Imported by: 0

README

TTLCache - an in-memory LRU cache with expiration

TTLCache is a minimal wrapper over a string map in golang, entries of which are

  1. Thread-safe
  2. Auto-Expiring after a certain time
  3. Auto-Extending expiration on Gets

Build Status

Usage
import (
  "time"
  "github.com/wunderlist/ttlcache"
)

func main () {
  cache := ttlcache.NewCache(ttlcache.Config{
  	Duration: time.Minute * 8,
  	HasTouchLife: true,
  })
  cache.Set("key", "value")
  value, exists := cache.GetString("key")
  count := cache.Count()
  cache.Delete("key")
}

Documentation

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 synchronised map of items that auto-expire once stale

func NewCache

func NewCache(config Config) *Cache

NewCache is a helper to create instance of the Cache struct

func (*Cache) Count

func (cache *Cache) Count() int

Count returns the number of items in the cache (helpful for tracking memory leaks)

func (*Cache) Delete

func (cache *Cache) Delete(key string)

Delete is a thread-safe way to delete items to the map

func (*Cache) Get

func (cache *Cache) Get(key string) (data interface{}, found bool)

Get is a thread-safe way to lookup items

func (*Cache) GetBytes

func (cache *Cache) GetBytes(key string) ([]byte, bool)

func (*Cache) GetInt

func (cache *Cache) GetInt(key string) (int, bool)

func (*Cache) GetString

func (cache *Cache) GetString(key string) (string, bool)

func (*Cache) Set

func (cache *Cache) Set(key string, data interface{})

Set is a thread-safe way to add new items to the map

type Config

type Config struct {
	Duration     time.Duration
	HasTouchLife bool // Every lookup, also touches the item, hence extending it's life
}

type Item

type Item struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Item represents a record in the cache map

Jump to

Keyboard shortcuts

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