大家好,今天给大家说明如何在.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",)
);
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());
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去做你觉得有价值的事情,都是非常简单的事情。

最新文章

  1. 关于handler
  2. jquery 判断checkbox是否选中几个版本的区别
  3. 首次构建android studio gradle 下载缓慢的问题
  4. delphi提示“Undeclared_identifier”的缺少引用单元列表
  5. 在win7的虚拟机中LINUX与winxp两客户机互通问题
  6. hdu 2460
  7. android中ScrollView和GridView/ListView共存时,ScrollView不在顶部的解决方法
  8. python并行任务之生产消费模式
  9. ios video标签部分mp4文件无法播放的问题
  10. angular何时触发脏检查机制
  11. 初识 go 语言:方法,接口及并发
  12. 生产者消费者synchronized wait notify
  13. Java插入排序算法
  14. git clone 后误删除了分离出来的文件怎么恢复?
  15. jenkins 登录远程机器并执行脚本,脚本中有后台执行的程序无法执行解决方法。
  16. CSS-弹性布局-动画-过渡
  17. python2 python3编码问题记录
  18. python 日志的配置,python对日志封装成类,日志的调用
  19. 深入annotation
  20. 【论文翻译】MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications

热门文章

  1. SEER流量众筹模块开发测试网络及使用文档发布
  2. 编程杂谈——std::vector与List&lt;T&gt;的性能比较
  3. GLSL 参考GIMP源码实现色彩平衡调节
  4. Django+小程序技术打造微信小程序助手 ✌✌
  5. http服务端架构演进
  6. DataStructure之线性表以及其实现
  7. java23种设计模式(二)抽象工厂模式
  8. plSql使用流程
  9. Web安全之爆破中的验证码识别~
  10. 掌握git基本功