AspectCache 0.2.1

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

// Install AspectCache as a Cake Tool
#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 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 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. 
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
0.2.1 600 8/8/2019
0.2.0 499 8/8/2019
0.1.0 513 8/2/2019