ASP.NET Core 使用 Redis 客户端
Mac OS 安装 Redis(用于连 Redis 服务器,方便查看数据):https://redis.io/topics/quickstart
wget http://download.redis.io/redis-stable.tar.gz(没有wget命令,手动下载)tar xvzf redis-stable.tar.gzcd redis-stablemakesudo make installmake test(测试安装是否成功)
安装好之后,我们就可以使用redis-cli命令了,
连接 Redis 服务器:
$ redis-cli -h 12.22.10.33 -p 6379 -a "password"
12.22.10.33:6379> ping
PONG
查看 key 是否存在(1 表示存在):
$ exists test_key
(integer) 1
查看指定 key 的值类型:
$ type test_key
string
获取指定 key 的字符串值:
$ get test_key
"hello world"
上面是一些简单的redis-cli命令,更多命令查看:http://www.runoob.com/redis/redis-commands.html
ASP.NET Core 使用 Redis 客户端,最好的选择当然是 StackExchange.Redis,GitHub 地址:https://github.com/StackExchange/StackExchange.Redis
使用很简单,首先安装程序包:
PM> Install-Package StackExchange.Redis
使用简单示例:
static void Main(string[] args)
{
//var configurationOptions = new ConfigurationOptions
//{
// EndPoints =
// {
// "10.11.22.1", "6379",
// "10.11.22.2", "6379",
// "10.11.22.3", "6379"
// },
// Password = "aqsea3491"
//};
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("10.11.22.1:6379,10.11.22.1:6379,10.11.22.1:6379,password=123456");
IDatabase db = redis.GetDatabase();
string value = "abcdefg";
db.StringSet("test_key", value);
value = db.StringGet("test_key");
Console.WriteLine(value);
Console.ReadLine();
}
当然,如果用于生产环境的话,需要再进行封装下,如果我们使用的是 ASP.NET Core 的话,还有一种不用自己封装的选择,那就是 Microsoft.Extensions.Caching.Redis,GitHub 地址:https://github.com/aspnet/Caching/tree/dev/src/Microsoft.Extensions.Caching.Redis
Microsoft.Extensions.Caching.Redis 是微软自己封装的 Redis 组件,内部使用的还是 StackExchange.Redis,但在 ASP.NET Core 中使用起来,非常简单。
首先安装程序包:
PM> Microsoft.Extensions.Caching.Redis
Startup.ConfigureServices配置:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
// For redis
// install-package Microsoft.Extensions.Caching.Redis
services.AddDistributedRedisCache(options =>
{
options.InstanceName = "";
options.Configuration = "10.11.22.1:6379,10.11.22.1:6379,10.11.22.1:6379,password=123456";
});
}
简单使用:
public class ValuesController : Controller
{
private readonly IDistributedCache _distributedCache;
public ValuesController(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}
// GET api/values
[HttpGet]
public async Task<string> Get()
{
// redis operate
var key = "test_key";
var valueByte = await _distributedCache.GetAsync(key);
if (valueByte == null)
{
await _distributedCache.SetAsync(key, Encoding.UTF8.GetBytes("world22222"), new DistributedCacheEntryOptions().SetSlidingExpiration(DateTimeOffset.Now.AddSeconds(3000)));
valueByte = await _distributedCache.GetAsync(key);
}
var valueString = Encoding.UTF8.GetString(valueByte);
return valueString;
}
}
测试过程中,发现 Microsoft.Extensions.Caching.Redis 有一个问题,虽然IDistributedCache提供了SetStringAsync方法,但实际插入到 Redis 的值类型,并不是string,而是hash,可以用redis-cli命令进行测试:
114.55.56.213:6379> get test_key
(error) WRONGTYPE Operation against a key holding the wrong kind of value
114.55.56.213:6379> type test_key
hash
所以,没办法,只能使用SetAsync,然后读取再由byte转换为string。
另外,微软封装的Caching,除了 Microsoft.Extensions.Caching.Redis,还有:
- Microsoft.Extensions.Caching.Abstractions
- Microsoft.Extensions.Caching.Memory
- Microsoft.Extensions.Caching.SqlServer(使用 SqlServer 数据库,作为缓存存储)
详细使用,请查看:Working with a distributed cache
参考资料:
- redis-cli, the Redis command line interface
- MAC下 安装 redis
- Redis 常用命令
- StackExchange.Redis ConnectionMultiplexer.Connect() Intermittently Works
- StackExchange.Redis simple C# Example
- Redis Cache in ASP.NET Core
- Using Redis Cache in .net Core
- redis 报Operation against a key holding the wrong kind of value 警告的解决方法
ASP.NET Core 使用 Redis 客户端的更多相关文章
- asp.net core 使用 Redis 和 Protobuf
asp.net core 使用 Redis 和 Protobuf 前言 上篇博文介绍了怎么样在 asp.net core 中使用中间件,以及如何自定义中间件.项目中刚好也用到了Redis,所以本篇就介 ...
- ASP.NET Core 使用 Redis 实现分布式缓存:Docker、IDistributedCache、StackExchangeRedis
ASP.NET Core 使用 Redis 实现分布式缓存:Docker.IDistributedCache.StackExchangeRedis 前提:一台 Linux 服务器.已安装 Docker ...
- asp.net Core 使用redis(StackExchange.Redis)
原文:asp.net Core 使用redis(StackExchange.Redis) 一.添加配置(appsettings.json) "Redis": { "Def ...
- ASP.NET Core与Redis搭建一个简易分布式缓存
本文主要介绍了缓存的概念,以及如何在服务器内存中存储内容.今天的目标是利用IDistributedCache来做一些分布式缓存,这样我们就可以横向扩展我们的web应用程序. 在本教程中,我将使用Re ...
- ASP.NET Core 使用 Redis 和 Protobuf 进行 Session 缓存
前言 上篇博文介绍了怎么样在 asp.net core 中使用中间件,以及如何自定义中间件.项目中刚好也用到了Redis,所以本篇就介绍下怎么样在 asp.net core 中使用 Redis 进行资 ...
- 负载均衡的场景下ASP.NET Core如何获取客户端IP地址
在ASP.NET中,使用负载均衡时,可以通过ServerVariables获取客户端的IP地址. var ip = request.ServerVariables["HTTP_X_FORWA ...
- Asp.net Core 使用Redis存储Session
前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(middleware). 对于Session来说褒贬不一, ...
- asp.net core 运用 Redis 配置步骤
Redis可以用来存储session或直接存储键值对 首先要有asp.net core的项目,可以是webapi 或者MVC项目,还有有本地的Redis或者在远程服务器上,具体的安装就不讲述了 以下是 ...
- 在ASP.NET Core中获取客户端IP地址
随着ASP.NET的发展,有不同的方式从请求中访问客户端IP地址.WebForms和MVC Web应用程序只是访问当前HTTP上下文的请求. var ip = HttpContext.Current. ...
随机推荐
- C#中制作MDI窗体
在VB中做 MDI窗体很简单.在C#里就没有这个轻松了,不过还是很方便的. 首先在C#里添加一个窗体,命名为MdiMain,将其IsMdiContainer设定成true,这样MDI主窗体就建立了.然 ...
- 无限分级Repeater递归实现:读取一次数据库,使用LINQ2SQL技术,支持排序&显示隐藏
预览效果图: Selenium 数据库结构: id(int) classname(string) parentid(int) sort(int用于显示与排序) 1 家居 0 1 2 家电 0 ...
- 解决Webstom 2017中,输入法候选框无法显示问题
一.问题: 如题,IDE编辑界面内,输入法的候选框没法显示,有时需要打中文注释,非常麻烦. 原因:IDE自带的OpenJDK与输入法存在冲突 二.解决: (1)在编辑界面,双shift,搜索:swit ...
- CentOS下安装Tomcat 8
CentOS下安装Tomcat 8 安装Tomcat8 去http://tomcat.apache.org/download-80.cgi下载Tomcat8的安装文件apache-tomcat-8.0 ...
- 前端使用d3.js调用地图api 进行数据可视化
前段时间自己研究了demo就是把某个区域的某个位置通过经纬度在地图上可视化.其实就是使用了第三方插件,比现在比较火的可视化插件d3.js echart.js.大致思路就是,把要用到的位置的geojso ...
- tsung压力测试——tcp测试tsung.xml配置模版说明
<?xml version="1.0"?> <!DOCTYPE tsung SYSTEM "/usr/local/share/tsung/tsung-1 ...
- 第一份开发工作,边学边做android
我刚刚毕业,在培训学校学的Java web开发,虽然学的没有大学生那么丰富细致,没有他们理论基础扎实,但是这是我学习软件开发的唯一方式了. 从小学我学习就是倒数2.3等,所有人都认为我是个没法学习的孩 ...
- 利用可变参数模拟Printf()函数实现一个my_print()函数和调用可变参数注意的陷阱!
可变参数函数的实现与函数调用的栈结构密切相关,正常情况下C的函数参数入栈规则为__stdcall, 它是从右到左的,即函数中的最右边的参数最先入栈. 例如,对于函数: void test(char a ...
- LKD: Chapter 6 Kernel Data Structures
这一章我们研究四种主要的数据结构: linked lists, queues, maps, binary trees. Linked Lists:(<linux/list.h>) 在lin ...
- Shell脚本数据备份