Netcool.Caching 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Netcool.Caching --version 1.0.0                
NuGet\Install-Package Netcool.Caching -Version 1.0.0                
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="Netcool.Caching" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Netcool.Caching --version 1.0.0                
#r "nuget: Netcool.Caching, 1.0.0"                
#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 Netcool.Caching as a Cake Addin
#addin nuget:?package=Netcool.Caching&version=1.0.0

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

Netcool.Caching

GitHub Nuget Nuget

Netcool.Caching offers enhanced extensions for Distributed caching in ASP.NET Core, both MemoryDistributeCache and RedisCache.

Configuration

Distributed Redis cache

builder.Services.AddNetcoolRedisCache(options =>
{
    options.Configuration = "localhost";
});

Distributed Memory Cache

builder.Services.AddNetcoolDistributedMemoryCache();

Use in application service

public class MyService : IService
{
    private readonly INetcoolDistributedCache _cache;

    public MyService(INetcoolDistributedCache cache)
    {
        _cache = cache;
    }

    public void SetCache()
    {
        var obj = new WeatherForecast
        {
            Date = DateTime.Now,
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        };
        _cache.SetObject("WeatherForecast", obj,
            new DistributedCacheEntryOptions { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(5) });
    }
}

Extensions

Set and get object

cache.SetObject("Person", new Person());
var person = cache.GetObject("Person");

await cache.SetObjectAsync("Person", new Person());
var person =await cache.GetObjectAsync("Person");

Increase and decrease

cache.SetString("count", "1");

var curr = cache.Increase("count",byValue:2, maxValue:10);
var curr2 = await cache.IncreaseAsync("count",2);

var res = cache.GetString("count");
Assert.AreEqual("3", res);
Assert.AreEqual(3, curr);
cache.SetString("count", "1");

var curr = cache.Decrease("count", byValue:2, minValue:1);
var curr2 = await cache.DecreaseAsync("count", byValue:2, minValue:1);

var res = cache.GetString("count");
Assert.AreEqual(1, curr);
Assert.AreEqual("1", res);

Object Serializer

The default object serializer is SystemTextJsonSerializer, you can change the JsonSerializerOptions.

builder.Services.AddNetcoolRedisCache(options =>
{
    options.Configuration = "localhost";
    options.ObjectSerializer = new SystemTextJsonSerializer(new JsonSerializerOptions());
});

Otherwise, you can customize your own serializer:

public class MySerializer : ISerializer
{
    private readonly JsonSerializerOptions _jsonSerializerOptions;

    public byte[] Serialize<T>(T obj)
    {
        return Encoding.UTF8.GetBytes(obj.ToString());
    }

    public T Deserialize<T>(byte[] bytes)
    {
        return (T)Convert.ChangeType(Encoding.UTF8.GetString(b).ToString(), typeof(T));
    }
}

Then configure at startup:

builder.Services.AddNetcoolRedisCache(options =>
{
    options.Configuration = "localhost";
    options.ObjectSerializer = new MySerializer();
});
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
9.0.0 0 12/1/2024
8.0.0 176 1/9/2024
1.0.3 471 10/12/2022
1.0.2 400 8/11/2022
1.0.0 391 8/10/2022

Distribute cache extensions