lru is a lightweight package that implements lru cache algorithm in Go.
go get github.com/erfanmomeniii/lru
Next, include it in your application:
import "github.com/erfanmomeniii/lru"
The following example illustrates how to use this package
package main
import (
"fmt"
"github.com/erfanmomeniii/lru"
)
func main() {
l := lru.New(2)
// 2 is the size of lru (default 2000)
l.Set("a", 12)
l.Set("b", 13)
fmt.Println(l.Get("a"))
// 12
l.Set("c", "hi")
fmt.Println(l.Get("c"))
// hi
fmt.Println(l.Get("a"))
// nil
}
type Lru struct {
q *queue.Queue
m map[any]any
size int64
}
Lru is an instantiation of the lru.
func New(size ...int64) *Lru
New creates a new instance of a lru.
func (l *Lru) Set(key any, value any)
Set adds or updates with inputted key and value.
func (l *Lru) Get(key any) any
Get returns the value associated with the inputted key.
func (l *Lru) Has(key any) bool
Has checks whether the key exists or not.
Pull requests are welcome. For changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.