• 三分钟学会Redis在.NET Core中做缓存中间件


    转自:ZaraNetcnblogs.com/ZaraNet/p/11837518.html

    大家好,今天给大家说明如何在.NET Core中使用Redis,我们在想要辩论程序的好与坏,都想需要一个可视化工具,我经常使用的是一位国内大牛开发的免费工具,其Github地址为: https://github.com/qishibo/AnotherRedisDesktopManager/releases ,它真的很给力,Redis的安装在 https://github.com/MicrosoftArchive/redis/releases,我一般使用的EasyCaching用于做缓存抽象层,首先创建一个.NET Core API 项目,随后nuget安装 EasyCaching.Core 以及 EasyCaching.Redis 。

    复制代码
    public void ConfigureServices(IServiceCollection services)
            {
                services.AddEasyCaching(options=> {
                    options.UseRedis(configure => {
                        configure.DBConfig.Endpoints.Add(
                            new EasyCaching.Core.Configurations.ServerEndPoint("localhost",6379)
                        );
                        configure.DBConfig.AllowAdmin = true;
                    },"RedisExample");
                });
                services.AddControllers();
            }
    复制代码

       随后在Startup中注册中间件,首先启动添加EasyCaching的服务,在向启动添加EasyCaching的某些选项,可以看到AddEasyCaching的过程是这样的。

    //  EasyCaching service collection extensions.
        public static class EasyCachingServiceCollectionExtensions
        {
            public static IServiceCollection AddEasyCaching(this IServiceCollection services, Action<EasyCachingOptions> setupAction);
        }

       UseRedis 方法的第二个参数,适用于Repository的选择哪个RedisClient实例,这是非常有利的;我们创建一个API,名为 RedisController ,其中依赖注入我们的服务。

    复制代码
    [Route("/Redis")]
        [ApiController]
        public class RedisController : ControllerBase
        {
            private IEasyCachingProvider cachingProvider;
            private IEasyCachingProviderFactory easyCachingProviderFactory;
            public RedisController(IEasyCachingProviderFactory cachingProviderFactory)
            {
                this.easyCachingProviderFactory = cachingProviderFactory;
                this.cachingProvider = cachingProviderFactory.GetCachingProvider("RedisExample");
            }
            [HttpGet("Demo")]
            public IActionResult SetRedisItem()
            {
                this.cachingProvider.Set("zaranet use easycaching", "this is my value", TimeSpan.FromDays(100));
                return Ok();
            }
        }
    复制代码

      点击启动,访问到 https://localhost:port/Redis/Demo 中,使用可视化工具查看,发现OK了。

     不光如何,我们我们进行了赋值,现在应该还需要一个获取的操作。

    [HttpGet("Get")]
            public IActionResult GetRedisItem()
            {
               var item =  this.cachingProvider.Get<string>("zaranet use easycaching");
               return Ok(item);
            }

     就这样,你就可以在.NET Core中使用Redis去做你觉得有价值的事情,都是非常简单的事情。

  • 相关阅读:
    js解析json数据
    json.stringify
    [Eclipse的Maven项目搭建,仅为测试Maven功能]如何在Eclipse下搭建Maven项目
    在 Windows 中配置Maven:
    jsp引入文件时候经常遇到的${ctx}
    <%%> <%! %> <%=%> <%-- --%> jsp中jstl一些运用
    Spring MVC之@RequestBody, @ResponseBody 详解
    Spring MVC之@RequestMapping 详解
    Spring MVC之@RequestParam @RequestBody @RequestHeader 等详
    @RequiresPermissions 解释
  • 原文地址:https://www.cnblogs.com/fei686868/p/11856063.html
Copyright © 2020-2023  润新知