修改:Redis方法修改。

This commit is contained in:
2025-12-22 17:47:19 +08:00
parent 67f9192182
commit aa0d7043bf
11 changed files with 237 additions and 85 deletions

View File

@@ -8,7 +8,7 @@
<add key="RedisKey" value="authority" />
<add key="RedisConn" value="127.0.0.1,defaultDatabase=13,password=123456,connectTimeout=10000,connectRetry=1,syncTimeout=10000,allowAdmin = true" />
<add key="WXRedisConn" value="47.119.147.104:26379,defaultDatabase=0,password=1001^_^lool,connectTimeout=10000,connectRetry=1,syncTimeout=10000,allowAdmin = true" />
<!--<add key="RedisConn" value="127.0.0.1:7555,defaultDatabase=13,password=cw_oy_lsh,connectTimeout=10000,connectRetry=1,syncTimeout=10000,allowAdmin = true" />-->
<add key="DBName" value="authority" />

View File

@@ -1,10 +1,10 @@
using CSRedis;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using static CSRedis.CSRedisClient;
namespace Common
{
@@ -14,20 +14,19 @@ namespace Common
public class CSRedisCacheHelper
{
public static CSRedisClient redis;
private const string ip = "localhost";
private const string port = "26379";
//private static readonly string[] redisHosts = null;
private static int SessionExpireMinutes = 1;
private static int MonitorLogExpireMinutes = 1;
//过期时间60个小时
private static int DeviceStatusExpireMinutes = 60;
public static string HeartBeatPrefix = "HeartBeatMonitor";
static CSRedisCacheHelper()
{
var redisHostStr = string.Format("{0}:{1}", ip, port);
string redisHostStr = "47.119.147.104:26379";
if (!string.IsNullOrEmpty(redisHostStr))
{
redis = new CSRedisClient(redisHostStr + ",password=1001^_^lool,defaultDatabase=0");
var DingYueMsg = ("CellCorelDRAWUser", new Action<SubscribeMessageEventArgs>(async (args) =>
{
string body = args.Body;
}));
CSRedisCacheHelper.redis.Subscribe(DingYueMsg);
}
}
/// <summary>
@@ -36,30 +35,103 @@ namespace Common
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Set<T>(string key, T value, int ExpireTime)
public static void Set<T>(string key, T value)
{
redis?.Set(key, value, ExpireTime * 60);
redis.Set(key, value, SessionExpireMinutes * 60);
}
public static T Get<T>(string key)
{
return redis.Get<T>(key);
}
public static void Forever<T>(string key, T value)
public static void Del(string[] key)
{
redis.Set(key, value, -1);
redis.Del(key.ToArray());
}
public static void Del(string key)
public static void Del_Partition(string key)
{
redis.Del(key);
}
public static T ForeverGet<T>(string key)
{
return redis.Get<T>(key);
}
public static void Forever<T>(string key, T value)
{
redis.Set(key, value, MonitorLogExpireMinutes * 24 * 60 * 60);
}
public static T Get_Partition<T>(string key)
{
return redis.Get<T>(key);
}
public static void Set_Partition<T>(string key, T value)
{
redis.Set(key, value, DeviceStatusExpireMinutes * 60 * 60);
}
/// <summary>
/// 设置过期时间
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="ExpireTime_Minutes"></param>
public static void Set_PartitionWithTime<T>(string key, T value, int ExpireTime_Minutes)
{
redis.Set(key, value, ExpireTime_Minutes * 60);
}
public static void Set_PartitionWithForever<T>(string key, T value)
{
redis.Set(key, value, -1);
}
public static void Del_Partition(string[] key)
{
redis.Del(key);
}
public static bool Contains_Partition(string key)
{
return redis.Exists(key);
}
public static long GetForever_ExpireMinutes<T>(string key)
{
return redis.Ttl(key);
}
/// <summary>
/// 获取
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(string key, string mac)
{
T obj = redis.Get<T>(mac);
if (obj == null)
{
return redis.Get<T>(key);
}
return obj;
}
/// <summary>
/// 判断是否存在
/// </summary>
/// <param name="key"></param>
/// <param name="mac"></param>
/// <returns></returns>
public static bool Contains(string key, string mac)
{
bool result = redis.Exists(mac);
if (!result)
{
result = redis.Exists(key);
}
return result;
}
public static bool Contains(string key)
{
@@ -67,28 +139,10 @@ namespace Common
return result;
}
/// <summary>
/// 发布消息
/// </summary>
/// <param name="Topic"></param>
/// <param name="Payload"></param>
public static void Publish(string Topic, string Payload)
{
CSRedisCacheHelper.redis.PublishNoneMessageId(Topic, Payload);
CSRedisCacheHelper.redis.Publish(Topic, Payload);
}
public static void LPush(string Key, Dictionary<string, string> Value)
{
CSRedisCacheHelper.redis.LPush<Dictionary<string,string>>(Key,Value);
}
public static T BRPop<T>(string Key)
{
return CSRedisCacheHelper.redis.BRPop<T>(3,Key);
}
/// <summary>
/// 添加Hash缓存
/// </summary>
@@ -115,16 +169,73 @@ namespace Common
{
return redis.HMGet<T>(key, KeyV);
}
/// <summary>
/// Example
/// WXFault_酒店编号_房间号
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static Dictionary<string, string> HMGetAll(string key)
{
return redis.HGetAll(key);
}
/// <summary>
/// 获取 某个hash值的数量
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static int GetHMCount(string key)
{
return redis.HGetAll(key).Count;
}
/// <summary>
/// 删除Hash缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static long HDelAll<T>(string key)
{
return redis.HDel(key);
}
public static long HDel(string key, string key1)
{
return redis.HDel(key, key1);
}
public static void ListAdd(string key, params object[] obj)
{
redis.LPush(key, obj);
}
public static int MaxLen = 500000;
public static void StreamAdd(string key, string Value)
{
try
{
// 添加简单消息
redis.XAdd(key, MaxLen, "*", new ValueTuple<string, string>("__data", Value));
}
catch (Exception)
{
}
}
public static void StreamConsume(string key, string group = "UDPData", string consumer = "Crics1")
{
var data = redis.XReadGroup(group, consumer, 100, 10, new ValueTuple<string, string>(key, ">"));
if (data != null)
{
//检查pending表的长度
//消费确认前pending 应该等于2
var pending = redis.XPending(key, group);
foreach (var item in data)
{
var idarray = item.Item2;
foreach (var SerializeNo in idarray)
{
var id1 = SerializeNo.Item1;
redis.XAck(key, group, id1);
redis.XDel(key, id1);
}
}
}
}
}
}

View File

@@ -104,8 +104,8 @@ public class HomeController : BaseController
if (!string.IsNullOrEmpty(Token))
{
string Ip = IPHelper.GetIP();
var userinfo = RedisHelper.StringGet<UserInfo>(Token + Ip);
RedisHelper.RemoveKey(Token + Ip);
var userinfo = StackChangeRedisHelper.StringGet<UserInfo>(Token + Ip);
StackChangeRedisHelper.RemoveKey(Token + Ip);
}
base.ViewBag.Select = -1;
base.ViewBag.LogData = SqlSugarBase.Db.Ado.SqlQuery<DbLog>("SELECT * FROM DbLog ORDER BY ID DESC limit 1, 50").ToList();

View File

@@ -30,8 +30,8 @@ public class LoginController : Controller
if (!string.IsNullOrEmpty(Token))
{
string Ip = IPHelper.GetIP();
var userinfo = RedisHelper.StringGet<UserInfo>(Token);
RedisHelper.RemoveKey(Token);
var userinfo = StackChangeRedisHelper.StringGet<UserInfo>(Token);
StackChangeRedisHelper.RemoveKey(Token);
if (userinfo != default(UserInfo)) {
ReturnResult returnResult = new ReturnResult();
DL(ref returnResult, userinfo.Uid, userinfo.Pwd,1);

View File

@@ -74,7 +74,7 @@ namespace UI.Controllers
token = (BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(token)), 4, 8)).Replace("-", "");
if (moreLogin == true)
{
RedisHelper.StringSet(token, new UserInfo() { Pwd = Pwd, Uid = Uid }, TimeSpan.FromMinutes(20));
StackChangeRedisHelper.StringSet(token, new UserInfo() { Pwd = Pwd, Uid = Uid }, TimeSpan.FromMinutes(20));
}
resdata.Data = new
{
@@ -161,7 +161,7 @@ namespace UI.Controllers
var resdata = new ReturnResult<dynamic>();
try
{
var userinfo = RedisHelper.StringGet<UserInfo>(Token);
var userinfo = StackChangeRedisHelper.StringGet<UserInfo>(Token);
if (IsNew)
{
Ip = Ip == "" ? IPHelper.GetIP() : Ip;
@@ -174,11 +174,11 @@ namespace UI.Controllers
var token = (userinfo.Uid + userinfo.Pwd + DateTime.Now);
var md5 = new MD5CryptoServiceProvider();
token = (BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(token)), 4, 8)).Replace("-", "");
RedisHelper.StringSet(token, userinfo);
StackChangeRedisHelper.StringSet(token, userinfo);
resdata.Data = new { Token = token };
resdata.Status = 200;
}
RedisHelper.StringSet(Token, userinfo, TimeSpan.FromSeconds(10));
StackChangeRedisHelper.StringSet(Token, userinfo, TimeSpan.FromSeconds(10));
}
catch (Exception ex)
{
@@ -203,8 +203,8 @@ namespace UI.Controllers
{
Logs.WriteTimingUDPLog($"传入token {Token}");
Ip = Ip == "" ? IPHelper.GetIP() : Ip;
var userinfo = RedisHelper.StringGet<UserInfo>(Token);
RedisHelper.RemoveKey(Token);
var userinfo = StackChangeRedisHelper.StringGet<UserInfo>(Token);
StackChangeRedisHelper.RemoveKey(Token);
if (userinfo == default(UserInfo))
{
if (IsData == false)
@@ -276,7 +276,7 @@ namespace UI.Controllers
try
{
DbLog log = new DbLog() { Ip = Ip == "" ? IPHelper.GetIP() : Ip, Client = UserAgent == "" ? HttpContext.Request.UserAgent : UserAgent };
var userinfo = RedisHelper.StringGet<UserInfo>(Token);
var userinfo = StackChangeRedisHelper.StringGet<UserInfo>(Token);
string Uid = userinfo.Uid;
if (userinfo == null)
{

View File

@@ -13,6 +13,8 @@ using static Services.Manager.SyncAllFromOutterDB;
using Newtonsoft.Json;
using Models.ApiModei;
using NLog;
using Services.Tool;
using Common;
namespace UI.Controllers
{
@@ -448,8 +450,57 @@ namespace UI.Controllers
}
}
}
/// <summary>
/// 获取房间地址状态
/// </summary>
/// <param name="roomAddressStatus"></param>
/// <returns></returns>
[HttpPost]
public ActionResult GetRoomAddressStatus(int Code, string RoomNum)
{
RoomAddressStatus roomAddressStatus = new RoomAddressStatus
{
Code = Code,
RoomNum = RoomNum
};
try
{
if (roomAddressStatus == null)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { Status = 0, Message = "请求参数不能为空" }, JsonRequestBehavior.DenyGet);
}
string code = roomAddressStatus.Code.ToString();
string RoomNumVal = roomAddressStatus.RoomNum ?? string.Empty;
string key = $"WXFault_{code}_{RoomNumVal}";
var result = CSRedisCacheHelper.HMGetAll(key);
if (result != null && result.Count > 0)
{
return Json(new { Status = 1, Message = "获取成功", Data = result }, JsonRequestBehavior.DenyGet);
}
else
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return Json(new { Status = 0, Message = "未找到数据", Data = result }, JsonRequestBehavior.DenyGet);
}
}
catch (Exception ex)
{
logger.Error(ex, "GetRoomAddressStatus error");
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return Json(new { Status = -1, Message = "服务器错误", Detail = ex.Message }, JsonRequestBehavior.DenyGet);
}
}
}
public class RoomAddressStatus
{
public string RoomNum { get; set; }
public int Code { get; set; }
}
// 服务控制请求参数类
public class RCUServiceRequest
{

View File

@@ -1,5 +1,4 @@
using Common;
using Models;
using Models;
using Models.ModelItems;
using Models.View;
using Newtonsoft.Json;
@@ -1590,17 +1589,6 @@ namespace UI.Controllers
return ri;
}
public void A()
{
string code = "";
string roonumber = "";
string a = $"WXFault_{code}_{roonumber}";
CSRedisCacheHelper.HMGetAll("'");
}
}

View File

@@ -213,6 +213,7 @@
<Compile Include="Controllers\OtherApiController.cs" />
<Compile Include="Controllers\UpgradeController.cs" />
<Compile Include="Controllers\WxController.cs" />
<Compile Include="CSRedisCacheHelper.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>