CleanTemplate.Caching 8.0.2

dotnet add package CleanTemplate.Caching --version 8.0.2                
NuGet\Install-Package CleanTemplate.Caching -Version 8.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CleanTemplate.Caching" Version="8.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CleanTemplate.Caching --version 8.0.2                
#r "nuget: CleanTemplate.Caching, 8.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CleanTemplate.Caching as a Cake Addin
#addin nuget:?package=CleanTemplate.Caching&version=8.0.2

// Install CleanTemplate.Caching as a Cake Tool
#tool nuget:?package=CleanTemplate.Caching&version=8.0.2                

Clean Architecture Base Template for Caching <img src="../icon.png" height="40" width="40"/>

NuGet Version NuGet Downloads GitHub Release GitHub Tag GitHub Actions Workflow Status GitHub last commit GitHub commit activity GitHub Issues or Pull Requests GitHub Issues or Pull Requests GitHub top language

This package is a Clean Architecture Base Template comprising all Baseic and Abstract and Contract types for Redis and Memory Caching.

This package is one of the Cleantemplate side packages which provides the basic requirements for creating Redis or Memory Caches.

The .Net version used in this project is net8.0

<p align="center" width="100%"> <img src="../icon.png" height="128" width="128"/> </p>

Contents

Dependencies

net8.0

  • CleanTemplate (>= 7.0.0)
  • StackExchange.Redis (>= 2.7.33)
  • StackExchange.Redis.Extensions.AspNetCore (>= 10.2.0)
  • StackExchange.Redis.Extensions.Core (>= 10.2.0)
  • StackExchange.Redis.Extensions.Newtonsoft (>= 10.2.0)

Installation

.Net CLI

dotnet add package CleanTemplate.Caching --version x.x.x

Package Manager

NuGet\Install-Package CleanTemplate.Caching -Version x.x.x

Package Reference

<PackageReference Include="CleanTemplate.Caching" Version="x.x.x" />

Paket CLI

paket add CleanTemplate.Caching --version x.x.x

Script & Interactive

#r "nuget: CleanTemplate.Caching, x.x.x"

Cake

// Install CleanTemplate.Caching as a Cake Addin
#addin nuget:?package=CleanTemplate.Caching&version=x.x.x

// Install CleanTemplate.Caching as a Cake Tool
#tool nuget:?package=CleanTemplate.Caching&version=x.x.x

Usage

I tried to follow the basic format and structure of CleanTemplate here.

Dependency Injection and Add Caching to the project

To use from CleanTemplate.Caching in your project you should first register it in Program.cs as below:

1- First in appsettings.json add a section for redis configuration which is matched with StackExchange.Redis.Extensions.Core.Configuration.RedisConfiguration:

"Redis": {
    "Hosts": [
      {
        "Host": "localhost",
        "Port": 6379
      }
    ]
  }

2- Add below Extension Method (AddCleanCaching(IConfigurationSection redisSection)) to Program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCleanTemplate();

builder.Services.AddCleanCaching(builder.Configuration.GetSection("Redis"))

var app = builder.Build();

app.UseCleanTemplate();

app.Run();

Notable Point: Note that to use the CleanTemplate.Caching package, you must have registered the main CleanTemplate package beforehand.

Redis Cache

To use Base Redis Cache, you have to do exactly the same thing as using Base Repository with the difference that there is no UnitOfWork here. You should create an interface that inherit from ICleanBaseRedisCache<TKey, TValue> and create a Class that inherit from CleanBaseRedisCache<TKey, TValue> and also implements its interface:

public interface IProductRedisCache : ICleanBaseRedisCache<string, Product>
{
}

public class ProductRedisCache : CleanBaseRedisCache<string, Product>, IProductRedisCache
{
    public ProductRedisCache(IRedisDatabase database) : base(database)
    {
    }

    protected override string CachePrefixKey => "Product";
}

Now you can directly inject this service inside of your services and use it.

public class ProductController : ControllerBase
{
    private readonly IProductRedisCache _redisCache;
    public ProductController(IProductRedisCache redisCache)
    {
        _redisCache = redisCache;
    }
}

All basic Methods implemented in CleanBaseRedisCache are as below:

TValue? Get(TKey key);
Task<TValue?> GetAsync(TKey key);
Task<List<string>> GetAllKeysAsync();
bool KeyExists(TKey key);
Task<bool> KeyExistsAsync(TKey key);
bool Remove(TKey key);
Task<bool> RemoveAsync(TKey key);
Task<long> RemoveAllKeysAsync();
bool Set(TKey key, TValue value, TimeSpan? expiryTime = null);
Task<bool> SetAsync(TKey key, TValue value, TimeSpan? expiryTime = null);
bool Set(TKey key, TValue value, DateTimeOffset expiryAt);
Task<bool> SetAsync(TKey key, TValue value, DateTimeOffset expiryAt);
Task FlushDbAsync();
IDatabase GetDatabase();
IRedisDatabase GetRedisDatabase();

Memory Cache

To use Memory Cache, you should operate exactly the same as Redis Cache. You should create an interface that inherit from ICleanBaseMemoryCache<TKey, TValue> and create a Class that inherit from CleanBaseMemoryCache<TKey, TValue> and also implements its interface:

public interface IProductMemoryCache : ICleanBaseMemoryCache<string, Product>
{
}

public class ProductMemoryCache : CleanBaseMemoryCache<string, Product>, IProductMemoryCache
{
    public ProductMemoryCache(IMemoryCache memoryCache) : base(memoryCache)
    {
    }

    protected override string CachePrefixKey => "Product";
}

Now you can directly inject this service inside of your services and use it.

public class ProductController : ControllerBase
{
    private readonly IProductMemoryCache _memoryCache;
    public ProductController(IProductMemoryCache memoryCache)
    {
        _memoryCache = memoryCache;
    }
}

All basic Methods implemented in CleanBaseMemoryCache are as below:

TValue Set(TKey key, TValue value);
TValue Set(TKey key, TValue value, DateTimeOffset absoluteExpiration);
TValue Set(TKey key, TValue value, TimeSpan absoluteExpirationRelativeToNow);
TValue Set(TKey key, TValue value, IChangeToken expirationToken);
TValue Set(TKey key, TValue value, MemoryCacheEntryOptions? options);
TValue? Get(TKey key);
bool TryGetValue(TKey key, out TValue? value);
TValue? GetOrCreate(TKey key, Func<ICacheEntry, TValue> factory);
Task<TValue?> GetOrCreateAsync(TKey key, Func<ICacheEntry, Task<TValue>> factory);
ICacheEntry CreateEntry(TKey key);
void Remove(TKey key);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.2 120 6/28/2024
8.0.1 115 6/14/2024
8.0.0 132 4/6/2024
7.5.0 129 4/1/2024
7.0.1 133 3/28/2024
7.0.0 127 3/28/2024

- bug fix