博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1.6 WEB API NET CORE 使用Redis
阅读量:7146 次
发布时间:2019-06-29

本文共 1162 字,大约阅读时间需要 3 分钟。

 

 

一、Redis安装

https://www.cnblogs.com/cvol/p/9174078.html

 

二、新建.net core web api程序

选webapi 或者应用程序都可以

 

 三、使用Redis

用Nuget安装Microsoft.Extensions.Caching.Redis,别弄错了。

Program不动,配置在Startup.cs中改。

修改方法,加了红色部分。

public void ConfigureServices(IServiceCollection services)

{
services.AddMvc();
services.AddDistributedRedisCache(options =>
{
options.Configuration = "localhost";
options.InstanceName = "Demo";
});
}

 测试控制器

[Route("api/[controller]")]public class ValuesController : Controller{IDistributedCache Cache;public ValuesController(IDistributedCache cache){Cache = cache;}// GET api/values[HttpGet]public IEnumerable
Get(){string currentTime = Cache.GetStringAsync("test").Result;if (null == currentTime){currentTime = "va1" + DateTime.Now;Cache.SetAsync("test", Encoding.UTF8.GetBytes(currentTime));}return new string[] { currentTime, "value2" };}[Route("TestCount")][HttpGet]public int TestCount(int count){for (int i = 0; i < count; i++){Cache.SetAsync("test" + i, Encoding.UTF8.GetBytes(DateTime.Now.ToString()));var a = Cache.GetStringAsync("test" + i).Result;}return count;}}

四、运行结果,使用redis阅读器 ,执行一百万次结果

 

转载于:https://www.cnblogs.com/cvol/p/9176289.html

你可能感兴趣的文章
高通平台MSM8916LCM模块移植(一)-bootloader部分【转】
查看>>
oracle表空间不足相关问题解决办法
查看>>
CentOS-7 在windows server 2012下的虚拟机安装教程
查看>>
函数调用过程栈帧变化详解
查看>>
Android项目实战(三十二):圆角对话框Dialog
查看>>
Word或Excel里画柱状图和折线图组合体
查看>>
C# CRC16 查表法
查看>>
js中获取键盘事件
查看>>
面试(4)-spring-Spring面试题和答案
查看>>
请教 JTable 里的单元格如何使得双击进入单元格后,单元格的内容处于全选中状态...
查看>>
jQuery 各类判断函数汇总
查看>>
Android studio 分32位64位版本吗?
查看>>
UIcollectionView的使用(首页的搭建1)
查看>>
[原创]AM3352 + TPS65910 调试方法+调试记录
查看>>
.net基本数据类型操作
查看>>
docker 应用-2(Dockerfile 编写以及镜像保存提交)
查看>>
ubuntu 下安装查看pdf的工具
查看>>
UIApplication深入研究
查看>>
解决Ubuntu 12.04更新后 ”系统的网络服务与此版本的网络管理器不兼容“问题
查看>>
python变量
查看>>