A high-performance, generic caching library for C# applications, providing in-memory caching for any data type with any key type. This library significantly boosts application performance by reducing redundant data fetching operations. β‘
- Overview
- Problem Statement
- Solution
- Key Features
- Usage Example
- Benefits
- Use Cases
- Requirements
- Installation
- Contributing
- License
- Support
Custom Cache is a lightweight, thread-safe caching library designed to store frequently accessed data in memory, minimizing costly operations like database queries, API calls, or file system access. It supports generic types for keys and values, making it highly flexible and type-safe.
Applications often face performance bottlenecks when repeatedly fetching the same data from slow sources (e.g., databases, APIs, file systems). For instance, a DownloadData method taking 1 second per call can lead to significant delays and resource waste when called multiple times for the same data.
The Custom Cache library addresses this by:
- πΎ Storing data in memory for quick access
- π§ Supporting any key-value type combination
- β‘ Reducing data access time for cached items
- π‘οΈ Ensuring thread safety for concurrent operations
- Generic Type Support: Works with any key type (e.g.,
string,int,Guid, custom objects) and any data type (e.g., primitives, objects, collections). β - Performance Optimization:
- First access: Fetches from the source (normal speed). π
- Subsequent access: Near-instantaneous retrieval from cache. ποΈ
- Thread Safety: Safe for multi-threaded applications with concurrent read/write support. π
- Easy Integration: Intuitive API with minimal setup. π
- Design Patterns:
- π― Decorator Pattern: Enhances data access with caching.
- π Chain of Responsibility: Handles cache misses gracefully.
- π¦ Dictionary: Uses
Dictionary<TKey, TValue>for high-performance storage.
installation π
Clone the repository:
git clone https://github.com/username/custom-cache.gitBelow is an example of how to use the Custom Cache library:
using CustomCache;
// Simulated slow data source
private UserData DownloadUserData(string userId)
{
Thread.Sleep(1000); // Simulate 1-second delay
return new UserData { Id = userId, Name = "John Doe" };
}
// Usage
var cache = new Cache<string, UserData>();
// First access: Fetches from source (slow)
var userData1 = cache.Get("user123", () => DownloadUserData("user123"));
Console.WriteLine(userData1.Name); // Output: John Doe
// Second access: Served from cache (fast)
var userData2 = cache.Get("user123", () => DownloadUserData("user123"));
Console.WriteLine(userData2.Name); //
Output: John Doe (instantaneous)Optional Performance Chart
To highlight the performance benefits, hereβs a sample chart comparing cached vs. non-cached access times:

Support π§ For questions or support, please: Open an issue on GitHub. Contact us via marwanfarook99@gmail.com