tlcache

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: MIT Imports: 4 Imported by: 1

README

go-ttl-lru

Golang LRU Cache with TTL.

This provide cache in three types:

  • LRU
  • LRU with TTL
  • TTL

Install

go get -u github.com/JamesYYang/go-ttl-lru

Example

Use LRU Cache

import tlcache "github.com/JamesYYang/go-ttl-lru"

cache := tlcache.NewLRUCache(5)
cache.Add(1, "this is test 1")

if v, ok := cache.Get(1); ok {
    log.Printf("get key (%d) success, value: %s", 1, v)
}

Use TTL LRU Cache

import tlcache "github.com/JamesYYang/go-ttl-lru"

cache := tlcache.NewLRUWithTTLCache(5, 5*time.Second)
cache.Add(1, "this is test 1")

if v, ok := cache.Get(1); ok {
    log.Printf("get key (%d) success, value: %s", 1, v)
}
time.Sleep(5 * time.Second)
if v, ok := cache.Get(1); !ok {
    log.Printf("get key (%d) failed", 1)
}

Use TTL Cache

import tlcache "github.com/JamesYYang/go-ttl-lru"

cache := tlcache.NewTTLCache(5, 5*time.Second, true)
cache.Add(1, "this is test 1")

time.Sleep(3 * time.Second)
if v, ok := cache.Get(1); ok {
    log.Printf("get key (%d) success, value: %s", 1, v)
}
time.Sleep(3 * time.Second)
if v, ok := cache.Get(1); ok {
    log.Printf("get key (%d) success, value: %s", 1, v)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewLRUCache

func NewLRUCache(maxEntries int) *Cache

func NewLRUWithTTLCache

func NewLRUWithTTLCache(maxEntries int, expiry time.Duration) *Cache

func NewTTLCache

func NewTTLCache(maxEntries int, expiry time.Duration, updateAgeOnGet bool) *Cache

func (*Cache) Add

func (c *Cache) Add(key Key, value interface{})

func (*Cache) Clear

func (c *Cache) Clear()

func (*Cache) Get

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

func (*Cache) Remove

func (c *Cache) Remove(key Key)

func (*Cache) Size

func (c *Cache) Size() int

type CacheCore

type CacheCore interface {
	// contains filtered or unexported methods
}

type Key

type Key interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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