This caching client library, written in Go, enables efficient, unified interaction with popular caching backends: Redis, Memcached, and In-Memory cache with Least Recently Used (LRU) eviction policy. This library is designed to streamline cache operations with a unified API, providing developers the flexibility to use multiple backends interchangeably.
The library provides:
- Multi-Backend Support: Redis, Memcached, and in-memory caching.
- Unified API: A single, simple API for caching operations across different backends.
- In-Memory LRU Caching: Automatically evicts the least recently used items when memory limits are reached.
- Cache Policies: Set expiration times, handle cache invalidation, and manage data freshness.
- Performance Optimized: Benchmarked for high-performance cache operations.
- Simple CRUD Operations: Set, get, update, and delete cache entries easily.
- Flexible Expiration Control: Custom time-to-live (TTL) for each cache entry.
- In-Memory Cache with LRU: Efficient memory usage through LRU eviction.
- Detailed Error Handling: Clear error reporting for all operations, especially for connection issues.
- Performance Benchmarks: Consistent performance across high-throughput scenarios.
Install the library using Go modules:
go get github.com/AnanyaDevGo/Caching-Library-in-Go
The library offers a consistent interface for Redis, Memcached, and in-memory caching. Below are examples demonstrating how to use each backend.
import (
"github.com/AnanyaDevGo/Caching-Library-in-Go/cache"
"time"
"fmt"
)
The library allows you to choose a backend by initializing the respective cache client.
inMemoryCache := cache.NewInMemoryCache(100) // LRU cache with max 100 entries
inMemoryCache.Set("exampleKey", "exampleValue", time.Minute*5)
redisCache := cache.NewRedisCache("localhost:6379", "", 10) // Redis with DB 10
redisCache.Set("exampleKey", "exampleValue", time.Minute*5)
memcachedCache := cache.NewMemcachedCache("localhost:11211")
memcachedCache.Set("exampleKey", "exampleValue", time.Minute*5)
// Set a cache entry with expiration
key := "exampleKey"
value := "exampleValue"
ttl := time.Minute * 5
err := inMemoryCache.Set(key, value, ttl)
if err != nil {
log.Fatalf("Failed to set value: %v", err)
}
// Get a cache entry
retrievedValue, err := inMemoryCache.Get(key)
if err != nil {
log.Fatalf("Failed to retrieve value: %v", err)
} else {
fmt.Printf("Retrieved value: %s\n", retrievedValue)
}
// Delete a cache entry
err = inMemoryCache.Delete(key)
if err != nil {
log.Fatalf("Failed to delete value: %v", err)
}
This library's architecture emphasizes flexibility and performance with its multi-backend approach.
- Unified API: Ensures consistent cache operations across Redis, Memcached, and in-memory LRU.
- LRU In-Memory Caching: Efficient memory management with auto-eviction of stale entries.
- Redis and Memcached Integration: Supports common cache operations like
Set
,Get
,Delete
. - Cache Policies: Custom expiration and invalidation for data freshness.
Performance benchmarks for each backend:
-
In-Memory Cache:
- Set: 9.3M ops/sec
- Get: 61M ops/sec
- Delete: 4.3M ops/sec
- Clear: 3.1M ops/sec
-
Redis Integration:
- Set: ~50k ops/sec
- Get: ~60k ops/sec
-
Memcached Integration:
- Set: 10.6k ops/sec
- Get: 8.8k ops/sec
These benchmarks reflect the library’s suitability for high-throughput caching.
├── cache/
│ ├── redis.go # Redis client implementation
│ ├── memcached.go # Memcached client implementation
│ ├── inmemory.go # LRU in-memory cache
│ ├── cache.go # Core interface and logic
│ └── errors.go # Custom error handling
├── examples/
│ └── main.go # Usage
└── README.md # Project documentation
-
Redis:
HOST
: e.g.,localhost
PORT
: e.g.,6379
PASSWORD
: Optional for secure connectionsDB
: Default is0
-
Memcached:
HOST
: e.g.,localhost
PORT
: e.g.,11211
Contributions are welcome! Please open issues or pull requests on GitHub, and follow Go conventions. Make sure to add tests for new features.
This project is licensed under the MIT License. See the LICENSE file for more details.