Skip to content

Commit 79edf0f

Browse files
MoienTajikMoien Tajik
andauthored
add persist key method to redis providers (#392)
Co-authored-by: Moien Tajik <m.tajik@alibaba.ir>
1 parent 57711ef commit 79edf0f

File tree

4 files changed

+127
-59
lines changed

4 files changed

+127
-59
lines changed

src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Keys.cs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
namespace EasyCaching.CSRedis
1+
namespace EasyCaching.CSRedis
22
{
33
using EasyCaching.Core;
44
using System.Collections.Generic;
5-
using System.Threading.Tasks;
6-
7-
public partial class DefaultCSRedisCachingProvider : IRedisCachingProvider
8-
{
9-
public string RedisName => this._name;
10-
5+
using System.Threading.Tasks;
6+
7+
public partial class DefaultCSRedisCachingProvider : IRedisCachingProvider
8+
{
9+
public string RedisName => this._name;
10+
1111
public bool KeyDel(string cacheKey)
1212
{
1313
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
@@ -38,22 +38,38 @@ public async Task<bool> KeyExpireAsync(string cacheKey, int second)
3838

3939
var flag = await _cache.ExpireAsync(cacheKey, second);
4040
return flag;
41-
}
42-
43-
public bool KeyExists(string cacheKey)
44-
{
45-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
46-
47-
var flag = _cache.Exists(cacheKey);
48-
return flag;
49-
}
50-
51-
public async Task<bool> KeyExistsAsync(string cacheKey)
52-
{
53-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
54-
55-
var flag = await _cache.ExistsAsync(cacheKey);
56-
return flag;
41+
}
42+
43+
public bool KeyPersist(string cacheKey)
44+
{
45+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
46+
47+
var flag = _cache.Persist(cacheKey);
48+
return flag;
49+
}
50+
51+
public async Task<bool> KeyPersistAsync(string cacheKey)
52+
{
53+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
54+
55+
var flag = await _cache.PersistAsync(cacheKey);
56+
return flag;
57+
}
58+
59+
public bool KeyExists(string cacheKey)
60+
{
61+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
62+
63+
var flag = _cache.Exists(cacheKey);
64+
return flag;
65+
}
66+
67+
public async Task<bool> KeyExistsAsync(string cacheKey)
68+
{
69+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
70+
71+
var flag = await _cache.ExistsAsync(cacheKey);
72+
return flag;
5773
}
5874

5975
public long TTL(string cacheKey)
@@ -123,5 +139,5 @@ public async Task<List<string>> SearchKeysAsync(string cacheKey, int? count)
123139

124140
return keys;
125141
}
126-
}
127-
}
142+
}
143+
}

src/EasyCaching.Core/IRedisCachingProvider.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public interface IRedisCachingProvider
4242
/// <returns></returns>
4343
Task<bool> KeyExpireAsync(string cacheKey, int second);
4444
/// <summary>
45+
/// https://redis.io/commands/persist
46+
/// </summary>
47+
/// <param name="cacheKey"></param>
48+
/// <returns></returns>
49+
bool KeyPersist(string cacheKey);
50+
/// <summary>
51+
/// https://redis.io/commands/persist
52+
/// </summary>
53+
/// <param name="cacheKey"></param>
54+
/// <returns></returns>
55+
Task<bool> KeyPersistAsync(string cacheKey);
56+
/// <summary>
4557
///
4658
/// </summary>
4759
/// <returns></returns>

src/EasyCaching.Redis/DefaultRedisCachingProvider.Keys.cs

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
namespace EasyCaching.Redis
2-
{
1+
namespace EasyCaching.Redis
2+
{
33
using EasyCaching.Core;
44
using StackExchange.Redis;
55
using System;
66
using System.Collections.Generic;
7-
using System.Linq;
8-
using System.Threading.Tasks;
9-
10-
/// <summary>
11-
/// Default redis caching provider.
12-
/// </summary>
13-
public partial class DefaultRedisCachingProvider : IRedisCachingProvider
14-
{
15-
public string RedisName => this._name;
16-
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
/// <summary>
11+
/// Default redis caching provider.
12+
/// </summary>
13+
public partial class DefaultRedisCachingProvider : IRedisCachingProvider
14+
{
15+
public string RedisName => this._name;
16+
1717
public bool KeyDel(string cacheKey)
1818
{
1919
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
@@ -44,23 +44,39 @@ public async Task<bool> KeyExpireAsync(string cacheKey, int second)
4444

4545
var flag = await _cache.KeyExpireAsync(cacheKey, TimeSpan.FromSeconds(second));
4646
return flag;
47-
}
48-
49-
public bool KeyExists(string cacheKey)
50-
{
51-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
52-
53-
var flag = _cache.KeyExists(cacheKey);
54-
return flag;
55-
}
56-
57-
public async Task<bool> KeyExistsAsync(string cacheKey)
58-
{
59-
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
60-
61-
var flag = await _cache.KeyExistsAsync(cacheKey);
62-
return flag;
63-
}
47+
}
48+
49+
public bool KeyPersist(string cacheKey)
50+
{
51+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
52+
53+
var flag = _cache.KeyPersist(cacheKey);
54+
return flag;
55+
}
56+
57+
public async Task<bool> KeyPersistAsync(string cacheKey)
58+
{
59+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
60+
61+
var flag = await _cache.KeyPersistAsync(cacheKey);
62+
return flag;
63+
}
64+
65+
public bool KeyExists(string cacheKey)
66+
{
67+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
68+
69+
var flag = _cache.KeyExists(cacheKey);
70+
return flag;
71+
}
72+
73+
public async Task<bool> KeyExistsAsync(string cacheKey)
74+
{
75+
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));
76+
77+
var flag = await _cache.KeyExistsAsync(cacheKey);
78+
return flag;
79+
}
6480

6581
public long TTL(string cacheKey)
6682
{
@@ -128,8 +144,8 @@ public async Task<object> EvalAsync(string script, string cacheKey, List<object>
128144
var res = await _cache.ScriptEvaluateAsync(script, redisKey, redisValues.ToArray());
129145

130146
return res;
131-
}
132-
147+
}
148+
133149
public List<string> SearchKeys(string cacheKey, int? count)
134150
{
135151
var data = new List<string>();
@@ -156,6 +172,6 @@ public async Task<List<string>> SearchKeysAsync(string cacheKey, int? count)
156172
}
157173

158174
return data;
159-
}
160-
}
161-
}
175+
}
176+
}
177+
}

test/EasyCaching.UnitTests/CachingTests/BaseRedisFeatureCachingProviderTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ protected virtual async Task StringSet_And_KeyExpire_And_TTL_Async_Should_Succee
8181

8282
await _provider.KeyDelAsync(cacheKey);
8383
}
84+
85+
[Fact]
86+
protected virtual async Task StringSetWithExpiration_And_Persist_And_TTL_Async_Should_Succeed()
87+
{
88+
var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}";
89+
90+
var res = await _provider.StringSetAsync(cacheKey, "123", TimeSpan.FromSeconds(10));
91+
92+
Assert.True(res);
93+
94+
var initialTtl = await _provider.TTLAsync(cacheKey);
95+
96+
Assert.InRange(initialTtl, 1, 10);
97+
98+
var flag = await _provider.KeyPersistAsync(cacheKey);
99+
100+
Assert.True(flag);
101+
102+
var persistedTtl = await _provider.TTLAsync(cacheKey);
103+
104+
Assert.Equal(persistedTtl, -1);
105+
106+
await _provider.KeyDelAsync(cacheKey);
107+
}
84108
#endregion
85109

86110
#region String

0 commit comments

Comments
 (0)