StashCache 1.0.2

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

// Install StashCache as a Cake Tool
#tool nuget:?package=StashCache&version=1.0.2

StashCache

StashCache is an in-memory caching library for your .NET application. Under the hood, it uses the MemoryCache from Microsoft.Extensions.Caching.Memory.

Why StashCache?

Developers tend to procrastinate caching because oftentimes it is divorced from retrieving of data. StashCache is designed such that retrieving and caching of data is inside the same method block.

Download

StashCache is available on nuget.

Getting Started

See example in Sample.AspNetCore project in this repo.

  • Register the extension method for the service in Startup.ConfigureServices.
    public void ConfigureServices(IServiceCollection services)
    {
        // Import namespace:
        // using StashCache;
        services.AddStashCache();
    }
  • Choose cache key generator; or implement one as the need arises.

    TypeCacheKeyGenerator can be selected in order to generate cache key which is structured into: type/class + method + additional segments.

    // From Sample.AspNetCore WeatherForecastService class
    private static readonly ICacheKeyGenerator<TypeCacheKeyGenerator> CacheKeyGenerator = CacheKeyGeneratorFactory.GetCacheKeyGenerator<TypeCacheKeyGenerator>();

    private static readonly TimeSpan DefaultCacheExpiry = TimeSpan.FromHours(1);
  • Inject ILocalCache to a class which retrieves data
    private readonly ILocalCache _localCache;
    
    public WeatherForecastService(ILocalCache localCache)
    {
        _localCache = localCache;
    }
  • Lastly, implement caching in the same method block as the data retrieval.
    public async Task<IEnumerable<WeatherForecast>> GetAll(CancellationToken cancellationToken)
    {
        var cacheKey = CacheKeyGenerator.GenerateCacheKey<WeatherForecastService>();

        var result = await _localCache.GetOrAddAsync(cacheKey, async () =>
        {
            var summaries = await GetSummariesAsyc(); // This can be your database call
            return summaries;

        }, DefaultCacheExpiry, cancellationToken).ConfigureAwait(false);

        return result;
    }

Benchmark using BenchmarkDotNet

Method # of Cached Items Total # of retrievals Mean (μs) StdDev (μs) Allocated (KB)
GetOrAddAsync 100 1 61.05 2.416 57
GetOrAddAsync 100 10 609.78 5.298 568
GetOrAddAsync 100 100 5,829.17 121.375 5,675
GetOrAddAsync 100 500 29,906.00 201.832 28,376
GetOrAddAsync 500 500 156,081.09 2,215.348 142,444
GetOrAddAsync 1000 500 306,005.04 2,911.767 285,028
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
1.0.2 10,012 9/4/2021
1.0.1 299 8/28/2021
1.0.0 304 8/26/2021