Debian Codegraph @

README.md

raw symbols SCIP
1 # go-generics-cache
2
3 [![.github/workflows/test.yml](https://github.com/Code-Hex/go-generics-cache/actions/workflows/test.yml/badge.svg)](https://github.com/Code-Hex/go-generics-cache/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/Code-Hex/go-generics-cache/branch/main/graph/badge.svg?token=Wm7UEwgiZu)](https://codecov.io/gh/Code-Hex/go-generics-cache) [![Go Reference](https://pkg.go.dev/badge/github.com/Code-Hex/go-generics-cache.svg)](https://pkg.go.dev/github.com/Code-Hex/go-generics-cache)
4
5 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](https://go.dev/blog/generics-proposal) which is introduced in 1.18.
6
7 - a thread-safe
8 - implemented with [Go Generics](https://go.dev/blog/generics-proposal)
9 - TTL supported (with expiration times)
10 - Simple cache is like `map[string]interface{}`
11 - See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/simple/example_test.go)
12 - Cache replacement policies
13 - **Least recently used (LRU)**
14 - Discards the least recently used items first.
15 - See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/lru/example_test.go)
16 - **Least-frequently used (LFU)**
17 - Counts how often an item is needed. Those that are used least often are discarded first.
18 - [An O(1) algorithm for implementing the LFU cache eviction scheme](http://dhruvbird.com/lfu.pdf)
19 - See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/lfu/example_test.go)
20 - **First in first out (FIFO)**
21 - Using this algorithm the cache behaves in the same way as a [FIFO queue](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)).
22 - 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.
23 - See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/fifo/example_test.go)
24 - **Most recently used (MRU)**
25 - In contrast to Least Recently Used (LRU), MRU discards the most recently used items first.
26 - See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/mru/example_test.go)
27 - **Clock**
28 - Clock is a more efficient version of FIFO than Second-chance cache algorithm.
29 - See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/clock/example_test.go)
30
31 ## Requirements
32
33 Go 1.18 or later.
34
35 ## Install
36
37 $ go get github.com/Code-Hex/go-generics-cache
38
39 ## Usage
40
41 See also [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/example_test.go) or [go playground](https://go.dev/play/p/kDs-6wpRAcX)
42
43 ```go
44 package main
45
46 import (
47 "context"
48 "fmt"
49 "time"
50
51 cache "github.com/Code-Hex/go-generics-cache"
52 )
53
54 func main() {
55 ctx, cancel := context.WithCancel(context.Background())
56 defer cancel()
57
58 // use simple cache algorithm without options.
59 c := cache.NewContext[string, int](ctx)
60 c.Set("a", 1)
61 gota, aok := c.Get("a")
62 gotb, bok := c.Get("b")
63 fmt.Println(gota, aok) // 1 true
64 fmt.Println(gotb, bok) // 0 false
65
66 // Create a cache for Number constraint. key as string, value as int.
67 nc := cache.NewNumber[string, int]()
68 nc.Set("age", 26, cache.WithExpiration(time.Hour))
69
70 incremented := nc.Increment("age", 1)
71 fmt.Println(incremented) // 27
72
73 decremented := nc.Decrement("age", 1)
74 fmt.Println(decremented) // 26
75 }
76 ```
77
78 ## Articles
79
80 - English: [Some tips and bothers for Go 1.18 Generics](https://dev.to/codehex/some-tips-and-bothers-for-go-118-generics-lc7)
81 - Japanese: [Go 1.18 の Generics を使ったキャッシュライブラリを作った時に見つけた tips と微妙な点](https://zenn.dev/codehex/articles/3e6935ee6d853e)