Redis 中文文档 Redis 中文文档
指南
redis.io (opens new window)
指南
redis.io (opens new window)
  • 关于
    • Redis 开源治理
    • Redis 发布周期
    • Redis 赞助商
  • 入门
  • 数据类型
  • Redis Stack
  • 命令
  • 手册

EasyCaching is an open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easily!

CI Build Status


Platform Build Server Master Status Dev Status
:--- :--- :--- :---
Github Action Linux/Windows

Nuget Packages


Package Name Version Downloads
:--- :--- :---
EasyCaching.Core
EasyCaching.InMemory
EasyCaching.Redis
EasyCaching.Memcached
EasyCaching.SQLite
EasyCaching.HybridCache
EasyCaching.CSRedis
EasyCaching.Interceptor.Castle
EasyCaching.Interceptor.AspectCore
EasyCaching.Serialization.MessagePack
EasyCaching.Serialization.Json
EasyCaching.Serialization.Protobuf
EasyCaching.Bus.RabbitMQ
EasyCaching.Bus.Redis
EasyCaching.Bus.CSRedis
EasyCaching.ResponseCaching
EasyCaching.Disk
EasyCaching.LiteDB
EasyCaching.Serialization.SystemTextJson

Basic Usages


Step 1 : Install the package


Choose caching provider that you need and install it via Nuget.

  1. ``` sh
  2. Install-Package EasyCaching.InMemory
  3. Install-Package EasyCaching.Redis
  4. Install-Package EasyCaching.SQLite
  5. Install-Package EasyCaching.Memcached

  6. ```

Step 2 : Configure Startup class


Each caching provider has it's own configuration options.

Here is a sample configuration for InMemory and Redis caching provider.

  1. ``` cs
  2. public class Startup
  3. {
  4.     //...

  5.     public void ConfigureServices(IServiceCollection services)
  6.     {
  7.         //configuration
  8.         services.AddEasyCaching(options =>
  9.         {
  10.             //use memory cache that named default
  11.             options.UseInMemory("default");

  12.             // // use memory cache with your own configuration
  13.             // options.UseInMemory(config =>
  14.             // {
  15.             //     config.DBConfig = new InMemoryCachingOptions
  16.             //     {
  17.             //         // scan time, default value is 60s
  18.             //         ExpirationScanFrequency = 60,
  19.             //         // total count of cache items, default value is 10000
  20.             //         SizeLimit = 100
  21.             //     };
  22.             //     // the max random second will be added to cache's expiration, default value is 120
  23.             //     config.MaxRdSecond = 120;
  24.             //     // whether enable logging, default is false
  25.             //     config.EnableLogging = false;
  26.             //     // mutex key's alive time(ms), default is 5000
  27.             //     config.LockMs = 5000;
  28.             //     // when mutex key alive, it will sleep some time, default is 300
  29.             //     config.SleepMs = 300;
  30.             // }, "m2");

  31.             //use redis cache that named redis1
  32.             options.UseRedis(config =>
  33.             {
  34.                 config.DBConfig.Endpoints.Add(new ServerEndPoint("127.0.0.1", 6379));
  35.             }, "redis1")
  36.             .WithMessagePack()//with messagepack serialization
  37.             ;            
  38.         });    
  39.     }    
  40. }
  41. ```

Step 3 : Write code in your controller


  1. ``` cs
  2. [Route("api/[controller]")]
  3. public class ValuesController : Controller
  4. {
  5.     // //when using single provider
  6.     // private readonly IEasyCachingProvider _provider;
  7.     //when using multiple provider
  8.     private readonly IEasyCachingProviderFactory _factory;

  9.     public ValuesController(
  10.         //IEasyCachingProvider provider,
  11.         IEasyCachingProviderFactory factory
  12.         )
  13.     {
  14.         //this._provider = provider;
  15.         this._factory = factory;
  16.     }

  17.     [HttpGet]
  18.     public string Handle()
  19.     {
  20.         //var provider = _provider;
  21.         //get the provider from factory with its name
  22.         var provider = _factory.GetCachingProvider("redis1");    

  23.         //Set
  24.         provider.Set("demo", "123", TimeSpan.FromMinutes(1));

  25.         //Set Async
  26.         await provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1));                  
  27.     }
  28. }
  29. ```

Documentation


Detailed EasyCaching documentation can be found here.

Extension Libs


EasyCaching.Extensions

EasyCaching.Extensions.EasyCompressor

Examples


See sample

Todo List


See ToDo List

Contributing


Pull requests, issues and commentary!

License

Last Updated: 2023-09-03 19:17:54