Skip to content

Commit 593505a

Browse files
committed
✨增加SqlSugar缓存开关
1 parent d0b029f commit 593505a

File tree

2 files changed

+94
-51
lines changed

2 files changed

+94
-51
lines changed

Blog.Core.Api/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@
7878
"SeedDBDataEnabled": true, //生成表,并初始化数据
7979
"Author": "Blog.Core",
8080
"SvcName": "", // /svc/blog
81-
"UseLoadTest": false
81+
"UseLoadTest": false,
82+
"CacheDbEnabled": false
8283
},
8384

8485
//优化DB配置、不会再区分单库多库
Lines changed: 92 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using Blog.Core.Common.Caches.Interface;
4+
using Blog.Core.Common.Option;
45
using SqlSugar;
56

67
namespace Blog.Core.Common.Caches;
@@ -15,54 +16,95 @@ namespace Blog.Core.Common.Caches;
1516
/// </summary>
1617
public class SqlSugarCacheService : ICacheService
1718
{
18-
private readonly Lazy<ICaching> _caching = new(() => App.GetService<ICaching>(false));
19-
private ICaching Caching => _caching.Value;
20-
21-
public void Add<V>(string key, V value)
22-
{
23-
Caching.Set(key, value);
24-
}
25-
26-
public void Add<V>(string key, V value, int cacheDurationInSeconds)
27-
{
28-
Caching.Set(key, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
29-
}
30-
31-
public bool ContainsKey<V>(string key)
32-
{
33-
return Caching.Exists(key);
34-
}
35-
36-
public V Get<V>(string key)
37-
{
38-
return Caching.Get<V>(key);
39-
}
40-
41-
public IEnumerable<string> GetAllKey<V>()
42-
{
43-
return Caching.GetAllCacheKeys();
44-
}
45-
46-
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
47-
{
48-
if (!ContainsKey<V>(cacheKey))
49-
{
50-
var value = create();
51-
Caching.Set(cacheKey, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
52-
return value;
53-
}
54-
55-
return Caching.Get<V>(cacheKey);
56-
}
57-
58-
public void Remove<V>(string key)
59-
{
60-
Caching.Remove(key);
61-
}
62-
63-
public bool RemoveAll()
64-
{
65-
Caching.RemoveAll();
66-
return true;
67-
}
19+
private readonly Lazy<ICaching> _caching = new(() => App.GetService<ICaching>(false));
20+
private ICaching Caching => _caching.Value;
21+
private readonly AppSettingsOption _options = App.GetOptions<AppSettingsOption>();
22+
23+
public void Add<V>(string key, V value)
24+
{
25+
if (!_options.CacheDbEnabled)
26+
{
27+
return;
28+
}
29+
30+
Caching.Set(key, value);
31+
}
32+
33+
public void Add<V>(string key, V value, int cacheDurationInSeconds)
34+
{
35+
if (!_options.CacheDbEnabled)
36+
{
37+
return;
38+
}
39+
40+
Caching.Set(key, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
41+
}
42+
43+
public bool ContainsKey<V>(string key)
44+
{
45+
if (!_options.CacheDbEnabled)
46+
{
47+
return default;
48+
}
49+
50+
return Caching.Exists(key);
51+
}
52+
53+
public V Get<V>(string key)
54+
{
55+
if (!_options.CacheDbEnabled)
56+
{
57+
return default;
58+
}
59+
60+
return Caching.Get<V>(key);
61+
}
62+
63+
public IEnumerable<string> GetAllKey<V>()
64+
{
65+
if (!_options.CacheDbEnabled)
66+
{
67+
return default;
68+
}
69+
70+
return Caching.GetAllCacheKeys();
71+
}
72+
73+
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
74+
{
75+
if (!_options.CacheDbEnabled)
76+
{
77+
return create();
78+
}
79+
80+
if (!ContainsKey<V>(cacheKey))
81+
{
82+
var value = create();
83+
Caching.Set(cacheKey, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
84+
return value;
85+
}
86+
87+
return Caching.Get<V>(cacheKey);
88+
}
89+
90+
public void Remove<V>(string key)
91+
{
92+
if (!_options.CacheDbEnabled)
93+
{
94+
return;
95+
}
96+
97+
Caching.Remove(key);
98+
}
99+
100+
public bool RemoveAll()
101+
{
102+
if (!_options.CacheDbEnabled)
103+
{
104+
return true;
105+
}
106+
107+
Caching.RemoveAll();
108+
return true;
109+
}
68110
}

0 commit comments

Comments
 (0)