移动系统liao
2025-02-08 479b6cfc60113f692f6f9146bcd7b9231a32b0e8
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
using Furion;
using SqlSugar;
using SugarRedis;
 
//using SugarRedis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace cylsg.Core
{
    public class SqlSugarRedisCache : ICacheService
    {
 
        //NUGET安装 SugarRedis  (也可以自个实现)   
        //注意:SugarRedis 不要扔到构造函数里面, 一定要单例模式  
        public static SugarRedisClient _service = new SugarRedisClient(App.Configuration["RedisConfig:ConnectionString"] ?? "127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0");
 
 
        //+1重载 new SugarRedisClient(字符串)
        //默认:127.0.0.1:6379,password=,connectTimeout=3000,connectRetry=1,syncTimeout=10000,DefaultDatabase=0
 
        public void Add<V>(string key, V value)
        {
            _service.Set(key, value);
        }
 
        public void Add<V>(string key, V value, int cacheDurationInSeconds)
        {
            _service.SetBySeconds(key, value, cacheDurationInSeconds);
        }
 
        public bool ContainsKey<V>(string key)
        {
            return _service.Exists(key);
        }
 
        public V Get<V>(string key)
        {
            return _service.Get<V>(key);
        }
 
        public IEnumerable<string> GetAllKey<V>()
        {
            //性能注意: 只查sqlsugar用到的key 
            return _service.SearchCacheRegex("SqlSugarDataCache.*");
        }
 
        public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
        {
 
            if (this.ContainsKey<V>(cacheKey))
            {
                var result = this.Get<V>(cacheKey);
                if (result == null)
                {
                    return create();
                }
                else
                {
                    return result;
                }
            }
            else
            {
                var result = create();
                this.Add(cacheKey, result, cacheDurationInSeconds);
                return result;
            }
        }
 
        public void Remove<V>(string key)
        {
            _service.Remove(key);
        }
    }
}