DevelopmentHelpers.AzureDistributedCache
8.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package DevelopmentHelpers.AzureDistributedCache --version 8.0.0
NuGet\Install-Package DevelopmentHelpers.AzureDistributedCache -Version 8.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="DevelopmentHelpers.AzureDistributedCache" Version="8.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DevelopmentHelpers.AzureDistributedCache --version 8.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DevelopmentHelpers.AzureDistributedCache, 8.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 DevelopmentHelpers.AzureDistributedCache as a Cake Addin #addin nuget:?package=DevelopmentHelpers.AzureDistributedCache&version=8.0.0 // Install DevelopmentHelpers.AzureDistributedCache as a Cake Tool #tool nuget:?package=DevelopmentHelpers.AzureDistributedCache&version=8.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
DevelopmentHelpers.AzureDistributedCache
Overview
**DevelopmentHelpers.AzureDistributedCache is a library that simplifies access to Azure Redis Cache in .NET applications.
Installation
- Add Configuration Settings to your
appsettings.json
file:{ "DevelopmentHelpers" : { "AzureDistributedCacheConfiguration": { "DnsName": "--DNS--NAME", "ConnectionString": "-- Connection String --- " } } }
- Add the Development Helper Service
builder.Services.AddAzureRedisCache(builder.Configuration);
Code Example
public class IndexModel : PageModel
{
private readonly IDistributedCache cache;
private const string cacheItem = "CacheTime";
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger, IDistributedCache cache)
{
this.cache = cache;
_logger = logger;
}
public async Task OnGetAsync()
{
string value = await cache.GetStringAsync(cacheItem);
if (value == null)
{
value = DateTime.Now.ToString();
var options = new DistributedCacheEntryOptions();
options.SetSlidingExpiration(TimeSpan.FromMinutes(10));
await this.cache.SetStringAsync(cacheItem, value, options);
}
ViewData[cacheItem] = value;
ViewData["CurrentTime"] = DateTime.Now.ToString();
}
}
Motivation
I needed a consistent and easy to use library in .net application.
API Reference
IDistributedCache Interface
The IDistributedCache
interface is used to perform distributed caching operations in .NET. Below are some of its key methods:
- GetStringAsync:
Task<string> GetStringAsync(string key);
- SetStringAsync:
Task SetStringAsync(string key, string value, DistributedCacheEntryOptions options);
- RemoveAsync:
Task RemoveAsync(string key);
- SetStringAsync:
Task SetStringAsync(string key, string value, DistributedCacheEntryOptions options);
- GetAsync:
Task<byte[]> GetAsync(string ;
- SetStringAsync:
Task SetStringAsync(string key, string value, DistributedCacheEntryOptions options);
- GetAsync:
Task<byte[]> GetAsync(string key);
- SetAsync:
Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options);
- RefreshAsync:
Task RefreshAsync(string key);
- GetString:
string GetString(string key);
- SetString:
void SetString(string key, string value, DistributedCacheEntryOptions options);
- Get:
byte[] Get(string key);
- Set:
void Set(string key, byte[] value, DistributedCacheEntryOptions options);
- Remove:
void Remove(string key);
Tests
[TestClass]
public class CacheTests
{
[TestInitialize]
public void TestInitialize()
{
}
[DataTestMethod]
public async Task SetRemoveAndGetStringCacheAsync()
{
IDistributedCache redisCache = (IDistributedCache)
KernelMapper.ServiceProvider.GetService(typeof(IDistributedCache));
string key = Guid.NewGuid().ToString();
string value = DateTime.Now.ToString();
var options = new DistributedCacheEntryOptions();
options.SetSlidingExpiration(TimeSpan.FromMinutes(10));
//Set the cache
await redisCache.SetStringAsync(key, value, options);
var cacheValue = await redisCache.GetStringAsync(key);
Assert.IsTrue(value.Equals(cacheValue, StringComparison.CurrentCultureIgnoreCase));
await redisCache.RemoveAsync(key);
var cacheValue2 = await redisCache.GetStringAsync($"{key}");
Assert.IsTrue(cacheValue2 == null);
}
[DataTestMethod]
[DataRow("quick brown fox jumps right over the lazy dog.")]
public async Task SetAndGetByteCacheAsync(string dataItem)
{
IDistributedCache redisCache = (IDistributedCache)
KernelMapper.ServiceProvider.GetService(typeof(IDistributedCache));
string key = Guid.NewGuid().ToString();
byte[] value = Encoding.ASCII.GetBytes(dataItem);
var options = new DistributedCacheEntryOptions();
options.SetSlidingExpiration(TimeSpan.FromMinutes(10));
//Set the cache
await redisCache.SetAsync(key, value, options);
byte[] cacheValue = await redisCache.GetAsync(key);
Assert.IsTrue(dataItem.Equals(Encoding.ASCII.GetString(cacheValue),
StringComparison.CurrentCultureIgnoreCase));
await redisCache.RemoveAsync(key);
var cacheValue2 = await redisCache.GetAsync($"{key}");
Assert.IsTrue(cacheValue2 == null);
}
}
Product | Versions 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.
-
net8.0
- Microsoft.Extensions.Caching.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- StackExchange.Redis (>= 2.8.16)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added json mimetype in list