@@ -11,44 +11,30 @@ public CacheService(IDistributedCache cache, IOptionsMonitor<RedisOptions> redis
1111 this . redisOptionsMonitor = redisOptionsMonitor ;
1212 }
1313
14- /// <summary>
15- /// Get the values from the cache
16- /// </summary>
17- /// <typeparam name="T"></typeparam>
18- /// <param name="key"></param>
19- /// <returns>Return the values from the cache</returns>
20- public T GetCache < T > ( string key )
14+ public async Task < T > GetCacheAsync < T > ( string key )
2115 {
22- var value = cache . GetString ( key ) ;
16+ var jsonData = await cache . GetStringAsync ( key ) ;
2317
24- if ( value != null )
18+ if ( jsonData is null )
2519 {
26- return JsonConvert . DeserializeObject < T > ( value ) ;
20+ return default ;
2721 }
2822
29- return default ;
23+ return System . Text . Json . JsonSerializer . Deserialize < T > ( jsonData ) ;
3024 }
3125
32- /// <summary>
33- /// Set the values in the cache
34- /// </summary>
35- /// <typeparam name="T"></typeparam>
36- /// <param name="key"></param>
37- /// <param name="value"></param>
38- /// <returns>Return the values after set the values in the cache</returns>
39- public T SetCache < T > ( string key , T value )
26+ public async Task < T > SetCacheAsync < T > ( string key , T value )
4027 {
41- var options = redisOptionsMonitor . CurrentValue ;
42- var redisOptions = new DistributedCacheEntryOptions
28+ var optionsCache = redisOptionsMonitor . CurrentValue ;
29+ var options = new DistributedCacheEntryOptions
4330 {
44- //Set the time the cache will expire from the time of entry (representing now)
45- AbsoluteExpirationRelativeToNow = TimeSpan . FromMinutes ( options . AbsoluteExpireTime ) ,
46-
47- //Time until the cache entry is valid, before which if a hit occurs the time must be extended further.
48- SlidingExpiration = TimeSpan . FromMinutes ( options . SlidingExpireTime )
31+ AbsoluteExpirationRelativeToNow = optionsCache . AbsoluteExpireTime ,
32+ SlidingExpiration = optionsCache . SlidingExpireTime
4933 } ;
5034
51- cache . SetString ( key , JsonConvert . SerializeObject ( value ) , redisOptions ) ;
35+ var jsonData = System . Text . Json . JsonSerializer . Serialize ( value ) ;
36+
37+ await cache . SetStringAsync ( key , jsonData , options ) ;
5238
5339 return value ;
5440 }
0 commit comments