From 479b6cfc60113f692f6f9146bcd7b9231a32b0e8 Mon Sep 17 00:00:00 2001 From: 移动系统liao <liaoxujun@qq.com> Date: 星期六, 08 二月 2025 09:07:57 +0800 Subject: [PATCH] Merge branch 'cyronghe' --- cylsg/cylsg.redis/EzCoreNetRedisService.cs | 70 +++++++++++++++++++++++++++++++---- 1 files changed, 62 insertions(+), 8 deletions(-) diff --git a/cylsg/cylsg.redis/EzCoreNetRedisService.cs b/cylsg/cylsg.redis/EzCoreNetRedisService.cs index 5137422..f81b0a7 100644 --- a/cylsg/cylsg.redis/EzCoreNetRedisService.cs +++ b/cylsg/cylsg.redis/EzCoreNetRedisService.cs @@ -10,7 +10,7 @@ namespace EzCoreNet.Redis { - public class EzCoreNetRedisService : IEzCoreNetRedisService,IScoped + public class EzCoreNetRedisService : IEzCoreNetRedisService, IScoped { public bool Add(string key, object value, int expireSeconds) { @@ -35,9 +35,9 @@ return RedisHelper.Ttl(key); } - public bool SetTtl(string key, int ttl) + public bool SetTtl(string key, int ttl) { - return RedisHelper.Expire(key, ttl); + return RedisHelper.Expire(key, ttl); } /// <summary> @@ -70,7 +70,7 @@ { return RedisHelper.Keys(Prefix + "*"); } - + /// <summary> /// 鍚庡幓涓�涓紦瀛橈紝濡傛灉娌℃湁锛屽垯浠庢暟鎹簱涓幏鍙栵紝 /// </summary> @@ -102,23 +102,77 @@ public long Incrby(string key) { - return RedisHelper.IncrBy(key); + return RedisHelper.IncrBy(key); } public string Get32sn() { string formattedDate = DateTime.Now.ToString("yyyyMMdd"); - + var sn = Incrby($"CreatSnKey:{formattedDate}"); - if(sn<1) + if (sn < 1) { //璁剧疆鏈夋晥鏈熼檺涓�24灏忔椂 SetTtl($"CreatSnKey:{formattedDate}", 24 * 60 * 60); } - string re = "Ez" + formattedDate + sn.ToString(); + string re = "EZCoreCanYinLingShiGon" + formattedDate + sn.ToString(); return re; } + + + public string GetYYYMMddAnd4sn() + { + string formattedDate = DateTime.Now.ToString("yyyyMMdd"); + + + var sn = Incrby($"CreatYYYMMddAnd4snKey:{formattedDate}"); + if (sn < 1) + { + //璁剧疆鏈夋晥鏈熼檺涓�24灏忔椂 + SetTtl($"CreatYYYMMddAnd4snKey:{formattedDate}", 24 * 60 * 60); + } + string re = formattedDate + sn.ToString("D4"); + + return re; + } + /// <summary> + /// + /// </summary> + /// <param name="LockKey"></param> + /// <param name="expireSeconds"></param> + /// <param name="value"></param> + /// <returns></returns> + + public async Task<bool> TryLock(string LockKey, int expireSeconds = 600, string value = "Lock") + { + if (await RedisHelper.SetNxAsync(LockKey, value) == true) + { + if( await RedisHelper.ExpireAsync(LockKey, expireSeconds)==false) + return false; + return true; + } + else + return false; + + + } + + + /// <summary> + /// + /// </summary> + /// <param name="LockKey"></param> + /// <returns></returns> + public async Task<bool > TryUnLock(string LockKey) + { + var b= await RedisHelper.DelAsync(LockKey); + if(b>0) + return true; + else + return false; + + } } } -- Gitblit v1.9.1