EasyCaching——超级强大的开源缓存库

本文介绍了EasyCaching,一个开源缓存库,详细讲解了如何在ASP.NETCore中通过Nuget安装并配置内存缓存,包括使用InMemory缓存的两种方法和在控制器中调用EasyCachingProvider的操作。
摘要由CSDN通过智能技术生成

目录

EasyCaching

在Memory的使用

项目地址


EasyCaching

EasyCaching 是一个开源的缓存库,包含了缓存的基本用法和一些高级用法,可以帮助我们更轻松地处理缓存!支持 内存缓存,Redis,CSRedis,Memcached,SQLite 缓存 ,磁盘缓存,LiteDB 缓存FasterKv (混合内存和磁盘),只需要引用相对应得包即可

在Memory的使用

1、通过 Nuget 安装包

Install-Package EasyCaching.InMemory

2、Startup 类中的配置 有两种方法可以配置缓存提供程序。

通过 C# 代码:

public class Startup
{
   //...

   public void ConfigureServices(IServiceCollection services)
   {
       //other services.

       //Important step for In-Memory Caching
       services.AddEasyCaching(options =>
       {
           // use memory cache with a simple way
           options.UseInMemory("default");

           // use memory cache with your own configuration
           options.UseInMemory(config => 
           {
               config.DBConfig = new InMemoryCachingOptions
               {
                   // scan time, default value is 60s
                   ExpirationScanFrequency = 60, 
                   // total count of cache items, default value is 10000
                   SizeLimit = 100,     

                   // below two settings are added in v0.8.0
                   // enable deep clone when reading object from cache or not, default value is true.
                   EnableReadDeepClone = true,
                   // enable deep clone when writing object to cache or not, default value is false.
                   EnableWriteDeepClone = false,
               };
               // the max random second will be added to cache's expiration, default value is 120
               config.MaxRdSecond = 120;
               // whether enable logging, default is false
               config.EnableLogging = false;
               // mutex key's alive time(ms), default is 5000
               config.LockMs = 5000;
               // when mutex key alive, it will sleep some time, default is 300
               config.SleepMs = 300;
           }, "default1");
       });
   }
}

或者,您可以将配置存储在 .appsettings.json

public class Startup
{
    //...

    public void ConfigureServices(IServiceCollection services)
    {
        //other services.

        //Important step for In-Memory Caching
        services.AddEasyCaching(options =>
        {
            //use memory cache
            options.UseInMemory(Configuration, "default", "easycaching:inmemory");
        });
    }
}

3、调用 EasyCachingProvider 以下代码演示如何在 ASP.NET Core Web API 中使用 EasyCachingProvider。

[Route("api/[controller]")]
public class ValuesController : Controller
{
    private readonly IEasyCachingProvider _provider;

    public ValuesController(IEasyCachingProvider provider)
    {
        this._provider = provider;
    }

    [HttpGet]
    public string Get()
    {
        //Remove
        _provider.Remove("demo");

        //Set
        _provider.Set("demo", "123", TimeSpan.FromMinutes(1));

        //Get
        var res = _provider.Get("demo", () => "456", TimeSpan.FromMinutes(1));

        //Get without data retriever
        var res = _provider.Get<string>("demo");

        //Remove Async
        await _provider.RemoveAsync("demo");

        //Set Async
        await _provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1));   

        //Get Async  
        var res = await _provider.GetAsync("demo",async () => await Task.FromResult("456"), TimeSpan.FromMinutes(1));   

        //Get without data retriever Async
        var res = await _provider.GetAsync<string>("demo");
    }
}

项目地址

https://easycaching.readthedocs.io/

引入地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值