Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 36b64f2

Browse files
committed
Refactoring RedisCache
1 parent ea64801 commit 36b64f2

File tree

4 files changed

+19
-31
lines changed

4 files changed

+19
-31
lines changed

src/NET6CustomLibrary/NET6CustomLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4949
</PackageReference>
5050
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
51+
<PackageReference Include="StackExchange.Redis" Version="2.6.111" />
5152
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
5253
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
5354
<PackageReference Include="System.Text.Json" Version="6.0.7" />

src/NET6CustomLibrary/RedisCache/Options/RedisOptions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
public class RedisOptions
44
{
5+
public string Hostname { get; set; }
56
public string InstanceName { get; set; }
6-
public int AbsoluteExpireTime { get; set; }
7-
public int SlidingExpireTime { get; set; }
7+
public TimeSpan AbsoluteExpireTime { get; set; }
8+
public TimeSpan SlidingExpireTime { get; set; }
89
}

src/NET6CustomLibrary/RedisCache/Services/CacheService.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/NET6CustomLibrary/RedisCache/Services/ICacheService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public interface ICacheService
44
{
5-
T GetCache<T>(string key);
6-
T SetCache<T>(string key, T value);
5+
Task<T> GetCacheAsync<T>(string key);
6+
Task<T> SetCacheAsync<T>(string key, T value);
77
}

0 commit comments

Comments
 (0)