A thread-safe LRU (Least Recently Used) cache implementation in Go.
- Thread-safe LRU cache implementation
- Configurable cache capacity
- Basic operations: Get/Set/Remove
- Automatic eviction of least recently used items
go get github.com/hacker4257/lrucache
package main
import (
"fmt"
"github.com/hacker4257/lrucache"
)
func main() {
// Create a new cache with capacity of 1000
cache := lrucache.NewLruCache(1000)
// Set a cache entry
cache.Set("key1", "value1")
// Get a cache entry
value, exists := cache.Get("key1")
if exists {
fmt.Printf("Found value: %v\n", value)
}
}
Creates a new LRU cache instance with the specified capacity.
Retrieves the value for a given key. Returns false if the key doesn't exist.
Sets a key-value pair in the cache.
- Time Complexity: O(1) for Get/Set/Remove operations
- Space Complexity: O(capacity) where capacity is the cache size
Issues and Pull Requests are welcome!
MIT License