AspectCache 0.2.1
dotnet add package AspectCache --version 0.2.1
NuGet\Install-Package AspectCache -Version 0.2.1
<PackageReference Include="AspectCache" Version="0.2.1" />
<PackageVersion Include="AspectCache" Version="0.2.1" />
<PackageReference Include="AspectCache" />
paket add AspectCache --version 0.2.1
#r "nuget: AspectCache, 0.2.1"
#addin nuget:?package=AspectCache&version=0.2.1
#tool nuget:?package=AspectCache&version=0.2.1
aspnetcore-aspect-cache
ASP.NET Core Reusable cache implementation
You can cache any serializable type of entity with explicit key
[CachedAction(CacheKey ="Single", EntityType = typeof(Item))]
or use runtime key factory
[CachedAction(CacheKey ="Single")]
Provides:
- use cached value before executing controller's action
- auto-cache response after action execution
Usage
Cache implementation
You have to implement AspectCache.ICache
in your project. That allows to use any cache implementation for example Memory Cache
public class InMemoryCache : ICache
{
private readonly IMemoryCache _memoryCache;
public InMemoryCache(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
public T Get<T>(string key)
{
return _memoryCache.Get<T>(key);
}
public void Remove(string key)
{
_memoryCache.Remove(key);
}
public void Set<T>(string key, object value)
{
_memoryCache.Set<T>(key, (T)value);
}
}
Key Factory implementation
In real-life solutions sometimes you base the cache key on the property of time, or you build a complex map that needs to be evaluated.
In that scenarios you would need implementation of ICacheKeyFactory<T>
Assuming a hash key of entity Item equals "id" from Query String
public class ItemKeyFactory : ICacheKeyFactory<Item>
{
public string Create(HttpContext context)
{
return context.Request.Query["id"];
}
}
Init
Register your implementations in service collection
services.AddSingleton<ICache, InMemoryCache>();
services.AddSingleton<ICacheKeyFactory<Item>, ItemKeyFactory>();
CachedAction
Finally decorate your actions with attributes to either use a const cache key or to create a cache key during runtime based on key factory implementation
[HttpGet]
[CachedAction(CacheKey ="Single", EntityType = typeof(Item))]
[Route("single")]
public ActionResult Get()
{
var random = new Random();
var item = new Item
{
Value = random.Next(),
Id = random.Next().ToString()
};
return Ok(item);
}
[HttpGet]
[Route("by")]
[CachedAction(EntityType = typeof(Item))]
public ActionResult GetById()
{
var random = new Random();
var item = new Item
{
Id = Request.Query["id"].ToString(),
Value = random.Next()
};
return Ok(item);
}
Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.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 was computed. |
.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. |
-
.NETStandard 2.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 2.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.