NETCORE - Redis的使用
.NetCore 使用StackExchange.Redis 连接Redis
在netcore项目中安装 StackExchange.Redis 的Nuget包。
在appsettings.json中添加connection连接字符串:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DBViewAuth_MySql": "server=localhost;userid=root;pwd=123456;port=3306;database=dbviewauth;sslmode=none;",
"RedisConnectionString": "10.5.30.239:6380,DefaultDatabase=10"
}
}
然后新建一个RedisClient.cs 帮助类,简单封装了一下redis的string操作:
RedisClient.cs 帮助类,如下:
using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace VIEW.BACK { public class RedisClient { private static readonly object Locker = new object(); private ConnectionMultiplexer redisMultiplexer; IDatabase db = null; private static RedisClient _redisClient = null; public static RedisClient redisClient { get { if (_redisClient == null) { lock (Locker) { if (_redisClient == null) { _redisClient = new RedisClient(); } } } return _redisClient; } } public void InitConnect(IConfiguration Configuration) { try { var RedisConnection = Configuration.GetConnectionString("RedisConnectionString"); redisMultiplexer = ConnectionMultiplexer.Connect(RedisConnection); db = redisMultiplexer.GetDatabase(); } catch (Exception ex) { Console.WriteLine(ex.Message); redisMultiplexer = null; db = null; } } public RedisClient() { } #region String /// <summary> /// 保存单个key value /// </summary> /// <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> /// 获取一个key的对象 /// </summary> public T GetStringKey<T>(string key) { if (db == null) { return default; } var value = db.StringGet(key); if (value.IsNullOrEmpty) { return default; } return JsonConvert.DeserializeObject<T>(value); } /// <summary> /// 保存一个对象 /// </summary> /// <param name="obj"></param> public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?)) { if (db == null) { return false; } string json = JsonConvert.SerializeObject(obj); return db.StringSet(key, json, expiry); } #endregion } }
在Startup.cs入口中 初始化RedisClient 的连接:
Startup.cs文件如下:
public void ConfigureServices(IServiceCollection services) {
//初始化redis RedisClient.redisClient.InitConnect(Configuration); services.AddOptions(); }
方法调用:
[Route("tt1")] [HttpGet] public IEnumerable<string> tt1() { int id = 3; var redisResult = RedisClient.redisClient.GetStringKey<int>("key1"); //若redis没有数据,则取数据并设置redis if (redisResult == null || redisResult == default(int)) { Console.WriteLine("redisResult is empty"); RedisClient.redisClient.SetStringKey("key1", id.ToString()); var sdf = id.ToString(); } return new string[] { "value1", "value2" }; }
安装RedisDesktopManager 可查看 redis数据。
引用:https://blog.csdn.net/wcc27857285/article/details/96717258