Skip to content

OleConsignado/otc-caching

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Otc.Caching

Build Status

Otc.Caching is a simple distributed cache built on top of Microsoft.Extensions.Caching. It provides the convenient ITypedCache interface and the ability to switch between Redis, Sql Server and Memory storage engine without needs of recompile it.

Quickstart

Installation

Install the Otc.Caching.DistributedCache.All package from NuGet.org.

At startup, add DistributedCache to your service collection by calling AddDistributedCacheConfiguration extension method for IServiceCollection:

services.AddOtcDistributedCache(new DistributedCacheConfiguration(){
    // ... see DistributedCacheConfiguration for details
});

Usage

  1. Get the data from cache key 'my-cache-key-async', if does not found nothing, it will cache the value.
//Trying to get data from cache key 'my-cache-key-async',
//if doest not found nothing, it will be cached.
ITypedCache cache = ... // Get it by dependency injection
var cacheKey = "my-cache-key-async";

var myModelObj = await cache.GetAsync<MyModelClass>(cacheKey, TimeSpan.FromSeconds(30), async () => { 
    myModelObj = ... // retrieve the object from it source here
    return myModelObj;
} ));
  1. Get the data from cache key 'my-cache-get-key-async'.
//Trying to get data from cache key 'my-cache-get-key-async'.
ITypedCache cache = ... // Get it by dependency injection
var cacheKey = "my-cache-get-key-async";

var myModelObjFromCache = await cache.GetAsync<MyModelClass>(cacheKey);
  1. Cache the data with the cache key 'set-cache-async' and getting the data with the given key.
var cacheKey = "set-cache-async";

await typedCache.SetAsync(cacheKey, new User(), TimeSpan.FromSeconds(30));

var resultFromCache = await typedCache.GetAsync<User>("Test_SetAsync");