qwj
2023-08-09 ba230615ede7ae34b90ff1c22399daa28f184b50
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
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace DocumentServiceAPI.Core
{
    public class SqlSugarRedisCache : ICacheService
    {
 
        public SqlSugarRedisCache()
        {
        }
 
        public void Add<TV>(string key, TV value)
        {
            RedisHelper.Set(key, value);
        }
 
        public void Add<TV>(string key, TV value, int cacheDurationInSeconds)
        {
            RedisHelper.Set(key, value, cacheDurationInSeconds);
        }
 
        public bool ContainsKey<TV>(string key)
        {
            return RedisHelper.Exists(key);
        }
 
        public TV Get<TV>(string key)
        {
            return RedisHelper.Get<TV>(key);
        }
 
        public IEnumerable<string> GetAllKey<TV>()
        {
            return RedisHelper.Keys("SqlSugarDataCache.*");
        }
 
        public TV GetOrCreate<TV>(string cacheKey, Func<TV> create, int cacheDurationInSeconds = int.MaxValue)
        {
            if (this.ContainsKey<TV>(cacheKey))
            {
                return this.Get<TV>(cacheKey);
            }
            else
            {
                var result = create();
                this.Add(cacheKey, result, cacheDurationInSeconds);
                return result;
            }
        }
 
        public void Remove<TV>(string key)
        {
            RedisHelper.DelAsync(key);
        }
    }
}