Categorygithub.com/Code-Hex/go-generics-cache
modulepackage
0.0.9
Repository: https://github.com/code-hex/go-generics-cache.git
Documentation: pkg.go.dev

# README

go-generics-cache

.github/workflows/test.yml codecov

go-generics-cache is an in-memory key:value store/cache that is suitable for applications running on a single machine. This in-memory cache uses Go Generics which will be introduced in 1.18.

  • a thread-safe
  • implemented with Go Generics
  • TTL supported (with expiration times)
  • Simple cache is like map[string]interface{}
  • Cache replacement policies
    • Least recently used (LRU)
      • Discards the least recently used items first.
      • See examples
    • Least-frequently used (LFU)
    • First in first out (FIFO)
      • Using this algorithm the cache behaves in the same way as a FIFO queue.
      • The cache evicts the blocks in the order they were added, without any regard to how often or how many times they were accessed before.
      • See examples
    • Most recently used (MRU)
      • In contrast to Least Recently Used (LRU), MRU discards the most recently used items first.
      • See examples
    • Clock
      • Clock is a more efficient version of FIFO than Second-chance cache algorithm.
      • See examples

Requirements

Go 1.18 or later.

If Go 1.18 has not been released but you want to try this package, you can easily do so by using the gotip command.

$ go install golang.org/dl/gotip@latest
$ gotip download # latest commit
$ gotip version
go version devel go1.18-c2397905e0 Sat Nov 13 03:33:55 2021 +0000 darwin/arm64

Install

$ go get github.com/Code-Hex/go-generics-cache

Usage

See also examples or gotipplay playground

package main

import (
	"fmt"
	"time"

	cache "github.com/Code-Hex/go-generics-cache"
)

func main() {
	// use simple cache algorithm without options.
	c := cache.New[string, int]()
	c.Set("a", 1)
	gota, aok := c.Get("a")
	gotb, bok := c.Get("b")
	fmt.Println(gota, aok) // 1 true
	fmt.Println(gotb, bok) // 0 false

	// Create a cache for Number constraint. key as string, value as int.
	nc := cache.NewNumber[string, int]()
	nc.Set("age", 26, cache.WithExpiration(time.Hour))

	incremented := nc.Increment("age", 1)
	fmt.Println(incremented) // 27

	decremented := nc.Decrement("age", 1)
	fmt.Println(decremented) // 26
}

Articles

# Packages

No description provided by the author

# Functions

AsClock is an option to make a new Cache as clock algorithm.
AsFIFO is an option to make a new Cache as FIFO algorithm.
AsLFU is an option to make a new Cache as LFU algorithm.
AsLRU is an option to make a new Cache as LRU algorithm.
AsMRU is an option to make a new Cache as MRU algorithm.
New creates a new thread safe Cache.
NewNumber creates a new cache for Number constraint.
WithExpiration is an option to set expiration time for any items.

# Structs

Cache is a thread safe cache.
Item is an item.
NumberCache is a in-memory cache which is able to store only Number constraint.

# Interfaces

Interface is a common-cache interface.
Number is a constraint that permits any numeric types.

# Type aliases

ItemOption is an option for cache item.
Option is an option for cache.