GithubHelp home page GithubHelp logo

easycache's Introduction

EasyCache

Easy Cache For C#. Supports Inmemory cache only for now

Usage

  1. Add EasyCache to you DI
// startup.cs or program.cs
builder.Services.AddMemoryCache();
builder.Services.UseEasyCache();
  1. Inject IEasyCacheClient to use it.

  2. Define a method to retrieve data

  3. Call UseCache

// Creates a cache manager that returns List<WeatherForecast>, uses the method GetForecast to get data.
// Cache is maintained using keys. Here, the second parameter, an object, is the key.
// Key can be anything, string, object.

// To get the actual data, call cache.GetData(). Its an ansyn function that returns the data, either from cache or from your source.

var cache = _easyCacheClient.UseCache<List<WeatherForecast>>(GetForecast, new
{
    key = "weather_items",
    id = 45
});
var keys = _easyCacheKeyManager.GetKeys();
return Ok(new
{
    data = (await cache.GetData()).ToList(),
    keys = keys
});
  1. The default cache duration is 1 minute, after that, the cache will be invalidated and the next time the cache is used, it will invoke your function to get the data.
  2. This library uses MemoryCache under the hood, and the cache configuration is possible using https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.caching.memory.memorycacheentryoptions?view=dotnet-plat-ext-7.0 .

Methods

  1. IEasyCacheManager#InvalidateCache

You can call this method to invalidate your cache. Once you call this, the next time the cache is accessed, your method will be called.

var cache = _easyCacheClient.UseCache<List<WeatherForecast>>(GetForecast, new
{
    key = "weather_items",
    id = 45
});
// perform some task

cache.InvalidateCache()
  1. IEasyCacheClient#InvalidateCache

This method performs identical to above method, but requires that cache key be present

_easyCacheClient.InvalidateCache(new
{
    key = "weather_items",
    id = 45
});
  1. IEasyCacheKeys#GetKeys

This method can be called to retrieve the list of cache keys registered in your application. Will only contain the keys that have been used.

easycache's People

Contributors

niroula-kushal avatar

Stargazers

Biraj Mainali avatar

Watchers

James Cloos avatar  avatar

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.