移动系统liao
2024-09-24 72e71a7a31b14f30f38a2a3acecb7311049c608a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using Furion;
using Furion.DependencyInjection;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace EzCoreNet.Redis
{
    public class EzCoreNetRedisService : IEzCoreNetRedisService, IScoped
    {
        public bool Add(string key, object value, int expireSeconds)
        {
            return RedisHelper.Set(key, value, expireSeconds);
        }
 
 
        public bool DelKey(string key)
        {
            RedisHelper.Del(key);
            return true;
        }
 
        public T? Get<T>(string key)
        {
            return RedisHelper.Get<T>(key);
        }
 
        public long GetTtl(string key)
        {
 
            return RedisHelper.Ttl(key);
        }
 
        public bool SetTtl(string key, int ttl)
        {
            return RedisHelper.Expire(key, ttl);
        }
 
        /// <summary>
        /// 设置一个键值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Add<T>(string key, T value, int expireSeconds = -1)
        {
            return RedisHelper.Set(key, value, expireSeconds);
        }
 
        public async Task<long> delegateAllKeyWith(string Prefix)
        {
            // 使用Scan模式匹配所有符合前缀的key
            var keys = GetAllKey(Prefix);
 
            // 批量删除找到的keys
            if (keys != null && keys.Count() > 0)
            {
                return await RedisHelper.DelAsync(keys.ToArray());
            }
 
            return 0; // 如果没有找到匹配的key,返回0
        }
 
        public IEnumerable<string> GetAllKey(string Prefix)
        {
            return RedisHelper.Keys(Prefix + "*");
        }
 
        /// <summary>
        /// 后去一个缓存,如果没有,则从数据库中获取,
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="DataFormSql">读取方法</param>
        /// <param name="dc"> 读取的数据库服务</param>
        /// <param name="expireSeconds"></param>
        /// <returns></returns>
 
        public T? GetAndRefCacheKey<T>(string key, Func<ISqlSugarClient, T> DataFormSql, ISqlSugarClient? dc = null, int? expireSeconds = 3600)
        {
            var Data = RedisHelper.Get<T>(key);
            if (Data == null && DataFormSql != null)
            {
                if (dc == null)
                    dc = App.GetService<ISqlSugarClient>();
                Data = DataFormSql.Invoke(dc);
 
                //   Data =  DataFormSql.BeginInvoke(dc,ar=> DataFormSql.EndInvoke(ar),null);
                if (expireSeconds == null)
                    expireSeconds = -1;
                if (Data != null)
                    RedisHelper.Set(key, Data, (int)expireSeconds);
 
            }
            return Data;
        }
 
        public long Incrby(string key)
        {
            return RedisHelper.IncrBy(key);
        }
 
        public string Get32sn()
        {
            string formattedDate = DateTime.Now.ToString("yyyyMMdd");
          
 
            var sn = Incrby($"CreatSnKey:{formattedDate}");
            if (sn < 1)
            {
                //设置有效期限为24小时
                SetTtl($"CreatSnKey:{formattedDate}", 24 * 60 * 60);
            }
            string re = "EZCoreCanYinLingShiGon" + formattedDate + sn.ToString();
 
            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;
         
        }
    }
}