123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Extensions.Configuration;
- using Newtonsoft.Json;
- using StackExchange.Redis;
- namespace IoTIntegrationPlatform.Common
- {
- /// <summary>
- /// redis 帮助类
- /// </summary>
- public class RedisHelper
- {
- private ConnectionMultiplexer redis;
- private static string redisConnectStr = string.Empty;
- /// <summary>
- ///
- /// </summary>
- /// <param name="conn"></param>
- public static void SetRedisConnection(string conn)
- {
- redisConnectStr = conn;
- }
- private static RedisHelper _Instance;
- /// <summary>
- /// redis单例
- /// </summary>
- public static RedisHelper Instance
- {
- get
- {
- lock (redisConnectStr)
- {
- if (_Instance == null)
- {
- if (string.IsNullOrWhiteSpace(redisConnectStr))
- {
- Logging.Warn("redisConnectStr 未配置");
- }
- _Instance = new RedisHelper
- {
- redis = ConnectionMultiplexer.Connect(redisConnectStr)
- };
- }
- return _Instance;
- }
- }
- }
- public bool Remove(string key)
- {
- IDatabase db = redis.GetDatabase();
- return db.KeyDelete(key);
- }
- #region string
- /// <summary>
- /// 保存字符串
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <param name="TimeSpanSeconds"></param>
- /// <returns></returns>
- public bool StringSet(string key, string value, int TimeSpanSeconds = 0)
- {
- IDatabase db = redis.GetDatabase();
- if (TimeSpanSeconds > 0)
- {
- return db.StringSet(key, value, TimeSpan.FromSeconds(TimeSpanSeconds));
- }
- return db.StringSet(key, value);
- }
- public bool StringSet(string key, string value, TimeSpan? timeSpan)
- {
- IDatabase db = redis.GetDatabase();
- if (timeSpan != null)
- {
- return db.StringSet(key, value, timeSpan);
- }
- return db.StringSet(key, value);
- }
- /// <summary>
- /// 保存实体字符串
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public bool StringSet<T>(string key, T value, int TimeSpanSeconds = 0)
- {
- IDatabase db = redis.GetDatabase();
- if (TimeSpanSeconds > 0)
- {
- return db.StringSet(key, JsonConvert.SerializeObject(value), TimeSpan.FromSeconds(TimeSpanSeconds));
- }
- return db.StringSet(key, JsonConvert.SerializeObject(value));
- }
- /// <summary>
- /// 获取字符串
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string StringGet(string key)
- {
- IDatabase db = redis.GetDatabase();
- return db.StringGet(key);
- }
- /// <summary>
- /// 获取实体
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="key"></param>
- /// <returns></returns>
- public T StringGet<T>(string key, int TimeSpanSeconds = 0)
- {
- string result = StringGet(key);
- if (!string.IsNullOrWhiteSpace(result))
- {
- if (TimeSpanSeconds > 0)
- {
- StringSet(key, result, TimeSpanSeconds);
- }
- return JsonConvert.DeserializeObject<T>(result);
- }
- return default;
- }
- #endregion string
- #region List
- public long ListRightPush(string key, string value)
- {
- IDatabase db = redis.GetDatabase();
- return db.ListRightPush(key, value);
- }
- public string ListLeftPop(string key)
- {
- IDatabase db = redis.GetDatabase();
- return db.ListLeftPop(key);
- }
- public static List<T> GetList<T>(string key)
- {
- List<T> taskList = new List<T>();
- //获取redis保存数据
- var redisList = RedisHelper.Instance.StringGet(key);
- if (!string.IsNullOrEmpty(redisList))
- {
- taskList = JsonTools.JsonToT<List<T>>(redisList);
- }
- return taskList;
- }
- #endregion List
-
- }
- }
|