.Net Core連接redis主要有兩個庫:StackExchange.Redis和CsRedis。
1. ASP.NET Core 3.1使用StackExchange.Redis.Extensions.Core簡單教程。
[參考文章]:(https://www.chengxulvtu.com/stackexchange-redis-in-aspnet-core-3-1/)
小結:依賴於StackExchange.Redis。可以通過注冊服務,依賴注入IRedisCacheClient來訪問Redis,進行操作。功能還算齊全,有各種數據類型的操作函數、也有發布訂閱。
使用緩存查詢的例子代碼:
[HttpGet]
//使用緩存查詢的例子
public async Task<ActionResult<List<Product>>> GetProduct()
{
//先讀緩存
var list = await _redisClient.GetDbFromConfiguration().GetAsync<List<Product>>("products");
//redis沒數據,則訪問數據庫,並寫入緩存
if (list == null)
{
list = _service.Get();
await _redisClient.GetDbFromConfiguration().AddAsync("products", list, DateTimeOffset.Now.AddMinutes(5));
}
return list;
}
2. 使用StackExchange.Redis簡單教程。
這是StackExchange出的Redis庫,就是Stackoverflow,比較有名,star數也很多。
StackExchange.Redis的連接是多路復用的,且其操作redis的功能豐富。
例子里使用編寫好的靜態工具類,來操作Redis服務。其中Redis幫助類沒有使用依賴注入,使用了單例模式。
幫助類例子代碼:
/// <summary>
/// redis幫助類,簡單封裝string操作
/// </summary>
public class RedisHelper1
{
private static readonly object Locker = new object();
//連接多路復用器
private ConnectionMultiplexer _redisMultiplexer;
private IDatabase _db = null;
//單例模式
private static RedisHelper1 _instance = null;
public static RedisHelper1 Instance
{
get
{
if (_instance == null)
{
lock (Locker)
{
if (_instance == null)
{
_instance = new RedisHelper1();
}
}
}
return _instance;
}
}
public void InitConnect(IConfiguration configuration)
{
try
{
//連接redis服務器
string redisConnection = configuration.GetConnectionString("RedisConnectionString");
_redisMultiplexer = ConnectionMultiplexer.Connect(redisConnection);
_db = _redisMultiplexer.GetDatabase();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
_redisMultiplexer = null;
_db = null;
}
}
public RedisHelper1()
{
}
#region 通用key操作
/// <summary>
/// 判斷鍵是否存在
/// </summary>
public bool ExistsKey(string key)
{
return _db.KeyExists(key);
}
/// <summary>
/// 刪除鍵
/// </summary>
public bool DeleteKey(string key)
{
return _db.KeyDelete(key);
}
#endregion
#region string操作
/// <summary>
/// 保存單個key value
/// </summary>
/// <param name="key">鍵</param>
/// <param name="value">值</param>
/// <param name="expiry">過期時間</param>
public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?))
{
return _db.StringSet(key, value, expiry);
}
/// <summary>
/// 獲取單個key的值
/// </summary>
public RedisValue GetStringKey(string key)
{
return _db.StringGet(key);
}
/// <summary>
/// 保存一個對象
/// </summary>
/// <typeparam name="T">對象的類型</typeparam>
public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
{
string json = JsonConvert.SerializeObject(obj);
return _db.StringSet(key, json, expiry);
}
/// <summary>
/// 獲取一個key的對象
/// </summary>
/// <typeparam name="T">返回類型</typeparam>
public T GetStringKey<T>(string key)
{
RedisValue value = _db.StringGet(key);
if (value.IsNullOrEmpty)
{
return default;
}
return JsonConvert.DeserializeObject<T>(value);
}
#endregion
}
初始化的代碼:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
//初始化RedisHelper類
RedisHelper1.Instance.InitConnect(Configuration);
}
參考文章1
參考文章2
參考文章3
StackExchange.Redis官方文檔
3. 使用CsRedis簡單介紹。
印象中CsRedis是使用連接池的,與StackExchange.Redis不一樣。
雖然是國人基於舊的csredis修改后的,感覺用的人不如StackExchange.Redis多。
4. 使用FreeRedis簡單介紹。
FreeRedis是CsRedis的作者另外做的一個新的連接Redis的庫。
新出,star數少,跟CsRedis用起來很像。
小結
C#連接Redis主要就是這些庫,想查看更多庫,可以上redis官網查看C#的Redis Client。
寫了些測試例子在github上,用於做緩存,簡單使用。
本人例子