https://github.com/ad/lru
A simple LRU cache using go generics.
https://github.com/ad/lru
Last synced: about 1 year ago
JSON representation
A simple LRU cache using go generics.
- Host: GitHub
- URL: https://github.com/ad/lru
- Owner: ad
- Created: 2022-08-11T20:23:08.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-11T20:36:26.000Z (almost 4 years ago)
- Last Synced: 2025-01-25T20:11:24.337Z (over 1 year ago)
- Language: Go
- Size: 145 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LRU Cache
A simple LRU cache using go generics.
It automatically removes elements as new elements are added if the capacity is reached. Items are removes based on how recently they were used where the oldest items are removed first.
## CONSTANTS
```go
const (
// DefaultCacity is the default cache capacity
DefaultCapacity = 10000
)
```
## TYPES
```go
type Cache[K comparable, V any] struct {}
```
## FUNCTIONS
New initializes a new lru cache with the given capacity.
```go
func New[K comparable, V any](cacheOptions ...CacheOption) *Cache[K, V]
```
Delete an item from the cache.
```go
func (c *Cache[K, V]) Delete(key K) bool
```
Flush deletes all items from the cache.
```go
func (c *Cache[K, V]) Flush()
```
Get an item from the cache. This operation updates recent usage of the item.
```go
func (c *Cache[K, V]) Get(key K) (value V, ok bool)
```
Keys returns a slice of all the keys in the cache.
```go
func (c *Cache[K, V]) Keys() []K
```
Len is the number of key value pairs in the cache.
```go
func (c *Cache[K, V]) Len() int
```
Peek gets an item from the cache without updating the recent usage.
```go
func (c *Cache[K, V]) Peek(key K) (value V, ok bool)
```
Set the given key value pair. This operation updates the recent usage of the item.
```go
func (c *Cache[K, V]) Set(key K, value V)
```
### CacheOption configurs a lru cache.
```go
type CacheOption interface {}
```
WithCapacity configures how many items can be stored before old items begin to be deleted.
```go
func WithCapacity(capacity int) CacheOption
```