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 警告的解決方法
文章列表