11using System ;
22using System . Collections . Generic ;
33using Blog . Core . Common . Caches . Interface ;
4+ using Blog . Core . Common . Option ;
45using SqlSugar ;
56
67namespace Blog . Core . Common . Caches ;
@@ -15,54 +16,95 @@ namespace Blog.Core.Common.Caches;
1516/// </summary>
1617public 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