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.gz
cd redis-stable
make
sudo make install
make 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. ...
随机推荐
- HTTPCLIENT 模拟登陆
第一步构建忽略https验证的httpclient public static CloseableHttpClient getHttpClient() throws Exception { SSLCo ...
- iOS开发针对对Masonry下的FPS优化讨论
今天博客的内容就系统的讨论一下Masonry对FSP的影响,以及如何更好的使用Masonry.如果你对iOS开发足够熟悉的话,那么对Masonry框架应该不陌生.简单的说,Masonry的诞生让Aut ...
- 转贴---Performance Counter(包含最全的Windows计数器解释)
http://support.smartbear.com/viewarticle/55773/ 这个Article中介绍了一个新工具,TestComplete,把其中涉及到性能计数器的部分摘抄出来了. ...
- 去除HTML选择——兼容IE、FireFox(document.onselectstart,样式)
引之:http://taoistwar.iteye.com/blog/278963 今天做一个拖动效果,在网上找了个模板,作发后发现一拖动就会选择其它页面部分,需要去除这个效果, 找了个模板看了下发现 ...
- 预加载(图片,css ,js)
图片预加载 new Image().src = 'http://img1.t.sinajs.cn/t35/skin/skin_008/skin.css'; //新浪(4) 非ie下预加载(js,css ...
- lua API 小记2
1. 创建lua虚拟机 lua_State *lua_newstate (lua_Alloc f, void *ud) 创建一个新的独立的lua虚拟机. 参数指定了内存分配策略及其参数, 注意, 让用 ...
- Pyhton函数篇(一)之函数中的形参与实参
1:什么是函数 函数其实就是带名字的代码块,用于完成一些具体的工作.如果我们在写一段程序的时候,需要多次用到同样的一个功能,如果每次都要重复写相同的代码,不仅会增加我们的代码量,更会让我们写出的代码让 ...
- nginx反向代理node.js获取客户端IP
使用Nginx做node.js程序的反向代理,会有这么一个问题:在程序中获取的客户端IP永远是127.0.0.1 如果想要拿到真实的客户端IP改怎么办呢? 一.首先配置Nginx的反向代理 proxy ...
- 【ASP.NET Core】运行原理之启动WebHost
ASP.NET Core运行原理之启动WebHost 本节将分析WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().Build ...
- 在Github发布自己的compile包
Android入门到转行做服务员--在Github发布自己的compile包 2017-12-05 15:27:10 这是一粒代码发布的第一篇博客,一粒代码从事android开发,近期打算开始搞搞博客 ...