Redis.Sidecar.Cache 1.3.0

Suggested Alternatives

Redis.Cache.Extensions

Additional Details

Renamed package to Redis.Cache.Extensions. Please move to that package.

dotnet add package Redis.Sidecar.Cache --version 1.3.0                
NuGet\Install-Package Redis.Sidecar.Cache -Version 1.3.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="Redis.Sidecar.Cache" Version="1.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Redis.Sidecar.Cache --version 1.3.0                
#r "nuget: Redis.Sidecar.Cache, 1.3.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 Redis.Sidecar.Cache as a Cake Addin
#addin nuget:?package=Redis.Sidecar.Cache&version=1.3.0

// Install Redis.Sidecar.Cache as a Cake Tool
#tool nuget:?package=Redis.Sidecar.Cache&version=1.3.0                

Redis.Sidecar.Cache

.NET

Redis.Sidecar.Cache is a package used to easily set up and use Redis caches within your application using the StackExchange.Redis package. Unlike the name of the package, you can use any Redis cache, not just a Sidecar cache, even though a Sidecar cache is what will be used by default.

A variety of methods are available to easily set up and utilize a cache within your application.

RedisCache

RedisCache is a class available that takes care of setting up a connection to the Redis Sidecar for you, as well as provide useful methods for managing items in the cache.

Available Methods

The methods available to be used are Get/GetAsync, Set/SetAsync, Delete/DeleteAsync.

Get/GetAsync

Required Parameters:

  • key (string)
    • The identifier for the value to get from the cache.

Optional Parameters:

  • function (Func<T>)
    • Function to invoke to get the value to set in the cache, if the value isn't found in the cache.
  • expiration (TimeSpan)
    • Time for the object to live in the cache. Defaults to 1 hour if not provided.
  • condition (Func<T, bool>)
    • The condition that must be true to save the value to the cache.

Gets the object at the key from the cache. Get does this synchronously, whereas GetAsync does this asynchronously.

function: If provided, executes the input function to get the object and add it in the cache.

condition: If provided, only saves to the cache if the input condition is met.

Returns the value from the cache, if found. Returns null if the value could not be found.

Examples:

// Get
var value1 = redisCache.Get<int>("KeyName");
var value2 = redisCache.Get<int>("KeyName",() => 42);
var value3 = redisCache.Get<int>("KeyName",() => 42, TimeSpan.FromMinutes(20), x => x > 30);

// GetAsync
var value1 = await redisCache.GetAsync<int>("KeyName");
var value2 = await redisCache.GetAsync<int>("KeyName",() => 42);
var value3 = await redisCache.GetAsync<int>("KeyName",() => 42, TimeSpan.FromMinutes(20), x => x > 30); 
Set/SetAsync

Required Parameters:

  • key (string)
    • The identifier for the value to get from the cache.
  • value (T)
    • The value to set in the cache for the key.

Optional Parameters:

  • expiration (TimeSpan)
    • Time for the object to live in the cache. Defaults to 1 hour if not provided.
  • condition (Func<T, bool>)
    • The condition that must be true to save the value to the cache.

Sets the object at the key from the cache. Set does this synchronously, whereas SetAsync does this asynchronously.

condition: If provided, only saves to the cache if the input condition is met.

Sets the value for the key in the cache. Returns true if successful and false if not successful.

Examples:

// Set
var value1 = redisCache.Set<int>("KeyName", 42);
var value2 = redisCache.Set<int>("KeyName", 42, Timespan.FromMinutes(20));
var value3 = redisCache.Set<int>("KeyName", 42, TimeSpan.FromMinutes(20), x => x > 30);
var value4 = redisCache.Set<int>("KeyName", 42, x => x > 30);

// SetAsync
var value1 = await redisCache.SetAsync<int>("KeyName", 42);
var value2 = await redisCache.SetAsync<int>("KeyName", 42, Timespan.FromMinutes(20));
var value3 = await redisCache.SetAsync<int>("KeyName", 42, TimeSpan.FromMinutes(20), x => x > 30);
var value4 = await redisCache.SetAsync<int>("KeyName", 42, x => x > 30);
Delete/DeleteAsync

Required Parameters:

  • key (string)
    • The identifier for the value to get from the cache.

Removes the object at the key from the cache. Delete does this synchronously, whereas DeleteAsync does this asynchronously.

Returns true if successful and false if not successful.

Examples:

// Delete
var deleted = redisCache.Delete("KeyName");

// DeleteAsync
var deleted = await redisCache.DeleteAsync("KeyName");

WebApplicationBuilder Extensions

This class provides extensions for WebApplicationBuilder to automatically set up and register a RedisCache instance for your application to use.

By default, the RedisCache will attempt to find the Redis Sidecar container running on localhost.

Overloads are provided to provide an IDatabase or a specific host to set up the redis connection yourself.

Examples:

webApplicationBuilder.AddRedisCache(); // Default

webApplicationBuilder.AddRedisCache(myDatabase); // `IDatabase` instance provided.

webApplicationBuilder.AddRedisCache("myhost"); // Host provided.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
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
1.3.0 201 1/13/2024 1.3.0 is deprecated because it is no longer maintained.
1.2.0 127 1/12/2024
1.1.1 113 1/12/2024
1.1.0 113 1/11/2024
1.0.1 105 1/10/2024
1.0.0 124 1/9/2024