MCPcopy Index your code
hub / github.com/Code-Hex/go-generics-cache

github.com/Code-Hex/go-generics-cache @v1.5.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.5.1 ↗ · + Follow
205 symbols 684 edges 30 files 94 documented · 46% 9 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

go-generics-cache

.github/workflows/test.yml codecov Go Reference

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 is introduced in 1.18.

  • a thread-safe
  • implemented with Go Generics
  • TTL supported (with expiration times)
  • Simple cache is like map[string]interface{}
  • See examples
  • 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.

Install

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

Usage

See also examples or go playground

package main

import (
    "context"
    "fmt"
    "time"

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

func main() {
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    // use simple cache algorithm without options.
    c := cache.NewContext[string, int](ctx)
    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

Extension points exported contracts — how you extend this code

Interface (Interface)
Interface is a common-cache interface.
cache.go
Number (Interface)
Number is a constraint that permits any numeric types.
constraint.go
Option (FuncType)
Option is an option for FIFO cache.
policy/fifo/fifo.go
Option (FuncType)
Option is an option for MRU cache.
policy/mru/mru.go
Option (FuncType)
Option is an option for LRU cache.
policy/lru/lru.go
Option (FuncType)
Option is an option for clock cache.
policy/clock/clock.go
Option (FuncType)
Option is an option for LFU cache.
policy/lfu/lfu.go
ItemOption (FuncType)
ItemOption is an option for cache item.
cache.go

Core symbols most depended-on inside this repo

Set
called by 141
cache.go
Get
called by 80
cache.go
Len
called by 70
cache.go
Keys
called by 29
cache.go
Delete
called by 21
cache.go
WithExpiration
called by 13
cache.go
Push
called by 10
policy/lfu/priority_queue.go
Pop
called by 8
policy/lfu/priority_queue.go

Shape

Function 89
Method 75
Struct 30
FuncType 7
Interface 2
TypeAlias 2

Languages

Go100%

Modules by API surface

cache.go39 symbols
policy/mru/mru.go14 symbols
policy/lru/lru.go14 symbols
policy/fifo/fifo.go13 symbols
policy/clock/clock.go13 symbols
expiration.go13 symbols
policy/lfu/priority_queue.go11 symbols
policy/lfu/lfu.go11 symbols
example_test.go11 symbols
policy/simple/simple.go8 symbols
policy/lfu/lfu_test.go6 symbols
policy/clock/clock_test.go6 symbols

For agents

$ claude mcp add go-generics-cache \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact