| | |
| | | |
| | | namespace EzCoreNet.Redis |
| | | { |
| | | public class EzCoreNetRedisService : IEzCoreNetRedisService,IScoped |
| | | public class EzCoreNetRedisService : IEzCoreNetRedisService, IScoped |
| | | { |
| | | public bool Add(string key, object value, int expireSeconds) |
| | | { |
| | |
| | | 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> |
| | |
| | | { |
| | | return RedisHelper.Keys(Prefix + "*"); |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 后去一个缓存,如果没有,则从数据库中获取, |
| | | /// </summary> |
| | |
| | | |
| | | 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; |
| | | |
| | | } |
| | | } |
| | | } |