gofast is a Go module that provides easy-to-use in-memory cache algorithms like LRU (Least Recently Used). It allows you to import and use these algorithms simply and directly in your Go projects.
Install gofast by simply using the go get
command:
$ go get github.com/raghavgh/gofast
First, import gofast like this:
import "github.com/raghavgh/gofast"
Here is an example of how you can use the module:
type CacheRepo struct {
MetaCache gofast.Cache
}
func NewCacheRepo() *CacheRepo {
return &CacheRepo{
// gofast.LRU is the algorithm type for LRU.
// 1000 is limit of your cache.
MetaCache: gofast.NewCache(1000, gofast.LRU),
}
}
// Get returns the value (if any) and a boolean representing whether the value was found or not
Get(key string) (any, bool)
// Put adds a value to the cache
Put(key string, val any)
// Remove removes a value from the cache
Remove(key string)
// Len returns the number of elements of the cache
Len() int
// Clear clears the cache
Clear()
// Contains returns true if the cache contains the given key
Contains(key string) bool
**All above funtions are thread safe
Currently, the following cache algorithms are available:
LRU (Least Recently Used)
LFU (Least Frequently Used)
LIFO (Last In, First Out)
FIFO (First In, First Out)
More algorithms will be available in future versions.
Supported algorithms can be specified with the following constants:
gofast.LRU // Least Recently Used algorithm
gofast.LFU // Least Frequently Used algorithm
gofast.FIFO // First In, First Out algorithm
gofast.MRU // Most Recently Used algorithm
gofast.RR // Random Replacement algorithm
gofast.SLRU // Segmented Least Recently Used algorithm
gofast.LIFO // Last In, First Out algorithm
We’re excited to have you contribute to the gofast library! Whether you’re fixing bugs 🐛, adding new features ✨, improving documentation 📚, or enhancing test coverage 🧪—we’d love your help!
Check out our issues page for a list of open tasks or feel free to suggest your own improvements! We’re especially interested in:
We look forward to your valuable contributions and ideas 💡! Let’s make gofast even faster together! 🎉