Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tidwall/tinylru
A fast little LRU cache for Go
https://github.com/tidwall/tinylru
Last synced: about 4 hours ago
JSON representation
A fast little LRU cache for Go
- Host: GitHub
- URL: https://github.com/tidwall/tinylru
- Owner: tidwall
- License: mit
- Created: 2020-02-09T21:03:40.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-05-22T11:31:07.000Z (over 1 year ago)
- Last Synced: 2024-08-01T22:46:11.284Z (3 months ago)
- Language: Go
- Size: 35.2 KB
- Stars: 149
- Watchers: 6
- Forks: 19
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- go-awesome - tinylru
README
# `tinylru`
[![GoDoc](https://godoc.org/github.com/tidwall/tinylru?status.svg)](https://godoc.org/github.com/tidwall/tinylru)
A fast little LRU cache.
## Getting Started
### Installing
To start using `tinylru`, install Go and run go get:
```
$ go get -u github.com/tidwall/tinylru
```This will retrieve the library.
### Usage
Note: this package also has a non-generic `tinylru.LRU` implementation.
```go
// Create an LRU cache
var cache tinylru.LRUG[string, string]// Set the cache size. This is the maximum number of items that the cache can
// hold before evicting old items. The default size is 256.
cache.Resize(1024)// Set a key. Returns the previous value and ok if a previous value exists.
prev, ok := cache.Set("hello", "world")// Get a key. Returns the value and ok if the value exists.
value, ok := cache.Get("hello")// Delete a key. Returns the deleted value and ok if a previous value exists.
prev, ok := cache.Delete("hello")
```A `Set` function may evict old items when adding a new item while LRU is at
capacity. If you want to know what was evicted then use the `SetEvicted`
function.```go
// Set a key and return the evicted item, if any.
prev, ok, evictedKey, evictedValue, evicted := cache.SetEvicted("hello", "jello")
```### Contact
Josh Baker [@tidwall](https://twitter.com/tidwall)
### License
`tinylru` source code is available under the MIT License.