Cachula is a flexible, multi-layer caching library for .NET, designed for modern distributed applications. It supports in-memory and distributed cache layers, cache stampede protection, and seamless integration with dependency injection. Cachula is inspired by the best practices of modern caching libraries, with a special focus on efficient bulk operations.
- Multi-layer caching: Compose multiple cache layers (e.g., memory, Redis).
- Bulk operations: Native support for efficient batch get/set/remove operations for multiple keys.
- Cache stampede protection: Prevents multiple concurrent loads for the same key(s) using single-flight logic.
- Null/miss handling: Distinguishes between cache misses and null values.
- Dependency injection ready: Easy to add to your DI container.
- Extensible: Plug in your own cache layers or stampede protection strategies.
Cachula is designed for high-throughput, data-driven .NET applications that need to:
- Minimize round-trips to slow data sources.
- Efficiently cache and retrieve large numbers of items at once.
- Avoid cache stampede.
- Compose multiple cache layers for optimal performance and reliability.
Unique advantage: Unlike most caching libraries, Cachula natively supports working with multiple keys at once, making it ideal for batch-oriented scenarios.
services.AddMemoryCache();
services.PutOnCachula()
.WithMemoryCache(); // Adds in-memory cache layer
// Optionally add a distributed cache layer (e.g., Redis)
services.WithDistributedCache(new CachulaRedisCache(redisDatabase));public class MyService
{
private readonly ICachulaCache _cache;
public MyService(ICachulaCache cache) => _cache = cache;
public async Task<MyData?> GetDataAsync(string key)
{
return await _cache.GetOrSetAsync(
key,
async ct => await LoadFromDbAsync(key, ct),
TimeSpan.FromMinutes(10)
);
}
public async Task<IReadOnlyCollection<MyData>> GetManyAsync(IEnumerable<string> keys)
{
return await _cache.GetOrSetManyAsync(
keys,
async (missingKeys, ct) => await LoadManyFromDbAsync(missingKeys, ct),
TimeSpan.FromMinutes(10)
);
}
}Implement ICacheLayer to add your own cache backend.
Use CachulaRedisCache for Redis support:
var redisCache = new CachulaRedisCache(redisDatabase);
services.WithDistributedCache(redisCache);- Null: Value was found in cache and is
null. - Missed: Value was not found in cache.
Q: How is Cachula different from FusionCache or CacheTower?
- Cachula is focused on batch/bulk operations and multi-layer composition, with a simple, extensible API.
- It is designed for scenarios where you need to cache and retrieve many items at once efficiently.
