This Go package implements a Goroutine-safe Least Recently Used (LRU) cache with a time-to-live (TTL) feature. It allows storing, retrieving, and automatically evicting expired items from the cache.
- Goroutine-safe LRU caching
- Configurable TTL for cache entries
- Automatic eviction of expired items
To install the package, run:
go get github.com/usysrc/lru
Then import it in your code:
import "github.com/usysrc/lru"
Create a new cache with a specified TTL and capacity:
ttl := 5 * time.Minute
capacity := 100
cache := lru.NewCache(ttl, capacity)
Add an item to the cache with the Put
method:
cache.Put("key1", "value1")
Retrieve an item from the cache with the Get
method:
value, ok := cache.Get("key1")
if ok {
fmt.Println("Value:", value)
} else {
fmt.Println("Key not found or expired")
}
To automatically evict expired items, call the EvictExpiredItems
method in a goroutine:
go cache.EvictExpiredItems()
Creates a new cache with the given TTL and capacity.
ttl
: The time-to-live duration for cache entries.capacity
: The maximum number of entries the cache can hold.
Adds an item to the cache. If the key already exists, it updates the value and moves the item to the front.
key
: The key of the item.value
: The value of the item.
Retrieves an item from the cache. If the key is found and the item is not expired, it moves the item to the front.
key
: The key of the item.- Returns: The value and a boolean indicating if the key was found.
Evicts expired items from the cache. This method should be called in a goroutine to run periodically.
package main
import (
"fmt"
"time"
"github.com/usysrc/lru"
)
func main() {
ttl := 5 * time.Minute
capacity := 100
cache := lru.NewCache(ttl, capacity)
// Start the eviction goroutine
go cache.EvictExpiredItems()
// Add items to the cache
cache.Put("key1", "value1")
cache.Put("key2", "value2")
// Retrieve items from the cache
value, ok := cache.Get("key1")
if ok {
fmt.Println("Value:", value)
} else {
fmt.Println("Key not found or expired")
}
// Wait for items to expire
time.Sleep(6 * time.Minute)
// Try to retrieve expired item
value, ok = cache.Get("key1")
if ok {
fmt.Println("Value:", value)
} else {
fmt.Println("Key not found or expired")
}
}
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
This README provides an overview of the package, installation instructions, usage examples, and API reference. For more details, refer to the package documentation and source code.