Skip to content

gogama/policy-lru

Repository files navigation

policy-lru

A simple LRU cache in GoLang with custom evicton policies and Go generics support.

Use It If You Want

  1. A lightweight least recently used ("LRU") cache with no external dependencies.
  2. Go 1.18 generics.
  3. To define your own eviction policy, for example only evict from the cache when consumed memory or disk space exceeds some threshold.

Examples

Below is an example of the most basic LRU cache with a simple eviction policy based on a maximum number of keys. More examples are available in the API reference documentation.

package main

import (
	"fmt"
	"github.com/gogama/policy-lru"
)

func main() {
	lru := policylru.New[string, string](policylru.MaxCount[string, string](10))
	lru.Add("foo", "bar")
	value, ok := lru.Get("foo")
	fmt.Printf("In cache? %t. Value: %q.\n", ok, value)
}

Status

Go Report Card PkgGoDev

License

This project is licensed under the terms of the Apache License 2.0.

Acknowledgements

The code for this project is heavily based on the code from package lru from github.com/golang/groupcache. The borrowed code is licensed under the Apache License 2.0

Developer happiness on this project was boosted by JetBrains' generous donation of an open source license for their lovely GoLand IDE. ❤

About

LRU cache for Go with custom eviction policies

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages