Giter Site home page Giter Site logo

api-cache's Introduction

API Cache

API Cache is a .NET Framework (>= v4.6.1) and .NET Core (>=2.1) library for in-memory cache management in ASP.NET applications.

The library provides a background agent that periodically clears in-memory cache.

Setup

Let's assume we have an in-memory cache:

public static class Cache
{
    public static IDictionary<string, object> Instance { get; } = new Dictionary<string, object>();
}

Create and setup instance of ICacheManager or IAsyncCacheManager in a static context:

var cacheManager = new CacheManager(
	// Method which clears cache
	clearCache: () => Cache.Instance.Clear(),
	// Method which returns cached data
	getCacheData: () => Cache.Instance,
	// Setup info logging 
	logInfo: (message, data) => Debug.WriteLine($"INFO: {message}{Environment.NewLine}{JsonConvert.SerializeObject(data)}"),
	// Setup error logging 
	logError: (message, data) => Debug.WriteLine($"ERROR: {message}{Environment.NewLine}{JsonConvert.SerializeObject(data)}"),
	// Method which returns interval in hours when cache needs to be cleared again
	getClearIntervalInHours: () => 1,
	// Method which returns background thread's sleep time in minutes before attempting to clear cache again
	getThreadSleepTimeInMinutes: () => 1);

Only for .NET Core, add "API Cache" services to dependency injection container using extension method:

public void ConfigureServices(IServiceCollection services)
{
    services.AddApiCache(cacheManager);
}

Usage:

Start background agent:

cacheManager.Start();  

Stop background agent:

cacheManager.Stop();  

Force to clear cache:

cacheManager.ClearCache();  

Get agent status and cached data:

Cache.Instance[Guid.NewGuid().ToString("N")] = Guid.NewGuid().ToString("N");

var cacheInfo = cacheManager.GetCacheInfo();  

var json = JsonConvert.SerializeObject(cacheInfo);  

The above code generates the below JSON:

{
    "agent": {
        "isStarted": true,
        "timestamp": "2019-04-24T15:17:04.2907437+00:00",
        "clearIntervalInHours": 1,
        "threadSleepTimeInMinutes": 5
    },
    "clearTime": "2019-04-24T15:17:04.233738+00:00",
    "data": {
        "681d8917ff674e0b8dc7a59d08a31bfd": "eb375c11fd1a41b0a5c2c53e754c5c6c"
    }
}

For additional usage related info please see Agero.Core.ApiCache.Web (.NET Framework) and Agero.Core.ApiCache.Web.Core (.NET Core).

api-cache's People

Contributors

vyshaksrishyla avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

chyuck manhngts

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.