ttlcache

package module
v0.0.0-...-1d0d19f Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 4 Imported by: 0

README

TTLCache - an in-memory LRU cache with expiration

  1. Thread-safe
  2. Auto-Expiring after a certain time
  3. Auto-Extending expiration on Gets
  4. Customizable handlers
  5. Customizable monitoring
Usage
import (
    "fmt"
    "time"

    "github.com/VCisHere/ttlcache"
)

func main() {
    cfg := ttlcache.Config{
        Capacity: 2,
        TTL:      time.Second * 3,
        OnEvicted: func(key, value interface{}, reason ttlcache.EvictReason) {
            fmt.Println(reason.String(), key, value)
        },
        EnableLog:   true,
        LogInterval: time.Second * 1,
    }
    cache := ttlcache.NewCache(cfg)
    cache.Set("key", "value")
    value, exist := cache.Get("key1")
    fmt.Println(value, exist)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Set(key, value interface{})
	Get(key interface{}) (interface{}, bool)
	Remove(key interface{})
	Count() int
	OldestTime() time.Time
}

func NewCache

func NewCache(cfg Config) Cache

type Config

type Config struct {
	Capacity    int
	TTL         time.Duration
	OnEvicted   EvictCallback
	EnableLog   bool
	LogFunc     LogFunc
	LogInterval time.Duration
}

type EvictCallback

type EvictCallback func(key, value interface{}, reason EvictReason)

type EvictReason

type EvictReason int
const (
	Removed EvictReason = iota
	Expired
	OutOfCapacity
)

func (*EvictReason) String

func (reason *EvictReason) String() string

type LogFunc

type LogFunc func(keys, values []interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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