AutoCache.Lib 1.0.0

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

// Install AutoCache.Lib as a Cake Tool
#tool nuget:?package=AutoCache.Lib&version=1.0.0                

AutoCache

A lib that makes memory caching a little cleaner. Contains one little helper method with the following interface

 T CacheRetrieve<T>(Func<T> callback, CacheItemPolicy cachePolicy = null)

CacheRetrieve will by default use a 5 minute sliding expiration policy for cached items. But can be overridden in the constructor.

What it does

It caches in memory the result of the the entire method it's used in, using the parameters + the method name as a cache key.

Example usage

Say you have this method in your backend:

public int SomeDataById(int id)
{
  using(var con = connectionFactory.CreateConnection()) 
  {
    return con.Query("a lot of data where id = @id", new { id = id })
  }
}

You can encapsulate it with AutoCache like this for quick and easy automatic memory caching:

public int SomeDataById(int id)
{
  return AutoCache.CacheRetrieve(() =>
  {
    using(var con = connectionFactory.CreateConnection()) 
    {
      return con.Query("a lot of data where id = @id", new { id = id })
    }
  });
}

In reality, AutoCache translates the method definition to:

public int SomeDataById(int id)
{
    var cacheKey = "SomeDataById" + id.ToString();
    if(MemoryCache.Default.Contains(cacheKey))
      return MemoryCache.Default[cacheKey];
     
    int result = 0
    using(var con = connectionFactory.CreateConnection()) 
    {
      result = con.Query("a lot of data where id = @id", new { id = id });
    }

    MemoryCache.Default.Add(new CacheItem(cacheKey, result), new CacheItemPolicy(0, 5, 0));
    return MemoryCache.Default[cacheKey];
  });
}
Product Compatible and additional computed target framework versions.
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AutoCache.Lib:

Package Downloads
DocumentLab-x64

OCR using Tesseract, ImageMagick and EmguCV and an advanced query language. See the GitHub page for language documentation.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 6,036 5/3/2018
1.0.0 1,044 5/1/2018