Skip to content

DeltaCore.CacheHelper is a lightweight .NET library designed to simplify caching operations in your .NET applications using either in-memory or distributed (Redis) cache mechanisms.

License

Notifications You must be signed in to change notification settings

AhmedNady2003/CacheHelper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CacheHelper

DeltaCore.CacheHelper

DeltaCore.CacheHelper is a lightweight .NET library designed to simplify caching operations in your .NET applications using either in-memory or distributed (Redis) cache mechanisms.

This library abstracts common caching patterns to help developers interact with caching in a clean, reusable, and testable manner.


Features

  • In-Memory Caching using IMemoryCache
  • Distributed Caching using IDistributedCache (supports Redis)
  • Generic methods for Get, Set, and Delete
  • Overloaded Set methods to support custom expiration
  • Fully asynchronous for distributed cache
  • Supports SlidingExpiration, AbsoluteExpiration, and customizable options

Installation

Once published on NuGet, you’ll be able to install it using:

dotnet add package DeltaCore.CacheHelper

🚀 Usage

1. MemoryCacheService (In-Memory)

public class SomeService
{
    private readonly MemoryCachService _cache;

    public SomeService(MemoryCachService cache)
    {
        _cache = cache;
    }

    public void SetItem()
    {
        _cache.SetData<object>("user:1", new { Name = "Ahmed", Age = 21 });
    }

    public object? GetItem()
    {
        return _cache.GetData<object>("user:1");
    }

    public void RemoveItem()
    {
        _cache.DelData("user:1");
    }
}

You can also pass custom expiration:

var options = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(15));
_cache.SetData<T>("key", <T>value, options);

2. RedisCacheService (Distributed)

public class SomeService
{
    private readonly RedisCacheService _cache;

    public SomeService(RedisCacheService cache)
    {
        _cache = cache;
    }

    public async Task SetAsync()
    {
        await _cache.SetDataAsync<object>("product:1", new { Id = 1, Name = "Bread" });
    }

    public async Task<object?> GetAsync()
    {
        return await _cache.GetDataAsync<object>("product:1");
    }

    public async Task DeleteAsync()
    {
        await _cache.DelDataAsync("product:1");
    }
}

With custom expiration:

var options = new DistributedCacheEntryOptions
{
    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30)
};
await _cache.SetDataAsync<T>("key", <T>value, options);

Configuration

Add Services in Program.cs

builder.Services.AddMemoryCache();
builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = builder.Configuration.GetConnectionString("Redis");
});
builder.Services.AddScoped<MemoryCachService>();
builder.Services.AddScoped<RedisCacheService>();

Project Structure

DeltaCore.CacheHelper/
├── MemoryCacheService.cs     # Handles in-memory caching
├── RedisCacheService.cs      # Handles distributed (Redis) caching
├── DeltaCore.CacheHelper.csproj

GitHub Actions (CI/CD)

This project includes a GitHub Action to automatically publish the package to NuGet when you push a tag in the format v*.*.*.

Workflow file:.github/workflows/nuget-publish.yml


License

This project is licensed under the MIT License.


Author

👤 Ahmed Nady

GitHub LinkedIn Gmail

About

DeltaCore.CacheHelper is a lightweight .NET library designed to simplify caching operations in your .NET applications using either in-memory or distributed (Redis) cache mechanisms.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages