Skip to content

A simple LRU cache written in go, safe for concurrent access.

License

Notifications You must be signed in to change notification settings

thepatrik/golru

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

golru

golru is a simple LRU cache, configurable with a max number of entries. If the cache is full and another entry is added, the least recently used entry is evicted. The cache is safe for concurrent access.

The implementation is based on a doubly linked list and a hash table (map). The map makes the time complexity of get() to O(1). The list makes the time complexity of put() to O(1). The space complexity is O(n).

import (
	"fmt"

	"github.com/thepatrik/golru"
)

func main() {
	lru, _ := golru.New[string, any](golru.WithMaxEntries(100))

	lru.SetOnEvicted(func(k string, v any) {
		fmt.Printf("evicted key %v with value %v", k, v)
	})

	lru.Put("abracadabra", "Puff, the Magic Dragon")

	val, ok := lru.Get("abracadabra")
	if ok {
		fmt.Printf("found value \"%v\"\n", val)
	}
}

About

A simple LRU cache written in go, safe for concurrent access.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages