go_lru_ttl_cache

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 4 Imported by: 0

README

Build Status

LRU Cache with TTL support for Golang

LRUCache is an LRU cache written in Go. The cache supports TTL background cleaning.

The LRUCache is thread-safe

Installation

Use a go-get tool (or any go-compatible package manager) to download the cache:

go get github.com/alexions/go-lru-ttl-cache

Import the cache in your go file:

import (
    lrucache "github.com/alexions/go-lru-ttl-cache"
)

It is highly recommended to use tags to lock the cache version for your project.

Configuration

Import and create a Cache instance:

var cache = lrucache.NewLRUCache(lrucache.Configuration())

Configure allows you to configure cache using chainable API:

var cache = lrucache.NewLRUCache(
    lrucache.Configuration().
    SetDefaultTTL(5 * time.Minute).
    SetMaxSize(100)
)

Possible configuration with default values:

  • SetDefaultTTL(ttl time.Duration) - default expiration TTL. Item will be removed after the specified time (default: -1, no expiration)
  • SetMaxSize(size int) - cache storage limitation. After exceeding the limit the LRU item will be removed (default: math.MaxInt32 - 1)
  • SetCleanupInterval(interval time.Duration) - the cleaning interval. All the items with expired TTL will be removed from the cache. Works only with DefaultTTL greater than 0 (default: run every 1 min)
  • SetDeleteCallback(callback func(count int64)) - callback function to get amount of removed items. Runs every Cleanup interval.

Usage

Set/Get

config := Configuration()
cache := NewLRUCache(config)
cache.Set("hello", "world")

value := cache.Get("hello")
fmt.Println(value) // "hello"

Any data can be stored

type User struct {
    login string
    pass  string
}

config := Configuration()
cache := NewLRUCache(config)

cache.Set(1, &User{login: "alexions", pass: "password"})

user := cache.Get(1)
fmt.Println(user.(*User).login)

Checks if item is set:

if _, found := cache.Get("my_item"); !found {
    // Item not found
}

Size

config := Configuration()
cache := NewLRUCache(config)
cache.Set(1, "hello")
cache.Set(2, "world")

fmt.Println(cache.Size()) // 2

Delete

config := Configuration()
cache := NewLRUCache(config)
cache.Set(1, "hello")

cache.Size() // 1
cache.Delete(1)
cache.Size() // 0

Clean

config := Configuration()
cache := NewLRUCache(config)
cache.Set(1, "hello")
cache.Set(2, "world")

cache.Size() // 2
cache.Clean()
cache.Size() // 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigBuilder

type ConfigBuilder struct {
	// contains filtered or unexported fields
}

func Configuration

func Configuration() *ConfigBuilder

func (*ConfigBuilder) SetCleanupInterval

func (b *ConfigBuilder) SetCleanupInterval(interval time.Duration) *ConfigBuilder

func (*ConfigBuilder) SetDefaultTTL

func (b *ConfigBuilder) SetDefaultTTL(ttl time.Duration) *ConfigBuilder

func (*ConfigBuilder) SetDeleteCallback added in v0.1.2

func (b *ConfigBuilder) SetDeleteCallback(callback func(count int64)) *ConfigBuilder

func (*ConfigBuilder) SetMaxSize

func (b *ConfigBuilder) SetMaxSize(size int) *ConfigBuilder

type LRUCache

type LRUCache struct {
	// contains filtered or unexported fields
}

func NewLRUCache

func NewLRUCache(config *ConfigBuilder) *LRUCache

func (*LRUCache) Clean

func (c *LRUCache) Clean()

func (*LRUCache) Delete

func (c *LRUCache) Delete(key interface{})

func (*LRUCache) Get

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

func (*LRUCache) Set

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

func (*LRUCache) Size

func (c *LRUCache) Size() int

Jump to

Keyboard shortcuts

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