Mac OS 安装 Redis(用于连 Redis 服务器,方便查看数据):https://redis.io/topics/quickstart

  1. wget http://download.redis.io/redis-stable.tar.gz(没有wget命令,手动下载)
  2. tar xvzf redis-stable.tar.gz
  3. cd redis-stable
  4. make
  5. sudo make install
  6. 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

参考资料:

ASP.NET Core 使用 Redis 客户端的更多相关文章

  1. asp.net core 使用 Redis 和 Protobuf

    asp.net core 使用 Redis 和 Protobuf 前言 上篇博文介绍了怎么样在 asp.net core 中使用中间件,以及如何自定义中间件.项目中刚好也用到了Redis,所以本篇就介 ...

  2. ASP.NET Core 使用 Redis 实现分布式缓存:Docker、IDistributedCache、StackExchangeRedis

    ASP.NET Core 使用 Redis 实现分布式缓存:Docker.IDistributedCache.StackExchangeRedis 前提:一台 Linux 服务器.已安装 Docker ...

  3. asp.net Core 使用redis(StackExchange.Redis)

    原文:asp.net Core 使用redis(StackExchange.Redis) 一.添加配置(appsettings.json) "Redis": { "Def ...

  4. ASP.NET Core与Redis搭建一个简易分布式缓存

    ​本文主要介绍了缓存的概念,以及如何在服务器内存中存储内容.今天的目标是利用IDistributedCache来做一些分布式缓存,这样我们就可以横向扩展我们的web应用程序. 在本教程中,我将使用Re ...

  5. ASP.NET Core 使用 Redis 和 Protobuf 进行 Session 缓存

    前言 上篇博文介绍了怎么样在 asp.net core 中使用中间件,以及如何自定义中间件.项目中刚好也用到了Redis,所以本篇就介绍下怎么样在 asp.net core 中使用 Redis 进行资 ...

  6. 负载均衡的场景下ASP.NET Core如何获取客户端IP地址

    在ASP.NET中,使用负载均衡时,可以通过ServerVariables获取客户端的IP地址. var ip = request.ServerVariables["HTTP_X_FORWA ...

  7. Asp.net Core 使用Redis存储Session

    前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(middleware). 对于Session来说褒贬不一, ...

  8. asp.net core 运用 Redis 配置步骤

    Redis可以用来存储session或直接存储键值对 首先要有asp.net core的项目,可以是webapi 或者MVC项目,还有有本地的Redis或者在远程服务器上,具体的安装就不讲述了 以下是 ...

  9. 在ASP.NET Core中获取客户端IP地址

    随着ASP.NET的发展,有不同的方式从请求中访问客户端IP地址.WebForms和MVC Web应用程序只是访问当前HTTP上下文的请求. var ip = HttpContext.Current. ...

随机推荐

  1. Maven 项目pom.xml报错

    Maven项目报 Failure to transfer org.apache.maven.plugins:maven-*-plugin:pom 原因是maven的plugin并未下载到本地 或者本地 ...

  2. TCollector

    TCollector tcollector is a client-side process that gathers data from local collectors and pushes th ...

  3. python之socket模块

    UDP client #!/usr/bin/env python2.7 #-*-coding:utf-8 -*- import socket s=socket.socket(socket.AF_INE ...

  4. hibernate:There is a cycle in the hierarchy! 造成死循环解决办法

    下面是报的异常:在网上搜了关于:There is a cycle in the hierarchy!,才知道原来是因为死循环造成的!解决了好久,没有成功,后台不得已请教老大,老大说是因为在使用JSON ...

  5. IIFE(立即执行函数表达式)

    我们经常会看到这样的写法: ;(fuction () { // do something })() 这就是一个简单的IIFE(立即执行函数表达式,immediately-invoked functio ...

  6. [最短路]P1828 香甜的黄油 Sweet Butter

    题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...

  7. a标签嵌套解决方案

    在实际网页布局之中,我们有时候需要一整块点击区域中间还要有部分按钮点击,也就是需要a标签嵌套a标签,如下: <!-- a标签进行嵌套的时候 --><a href="#hao ...

  8. redux中间件的原理——从懵逼到恍然大悟

    前言react已经出来很久了,其生态圈之庞大,一锅炖不下!各种react-xx,已让我们不堪重负,github上随便一个demo,引入的模块至少都是五指之数+.看着头疼,嚼之无味…….在此建议新学者, ...

  9. Function Programming - 柯里化(curry)

    看到一篇非常不错的文章,这里分享给大家:http://www.jianshu.com/p/fa3568087881. 首先,柯里化的定义:你可以只透过部分的参数呼叫一个function,它会回传一个f ...

  10. QuickTime视频解析问题

    在QuickTime中可以解析出视频并播放视频,解析的格式后缀名为.mov,之后将该视频导入到Unity Project中,显示未解析到视频文件,本来应该会自动生成MovieTexture材质,但是并 ...