刘磊
2025-04-19 823752496e2a4cdb6a1fb36227cd15b8b7135336
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
using Microsoft.Extensions.Caching.Memory;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEA_Core.Caches
{
    /// <summary>
    /// 实现SqlSugar的ICacheService接口
    /// </summary>
    public class SqlSugarCacheService : ICacheService
    {
        private readonly Lazy<ICaching> _caching = new(() => App.GetService<ICaching>(false));
        private ICaching Caching => _caching.Value;
 
        public void Add<V>(string key, V value)
        {
            Caching.Set(key, value);
        }
 
        public void Add<V>(string key, V value, int cacheDurationInSeconds)
        {
            Caching.Set(key, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
        }
 
        public bool ContainsKey<V>(string key)
        {
            return Caching.Exists(key);
        }
 
        public V Get<V>(string key)
        {
            return Caching.Get<V>(key);
        }
 
        public IEnumerable<string> GetAllKey<V>()
        {
            return Caching.GetAllCacheKeys();
        }
 
        public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
        {
            if (!ContainsKey<V>(cacheKey))
            {
                var value = create();
                Caching.Set(cacheKey, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
                return value;
            }
 
            return Caching.Get<V>(cacheKey);
        }
 
        public void Remove<V>(string key)
        {
            Caching.Remove(key);
        }
 
        public bool RemoveAll()
        {
            Caching.RemoveAll();
            return true;
        }
    }
}