Skip to content
/ Cachula Public

Cachula is a flexible multi-layer caching library for .NET with in-memory and distributed layers, cache stampede protection, and DI integration. Optimized for modern distributed apps with a focus on efficient bulk operations.

License

Notifications You must be signed in to change notification settings

ne4ta/Cachula

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cachula logo

Cachula

Build NuGet NuGet Downloads License

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.


Key Features

  • 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.

Why Cachula?

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.


Quick Start

1. Register Cachula in DI

services.AddMemoryCache();
services.PutOnCachula()
    .WithMemoryCache(); // Adds in-memory cache layer

// Optionally add a distributed cache layer (e.g., Redis)
services.WithDistributedCache(new CachulaRedisCache(redisDatabase));

2. Use ICachulaCache in your services

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)
        );
    }
}

Advanced Usage

Custom Cache Layers

Implement ICacheLayer to add your own cache backend.

Distributed Cache

Use CachulaRedisCache for Redis support:

var redisCache = new CachulaRedisCache(redisDatabase);
services.WithDistributedCache(redisCache);

Null and Miss Handling

  • Null: Value was found in cache and is null.
  • Missed: Value was not found in cache.

FAQ

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.

About

Cachula is a flexible multi-layer caching library for .NET with in-memory and distributed layers, cache stampede protection, and DI integration. Optimized for modern distributed apps with a focus on efficient bulk operations.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages