An in-memory cache system in Go that supports expirable cache entries and various cache eviction policies including Least Frequently Used (LFU), Least Recently Used (LRU), and Adaptive Replacement Cache (ARC).
go get github.com/NonsoAmadi10/zwis
Here's a quick example of how to use the cache:
package main
import (
"context"
"fmt"
"time"
"github.com/NonsoAmadi10/zwis/zwis"
)
func main() {
ctx := context.Background()
// Create an LRU cache
lruCache, err := zwis.NewCache(cache.LRUCacheType, 100)
if err != nil {
panic(err)
}
// Set a value
lruCache.Set(ctx, "key1", "value1", 5*time.Second)
// Get a value
if value, ok := lruCache.Get(ctx, "key1"); ok {
fmt.Printf("key1: %v\n", value)
}
// Delete a value
lruCache.Delete(ctx, "key1")
// Clear the cache
lruCache.Clear(ctx)
}
- MemoryCache: Simple in-memory cache
- LRUCache: Least Recently Used cache
- LFUCache: Least Frequently Used cache
- ARCCache: Adaptive Replacement Cache
- DiskStore: Implementing a Simple Disk-Backed Cache (coming soon)
Contributions are welcome! Please feel free to submit a Pull Request.
This project structure provides a solid foundation for a Go cache library. It demonstrates several important Go concepts and best practices:
- Interface-based design
- Concurrency handling with mutexes
- Use of context for cancelation and timeouts
- Proper error handling
- Package organization
- Unit testing
- Documentation
Areas for potential improvement or expansion:
- Implement a cache using a diskstore
- Add benchmarking tests to compare performance of different cache types
- Implement cache statistics (hit rate, miss rate, etc.)
- Add support for cache serialization/deserialization for persistence
- Implement a distributed cache using Redis or similar