StashCache 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package StashCache --version 1.0.1
NuGet\Install-Package StashCache -Version 1.0.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="StashCache" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StashCache --version 1.0.1
#r "nuget: StashCache, 1.0.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 StashCache as a Cake Addin
#addin nuget:?package=StashCache&version=1.0.1

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

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;
    }
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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. 
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,081 9/4/2021
1.0.1 299 8/28/2021
1.0.0 304 8/26/2021