gcache

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2025 License: MIT Imports: 7 Imported by: 0

README

GCache - Generics Version

Cache library for golang. It supports expirable Cache, LFU, LRU and ARC.

Features

  • Supports expirable Cache, LFU, LRU and ARC.

  • Goroutine safe.

  • Supports event handlers which evict, purge, and add entry. (Optional)

  • Automatically load cache if it doesn't exists. (Optional)

Install

$ go get github_com/globusdigital/gcache

Example

Manually set a key-value pair.
package main

import (
  "github_com/globusdigital/gcache"
  "fmt"
)

func main() {
  gc := gcache.New[string,string](20).
    LRU().
    Build()
  gc.Set("key", "ok")
  value, err := gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
}
Get: ok
Manually set a key-value pair, with an expiration time.
package main

import (
  "github_com/globusdigital/gcache"
  "fmt"
  "time"
)

func main() {
  gc := gcache.New[string,string](20).
    LRU().
    Build()
  gc.SetWithExpire("key", "ok", time.Second*10)
  value, _ := gc.Get("key")
  fmt.Println("Get:", value)

  // Wait for value to expire
  time.Sleep(time.Second*10)

  value, err := gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
}
Get: ok
// 10 seconds later, new attempt:
panic: ErrKeyNotFound
Automatically load value
package main

import (
  "github_com/globusdigital/gcache"
  "fmt"
)

func main() {
  gc := gcache.New[string,string](20).
    LRU().
    LoaderFunc(func(key string) (string, error) {
      return "ok", nil
    }).
    Build()
  value, err := gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
}
Get: ok
Automatically load value with expiration
package main

import (
  "fmt"
  "time"

  "github_com/globusdigital/gcache"
)

func main() {
  var evictCounter, loaderCounter, purgeCounter int
  gc := gcache.New[string,string](20).
    LRU().
    LoaderExpireFunc(func(key string) (string, *time.Duration, error) {
      loaderCounter++
      expire := 1 * time.Second
      return "ok", &expire, nil
    }).
    EvictedFunc(func(key, value string) {
      evictCounter++
      fmt.Println("evicted key:", key)
    }).
    PurgeVisitorFunc(func(key, value string) {
      purgeCounter++
      fmt.Println("purged key:", key)
    }).
    Build()
  value, err := gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
  time.Sleep(1 * time.Second)
  value, err = gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
  gc.Purge()
  if loaderCounter != evictCounter+purgeCounter {
    panic("bad")
  }
}
Get: ok
evicted key: key
Get: ok
purged key: key

Cache Algorithm

  • Least-Frequently Used (LFU)

Discards the least frequently used items first.

func main() {
  // size: 10
  gc := gcache.New[string,string](10).
    LFU().
    Build()
  gc.Set("key", "value")
}
  • Least Recently Used (LRU)

Discards the least recently used items first.

func main() {
  // size: 10
  gc := gcache.New[string,string](10).
    LRU().
    Build()
  gc.Set("key", "value")
}
  • Adaptive Replacement Cache (ARC)

Constantly balances between LRU and LFU, to improve the combined result.

detail: http://en.wikipedia.org/wiki/Adaptive_replacement_cache

func main() {
  // size: 10
  gc := gcache.New[string,string](10).
    ARC().
    Build()
  gc.Set("key", "value")
}
  • SimpleCache (Default)

SimpleCache has no clear priority for evict cache. It depends on key-value map order.

func main() {
  // size: 10
  gc := gcache.New[string,string](10).Build()
  gc.Set("key", "value")
  v, err := gc.Get("key")
  if err != nil {
    panic(err)
  }
}

Loading Cache

If specified LoaderFunc, values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated.

func main() {
  gc := gcache.New[string,string](10).
    LRU().
    LoaderFunc(func(key string) (string, error) {
      return "value", nil
    }).
    Build()
  v, _ := gc.Get("key")
  // output: "value"
  fmt.Println(v)
}

GCache coordinates cache fills such that only one load in one process of an entire replicated set of processes populates the cache, then multiplexes the loaded value to all callers.

Expirable cache

func main() {
  // LRU cache, size: 10, expiration: after a hour
  gc := gcache.New[string,string](10).
    LRU().
    Expiration(time.Hour).
    Build()
}

Event handlers

Evicted handler

Event handler for evict the entry.

func main() {
  gc := gcache.New[string,string](2).
    EvictedFunc(func(key, value string) {
      fmt.Println("evicted key:", key)
    }).
    Build()
  for i := 0; i < 3; i++ {
    gc.Set(i, i*i)
  }
}
evicted key: 0
Added handler

Event handler for add the entry.

func main() {
  gc := gcache.New[string,string](2).
    AddedFunc(func(key, value string) {
      fmt.Println("added key:", key)
    }).
    Build()
  for i := 0; i < 3; i++ {
    gc.Set(i, i*i)
  }
}
added key: 0
added key: 1
added key: 2

Author

Jun Kimura

Documentation

Index

Constants

View Source
const (
	TYPE_SIMPLE = "simple"
	TYPE_LRU    = "lru"
	TYPE_LFU    = "lfu"
	TYPE_ARC    = "arc"
)

Variables

View Source
var KeyNotFoundError = errors.New("key not found")

Functions

This section is empty.

Types

type ARC

type ARC[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ARC Constantly balances between LRU and LFU, to improve the combined result.

func (*ARC[K, V]) Get

func (c *ARC[K, V]) Get(key K) (V, error)

Get a value from cache pool using key if it exists. If not exists and it has LoaderFunc, it will generate the value using you have specified LoaderFunc method returns value.

func (*ARC[K, V]) GetALL

func (c *ARC[K, V]) GetALL(checkExpired bool) map[K]V

GetALL returns all key-value pairs in the cache.

func (*ARC[K, V]) GetIFPresent

func (c *ARC[K, V]) GetIFPresent(key K) (V, error)

GetIFPresent gets a value from cache pool using key if it exists. If it does not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.

func (*ARC[K, V]) GetIFPresentWithContext added in v1.1.0

func (c *ARC[K, V]) GetIFPresentWithContext(ctx context.Context, key K) (V, error)

func (*ARC[K, V]) GetWithContext added in v1.1.0

func (c *ARC[K, V]) GetWithContext(ctx context.Context, key K) (V, error)

func (*ARC[K, V]) Has

func (c *ARC[K, V]) Has(key K) bool

Has checks if key exists in cache

func (*ARC[K, V]) Keys

func (c *ARC[K, V]) Keys(checkExpired bool) []K

Keys returns a slice of the keys in the cache.

func (*ARC[K, V]) Len

func (c *ARC[K, V]) Len(checkExpired bool) int

Len returns the number of items in the cache.

func (*ARC[K, V]) Purge

func (c *ARC[K, V]) Purge()

Purge is used to completely clear the cache

func (*ARC[K, V]) Remove

func (c *ARC[K, V]) Remove(key K) bool

Remove removes the provided key from the cache.

func (*ARC[K, V]) Set

func (c *ARC[K, V]) Set(key K, value V) error

func (*ARC[K, V]) SetWithExpire

func (c *ARC[K, V]) SetWithExpire(key K, value V, expiration time.Duration) error

SetWithExpire Set a new key-value pair with an expiration time

type AddedFunc

type AddedFunc[K comparable, V any] func(K, V)

type Cache

type Cache[K comparable, V any] interface {
	// Set inserts or updates the specified key-value pair.
	Set(key K, value V) error
	// SetWithExpire inserts or updates the specified key-value pair with an
	// expiration time.
	SetWithExpire(key K, value V, expiration time.Duration) error
	// Get returns the value for the specified key if it is present in the
	// cache. If the key is not present in the cache and the cache has
	// LoaderFunc, invoke the `LoaderFunc` function and inserts the key-value
	// pair in the cache. If the key is not present in the cache and the cache
	// does not have a LoaderFunc, return KeyNotFoundError.
	Get(K) (V, error)
	// GetIFPresent returns the value for the specified key if it is present in
	// the cache. Return KeyNotFoundError if the key is not present.
	GetIFPresent(K) (V, error)
	GetWithContext(context.Context, K) (V, error)
	GetIFPresentWithContext(context.Context, K) (V, error)
	// GetALL returns a map containing all key-value pairs in the cache.
	GetALL(checkExpired bool) map[K]V

	// Remove removes the specified key from the cache if the key is present.
	// Returns true if the key was present and the key has been deleted.
	Remove(key K) bool
	// Purge removes all key-value pairs from the cache.
	Purge()
	// Keys returns a slice containing all keys in the cache.
	Keys(checkExpired bool) []K
	// Len returns the number of items in the cache.
	Len(checkExpired bool) int
	// Has returns true if the key exists in the cache.
	Has(key K) bool
	// contains filtered or unexported methods
}

type CacheBuilder

type CacheBuilder[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K comparable, V any](size int) *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) ARC

func (cb *CacheBuilder[K, V]) ARC() *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) AddedFunc

func (cb *CacheBuilder[K, V]) AddedFunc(addedFunc AddedFunc[K, V]) *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) Build

func (cb *CacheBuilder[K, V]) Build() Cache[K, V]

func (*CacheBuilder[K, V]) Clock

func (cb *CacheBuilder[K, V]) Clock(clock Clock) *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) DeserializeFunc

func (cb *CacheBuilder[K, V]) DeserializeFunc(deserializeFunc DeserializeFunc[K, V]) *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) EvictType

func (cb *CacheBuilder[K, V]) EvictType(tp string) *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) EvictedFunc

func (cb *CacheBuilder[K, V]) EvictedFunc(evictedFunc EvictedFunc[K, V]) *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) Expiration

func (cb *CacheBuilder[K, V]) Expiration(expiration time.Duration) *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) LFU

func (cb *CacheBuilder[K, V]) LFU() *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) LRU

func (cb *CacheBuilder[K, V]) LRU() *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) LoaderExpireFunc

func (cb *CacheBuilder[K, V]) LoaderExpireFunc(loaderExpireFunc LoaderExpireFunc[K, V]) *CacheBuilder[K, V]

LoaderExpireFunc Set a loader function with expiration. loaderExpireFunc: create a new value with this function if cached value is expired. If nil returned instead of time.Duration from loaderExpireFunc than value will never expire.

func (*CacheBuilder[K, V]) LoaderFunc

func (cb *CacheBuilder[K, V]) LoaderFunc(loaderFunc LoaderFunc[K, V]) *CacheBuilder[K, V]

LoaderFunc Set a loader function. loaderFunc: create a new value with this function if cached value is expired.

func (*CacheBuilder[K, V]) PurgeVisitorFunc

func (cb *CacheBuilder[K, V]) PurgeVisitorFunc(purgeVisitorFunc PurgeVisitorFunc[K, V]) *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) SerializeFunc

func (cb *CacheBuilder[K, V]) SerializeFunc(serializeFunc SerializeFunc[K, V]) *CacheBuilder[K, V]

func (*CacheBuilder[K, V]) Simple

func (cb *CacheBuilder[K, V]) Simple() *CacheBuilder[K, V]

type Clock

type Clock interface {
	Now() time.Time
}

func NewRealClock

func NewRealClock() Clock

type DeserializeFunc

type DeserializeFunc[K comparable, V any] func(K, V) (V, error)

type EvictedFunc

type EvictedFunc[K comparable, V any] func(K, V)

type FakeClock

type FakeClock interface {
	Clock

	Advance(d time.Duration)
}

func NewFakeClock

func NewFakeClock() FakeClock

type Group

type Group[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Group represents a class of work and forms a namespace in which units of work can be executed with duplicate suppression.

func (*Group[K, V]) Do

func (g *Group[K, V]) Do(key K, fn func() (V, error), isWait bool) (V, bool, error)

Do executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results.

type LFUCache

type LFUCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LFUCache Discards the least frequently used items first.

func (*LFUCache[K, V]) Get

func (c *LFUCache[K, V]) Get(key K) (V, error)

Get a value from cache pool using key if it exists. If it does not exists key and has LoaderFunc, generate a value using `LoaderFunc` method returns value.

func (*LFUCache[K, V]) GetALL

func (c *LFUCache[K, V]) GetALL(checkExpired bool) map[K]V

GetALL returns all key-value pairs in the cache.

func (*LFUCache[K, V]) GetIFPresent

func (c *LFUCache[K, V]) GetIFPresent(key K) (V, error)

GetIFPresent gets a value from cache pool using key if it exists. If it does not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.

func (*LFUCache[K, V]) GetIFPresentWithContext added in v1.1.0

func (c *LFUCache[K, V]) GetIFPresentWithContext(ctx context.Context, key K) (V, error)

func (*LFUCache[K, V]) GetWithContext added in v1.1.0

func (c *LFUCache[K, V]) GetWithContext(ctx context.Context, key K) (V, error)

func (*LFUCache[K, V]) Has

func (c *LFUCache[K, V]) Has(key K) bool

Has checks if key exists in cache

func (*LFUCache[K, V]) Keys

func (c *LFUCache[K, V]) Keys(checkExpired bool) []K

Keys returns a slice of the keys in the cache.

func (*LFUCache[K, V]) Len

func (c *LFUCache[K, V]) Len(checkExpired bool) int

Len returns the number of items in the cache.

func (*LFUCache[K, V]) Purge

func (c *LFUCache[K, V]) Purge()

Completely clear the cache

func (*LFUCache[K, V]) Remove

func (c *LFUCache[K, V]) Remove(key K) bool

Remove removes the provided key from the cache.

func (*LFUCache[K, V]) Set

func (c *LFUCache[K, V]) Set(key K, value V) error

Set a new key-value pair

func (*LFUCache[K, V]) SetWithExpire

func (c *LFUCache[K, V]) SetWithExpire(key K, value V, expiration time.Duration) error

SetWithExpire Set a new key-value pair with an expiration time

type LRUCache

type LRUCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRUCache Discards the least recently used items first.

func (*LRUCache[K, V]) Get

func (c *LRUCache[K, V]) Get(key K) (V, error)

Get a value from cache pool using key if it exists. If it does not exists key and has LoaderFunc, generate a value using `LoaderFunc` method returns value.

func (*LRUCache[K, V]) GetALL

func (c *LRUCache[K, V]) GetALL(checkExpired bool) map[K]V

GetALL returns all key-value pairs in the cache.

func (*LRUCache[K, V]) GetIFPresent

func (c *LRUCache[K, V]) GetIFPresent(key K) (V, error)

GetIFPresent gets a value from cache pool using key if it exists. If it does not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.

func (*LRUCache[K, V]) GetIFPresentWithContext added in v1.1.0

func (c *LRUCache[K, V]) GetIFPresentWithContext(ctx context.Context, key K) (V, error)

func (*LRUCache[K, V]) GetWithContext added in v1.1.0

func (c *LRUCache[K, V]) GetWithContext(ctx context.Context, key K) (V, error)

func (*LRUCache[K, V]) Has

func (c *LRUCache[K, V]) Has(key K) bool

Has checks if key exists in cache

func (*LRUCache[K, V]) Keys

func (c *LRUCache[K, V]) Keys(checkExpired bool) []K

Keys returns a slice of the keys in the cache.

func (*LRUCache[K, V]) Len

func (c *LRUCache[K, V]) Len(checkExpired bool) int

Len returns the number of items in the cache.

func (*LRUCache[K, V]) Purge

func (c *LRUCache[K, V]) Purge()

Purge Completely clear the cache

func (*LRUCache[K, V]) Remove

func (c *LRUCache[K, V]) Remove(key K) bool

Remove removes the provided key from the cache.

func (*LRUCache[K, V]) Set

func (c *LRUCache[K, V]) Set(key K, value V) error

Set set a new key-value pair

func (*LRUCache[K, V]) SetWithExpire

func (c *LRUCache[K, V]) SetWithExpire(key K, value V, expiration time.Duration) error

SetWithExpire Set a new key-value pair with an expiration time

type LoaderExpireFunc

type LoaderExpireFunc[K comparable, V any] func(context.Context, K) (V, *time.Duration, error)

type LoaderFunc

type LoaderFunc[K comparable, V any] func(context.Context, K) (V, error)

type PurgeVisitorFunc

type PurgeVisitorFunc[K comparable, V any] func(K, V)

type RealClock

type RealClock struct{}

func (RealClock) Now

func (rc RealClock) Now() time.Time

type SerializeFunc

type SerializeFunc[K comparable, V any] func(K, V) (V, error)

type SimpleCache

type SimpleCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SimpleCache has no clear priority for evict cache. It depends on key-value map order.

func (*SimpleCache[K, V]) Get

func (c *SimpleCache[K, V]) Get(key K) (V, error)

Get a value from cache pool using key if it exists. If it does not exists key and has LoaderFunc, generate a value using `LoaderFunc` method returns value.

func (*SimpleCache[K, V]) GetALL

func (c *SimpleCache[K, V]) GetALL(checkExpired bool) map[K]V

GetALL returns all key-value pairs in the cache.

func (*SimpleCache[K, V]) GetIFPresent

func (c *SimpleCache[K, V]) GetIFPresent(key K) (V, error)

GetIFPresent gets a value from cache pool using key if it exists. If it does not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.

func (*SimpleCache[K, V]) GetIFPresentWithContext added in v1.1.0

func (c *SimpleCache[K, V]) GetIFPresentWithContext(ctx context.Context, key K) (V, error)

func (*SimpleCache[K, V]) GetWithContext added in v1.1.0

func (c *SimpleCache[K, V]) GetWithContext(ctx context.Context, key K) (V, error)

func (*SimpleCache[K, V]) Has

func (c *SimpleCache[K, V]) Has(key K) bool

Has checks if key exists in cache

func (*SimpleCache[K, V]) Keys

func (c *SimpleCache[K, V]) Keys(checkExpired bool) []K

Keys returns a slice of the keys in the cache.

func (*SimpleCache[K, V]) Len

func (c *SimpleCache[K, V]) Len(checkExpired bool) int

Len returns the number of items in the cache.

func (*SimpleCache[K, V]) Purge

func (c *SimpleCache[K, V]) Purge()

Completely clear the cache

func (*SimpleCache[K, V]) Remove

func (c *SimpleCache[K, V]) Remove(key K) bool

Remove removes the provided key from the cache.

func (*SimpleCache[K, V]) Set

func (c *SimpleCache[K, V]) Set(key K, value V) error

Set a new key-value pair

func (*SimpleCache[K, V]) SetWithExpire

func (c *SimpleCache[K, V]) SetWithExpire(key K, value V, expiration time.Duration) error

Set a new key-value pair with an expiration time

Directories

Path Synopsis
examples
autoloading command

Jump to

Keyboard shortcuts

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