@@ -24,6 +24,7 @@ public class ClassicUserController : ControllerBase
2424 private readonly IConfiguration _configuration ;
2525
2626 // 缓存配置字段
27+ private readonly bool _isCacheEnabled ;
2728 private readonly bool _useRedis ;
2829 private readonly TimeSpan _absoluteExpiration ;
2930 private readonly TimeSpan _slidingExpiration ;
@@ -43,6 +44,7 @@ public ClassicUserController(
4344 _configuration = configuration ;
4445
4546 // 读取缓存配置
47+ _isCacheEnabled = configuration . GetValue < bool > ( "CacheSettings:Enabled" , true ) ;
4648 _useRedis = configuration . GetValue < bool > ( "CacheSettings:UseRedis" ) ;
4749 var absMinutes = configuration . GetValue < int > ( "CacheSettings:ExpirationMinutes" , 5 ) ;
4850 var slideMinutes = configuration . GetValue < int > ( "CacheSettings:SlidingExpirationMinutes" , 2 ) ;
@@ -70,32 +72,34 @@ public async Task<IActionResult> GetProfile(string username)
7072 ClassicProfile ? cachedProfile = null ;
7173
7274 // 读取缓存
73- try
75+ if ( _isCacheEnabled )
7476 {
75- if ( _useRedis && _distributedCache != null )
77+ try
7678 {
77- // Redis: 读取字符串并反序列化
78- var jsonStr = await _distributedCache . GetStringAsync ( cacheKey ) ;
79- if ( ! string . IsNullOrEmpty ( jsonStr ) )
79+ if ( _useRedis && _distributedCache != null )
8080 {
81- cachedProfile = JsonSerializer . Deserialize < ClassicProfile > ( jsonStr ) ;
81+ // Redis: 读取字符串并反序列化
82+ var jsonStr = await _distributedCache . GetStringAsync ( cacheKey ) ;
83+ if ( ! string . IsNullOrEmpty ( jsonStr ) )
84+ {
85+ cachedProfile = JsonSerializer . Deserialize < ClassicProfile > ( jsonStr ) ;
86+ }
87+ }
88+ else
89+ {
90+ // Memory: 直接读取对象引用
91+ _memoryCache . TryGetValue ( cacheKey , out cachedProfile ) ;
92+ }
93+ if ( cachedProfile != null )
94+ {
95+ return Ok ( cachedProfile ) ;
8296 }
8397 }
84- else
85- {
86- // Memory: 直接读取对象引用
87- _memoryCache . TryGetValue ( cacheKey , out cachedProfile ) ;
88- }
89- if ( cachedProfile != null )
98+ catch ( Exception ex )
9099 {
91- return Ok ( cachedProfile ) ;
100+ _logger . LogWarning ( ex , "Cache read failed for {Key}" , cacheKey ) ;
92101 }
93102 }
94- catch ( Exception ex )
95- {
96- _logger . LogWarning ( ex , "Cache read failed for {Key}" , cacheKey ) ;
97- }
98-
99103
100104 try
101105 {
@@ -118,30 +122,33 @@ public async Task<IActionResult> GetProfile(string username)
118122 var classicProfile = ClassicMapper . ToClassicProfile ( profile ) ;
119123
120124 // 写入缓存
121- try
125+ if ( _isCacheEnabled )
122126 {
123- if ( _useRedis && _distributedCache != null )
127+ try
124128 {
125- // Redis: 序列化为 JSON 存储
126- var jsonStr = JsonSerializer . Serialize ( classicProfile ) ;
127- await _distributedCache . SetStringAsync ( cacheKey , jsonStr , new DistributedCacheEntryOptions
129+ if ( _useRedis && _distributedCache != null )
128130 {
129- AbsoluteExpirationRelativeToNow = _absoluteExpiration
130- } ) ;
131+ // Redis: 序列化为 JSON 存储
132+ var jsonStr = JsonSerializer . Serialize ( classicProfile ) ;
133+ await _distributedCache . SetStringAsync ( cacheKey , jsonStr , new DistributedCacheEntryOptions
134+ {
135+ AbsoluteExpirationRelativeToNow = _absoluteExpiration
136+ } ) ;
137+ }
138+ else
139+ {
140+ // Memory: 存储对象引用 (带 Size 限制)
141+ _memoryCache . Set ( cacheKey , classicProfile , new MemoryCacheEntryOptions ( )
142+ . SetAbsoluteExpiration ( _absoluteExpiration )
143+ . SetSlidingExpiration ( _slidingExpiration )
144+ . SetSize ( 1 ) ) ;
145+ }
131146 }
132- else
147+ catch ( Exception ex )
133148 {
134- // Memory: 存储对象引用 (带 Size 限制)
135- _memoryCache . Set ( cacheKey , classicProfile , new MemoryCacheEntryOptions ( )
136- . SetAbsoluteExpiration ( _absoluteExpiration )
137- . SetSlidingExpiration ( _slidingExpiration )
138- . SetSize ( 1 ) ) ;
149+ _logger . LogError ( ex , "Cache write failed for {Key}" , cacheKey ) ;
139150 }
140151 }
141- catch ( Exception ex )
142- {
143- _logger . LogError ( ex , "Cache write failed for {Key}" , cacheKey ) ;
144- }
145152
146153 return Ok ( classicProfile ) ;
147154 }
@@ -260,22 +267,25 @@ await _context.GalleryItems
260267 await transaction . CommitAsync ( ) ;
261268
262269 // 清除缓存
263- string cacheKey = GetProfileCacheKey ( username ) ;
264- try
270+ if ( _isCacheEnabled )
265271 {
266- if ( _useRedis && _distributedCache != null )
272+ string cacheKey = GetProfileCacheKey ( username ) ;
273+ try
267274 {
268- await _distributedCache . RemoveAsync ( cacheKey ) ;
275+ if ( _useRedis && _distributedCache != null )
276+ {
277+ await _distributedCache . RemoveAsync ( cacheKey ) ;
278+ }
279+ else
280+ {
281+ _memoryCache . Remove ( cacheKey ) ;
282+ }
269283 }
270- else
284+ catch ( Exception ex )
271285 {
272- _memoryCache . Remove ( cacheKey ) ;
286+ _logger . LogError ( ex , "Cache removal failed for {Key}" , cacheKey ) ;
273287 }
274288 }
275- catch ( Exception ex )
276- {
277- _logger . LogError ( ex , "Cache removal failed for {Key}" , cacheKey ) ;
278- }
279289
280290 _logger . LogInformation ( "Profile updated for user: {Username}" , username ) ;
281291
0 commit comments