https://github.com/iharyakimush/community-extensions-caching
Combine In-Memory and Distributed Caching in ASP.NET Core
https://github.com/iharyakimush/community-extensions-caching
asp-net-core cache distributed-cache in-memory-caching
Last synced: 5 months ago
JSON representation
Combine In-Memory and Distributed Caching in ASP.NET Core
- Host: GitHub
- URL: https://github.com/iharyakimush/community-extensions-caching
- Owner: IharYakimush
- License: mit
- Created: 2018-07-23T11:52:41.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-26T12:35:46.000Z (almost 7 years ago)
- Last Synced: 2025-01-18T14:09:23.880Z (5 months ago)
- Topics: asp-net-core, cache, distributed-cache, in-memory-caching
- Language: C#
- Homepage:
- Size: 76.2 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Combine In-Memory and Distributed Caching
Standard version of `IMemoryCache` and `IDistributedCache` from `Microsoft.Extensions.Caching` allows to register only 1 instance in `IServiceCollection`.
This project allows you to:
- Use generic versions of `IMemoryCache` and `IDistributedCache` to be able to manage multiple cache instances
- Use strongly typed objects with `IDistributedCache` instead of byte arrays
- Use `ICombinedCache` combined cache which try to get value from memory cache first and perform fallback to distributed cache
- Use App.Metrics to monitor cache hit ration and performance# Sample
### Register caches
```
public void ConfigureServices(IServiceCollection services)
{
// Register memory cache
services.AddMemoryCache();// Allow to collect App.Metrics for memory cache
services.AddMetricsToMemoryCache();// Register Redis distributed cache
services.AddRedisDistributedCache(options =>
{
options.Configuration = "redis";
options.InstanceName = "redis";
});// Allow to collect App.Metrics for distributed cache
services.AddMetricsToDistributedCache();// Register combined cache
services.AddCombinedCache();services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
```### Use caches
```
public class ValuesController : ControllerBase
{
private readonly IMemoryCache mem;
private readonly IDistributedCache dist;
private readonly ICombinedCache comb;public ValuesController(
IMemoryCache onlyMemoryCache,
IDistributedCache onlyDistributedCache,
ICombinedCache combinedCache)
{
mem = onlyMemoryCache ?? throw new ArgumentNullException(nameof(onlyMemoryCache));
dist = onlyDistributedCache ?? throw new ArgumentNullException(nameof(onlyDistributedCache));
comb = combinedCache ?? throw new ArgumentNullException(nameof(combinedCache));
}public ActionResult> Get()
{
string dateString = DateTime.UtcNow.ToString("O");return new string[]
{
"v1mem-" + this.mem.GetOrSetValue("v1", () => dateString),"v1dist-" + this.dist.GetOrSetValue("v1", () => dateString),
"v1comb-" + this.comb.GetOrSetValue("v1", () => dateString)
};
}
}
```### Sample project
https://github.com/IharYakimush/community-extensions-caching/tree/master/Community.Extensions.Caching.Sample# Grafana Dashboard
Grafana dashboard for InfluxDB available at https://github.com/IharYakimush/community-extensions-caching/tree/master/GrafanaDashbord.json# NuGet
- Base abstractions and memory cache: https://www.nuget.org/packages/Community.Extensions.Caching/
- Redis distributed cache: https://www.nuget.org/packages/Community.Extensions.Caching.Redis/
- App.Metrics for caches: https://www.nuget.org/packages/Community.Extensions.Caching.AppMetrics/