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

Commit 2b4296f

Browse files
committed
Refactoring RedisCache
1 parent e8e171e commit 2b4296f

File tree

5 files changed

+70
-61
lines changed

5 files changed

+70
-61
lines changed

src/NET6CustomLibrary/RedisCache/CacheService.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/NET6CustomLibrary/RedisCache/ICacheService.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NET6CustomLibrary.MailKit.Options;
2+
3+
public class RedisOptions
4+
{
5+
public string InstanceName { get; set; }
6+
public int AbsoluteExpireTime { get; set; }
7+
public int SlidingExpireTime { get; set; }
8+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace NET6CustomLibrary.RedisCache.Services;
2+
3+
public class CacheService : ICacheService
4+
{
5+
private readonly IDistributedCache cache;
6+
private readonly IOptionsMonitor<RedisOptions> redisOptionsMonitor;
7+
8+
public CacheService(IDistributedCache cache, IOptionsMonitor<RedisOptions> redisOptionsMonitor)
9+
{
10+
this.cache = cache;
11+
this.redisOptionsMonitor = redisOptionsMonitor;
12+
}
13+
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)
21+
{
22+
var value = cache.GetString(key);
23+
24+
if (value != null)
25+
{
26+
return JsonConvert.DeserializeObject<T>(value);
27+
}
28+
29+
return default;
30+
}
31+
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)
40+
{
41+
var options = redisOptionsMonitor.CurrentValue;
42+
var redisOptions = new DistributedCacheEntryOptions
43+
{
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)
49+
};
50+
51+
cache.SetString(key, JsonConvert.SerializeObject(value), redisOptions);
52+
53+
return value;
54+
}
55+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NET6CustomLibrary.RedisCache.Services;
2+
3+
public interface ICacheService
4+
{
5+
T GetCache<T>(string key);
6+
T SetCache<T>(string key, T value);
7+
}

0 commit comments

Comments
 (0)