AutoCache.Lib 1.1.1

A small memory cache utility class that makes caching resource intensive operations in memory a little easier.

Install-Package AutoCache.Lib -Version 1.1.1
dotnet add package AutoCache.Lib --version 1.1.1
<PackageReference Include="AutoCache.Lib" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoCache.Lib --version 1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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];
  });
}

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];
  });
}

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.1.1 299 5/3/2018
1.0.0 235 5/1/2018