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.
<PackageVersion Include="StashCache" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="StashCache" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version 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.
#:package StashCache@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StashCache&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=StashCache&version=1.0.1
                    
Install as a Cake Tool

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.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 12,576 9/4/2021
1.0.1 486 8/28/2021
1.0.0 492 8/26/2021