Skip to content

Commit

Permalink
Merge pull request #7 from catcherwong/dev
Browse files Browse the repository at this point in the history
RemoveByPrefix(Async) for Redis and SQLite caching provider.
  • Loading branch information
catcherwong authored Feb 2, 2018
2 parents 1969ba1 + 4da5dfb commit 864c16a
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 14 deletions.
30 changes: 17 additions & 13 deletions src/EasyCaching.Redis/DefaultRedisCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public CacheValue<T> Get<T>(string cacheKey) where T : class
else
{
return CacheValue<T>.NoValue;
}
}
}

/// <summary>
Expand All @@ -162,7 +162,7 @@ public async Task<CacheValue<T>> GetAsync<T>(string cacheKey) where T : class
else
{
return CacheValue<T>.NoValue;
}
}
}

/// <summary>
Expand Down Expand Up @@ -251,7 +251,7 @@ public async Task<bool> ExistsAsync(string cacheKey)
ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey));

return await _cache.KeyExistsAsync(cacheKey);
}
}

/// <summary>
/// Refresh the specified cacheKey, cacheValue and expiration.
Expand Down Expand Up @@ -296,7 +296,7 @@ public void RemoveByPrefix(string prefix)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

this.HandlePrefix(prefix);
prefix = this.HandlePrefix(prefix);

foreach (var server in _servers)
{
Expand All @@ -312,7 +312,7 @@ public async Task RemoveByPrefixAsync(string prefix)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

this.HandlePrefix(prefix);
prefix = this.HandlePrefix(prefix);

foreach (var server in _servers)
{
Expand Down Expand Up @@ -361,35 +361,39 @@ private async Task HandleKeyDelWithTranAsync(IServer server, string prefix)
{
var tran = _cache.CreateTransaction();

await tran.KeyDeleteAsync(keys.ToArray());
Task delTask = tran.KeyDeleteAsync(keys.ToArray());

Task execTask = tran.ExecuteAsync(CommandFlags.FireAndForget);

await Task.WhenAll(delTask, execTask);

//await tran.KeyDeleteAsync(keys.ToArray());

await tran.ExecuteAsync(CommandFlags.FireAndForget);
//await tran.ExecuteAsync(CommandFlags.FireAndForget);
}
}
while (count <= 0);
}

/// <summary>
/// Handles the prefix of CacheKey.
/// </summary>
/// <param name="prefix">Prefix of CacheKey.</param>
/// <exception cref="ArgumentException">
private void HandlePrefix(string prefix)
private string HandlePrefix(string prefix)
{
// Forbid
if (prefix.Equals("*"))
{
throw new ArgumentException("the prefix should not to *");
}

// Don't start with *
prefix = new System.Text.RegularExpressions.Regex("^\\*+").Replace(prefix, "");

// End with *
if (!prefix.EndsWith("*", StringComparison.OrdinalIgnoreCase))
{
prefix = string.Concat(prefix, "*");
}

return prefix;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,61 @@ public async Task Refresh_Async_Should_Succeed()

Assert.Equal("NewValue", act.Value);
}



[Fact]
public void RemoveByPrefix_Should_Succeed()
{
SetCacheItem("demo:1", "1");
SetCacheItem("demo:2", "2");
SetCacheItem("demo:3", "3");
SetCacheItem("demo:4", "4");

_provider.RemoveByPrefix("demo");

var demo1 = _provider.Get<string>("demo:1");
var demo2 = _provider.Get<string>("demo:2");
var demo3 = _provider.Get<string>("demo:3");
var demo4 = _provider.Get<string>("demo:4");

Assert.False(demo1.HasValue);
Assert.False(demo2.HasValue);
Assert.False(demo3.HasValue);
Assert.False(demo4.HasValue);
}

[Fact]
public async Task RemoveByPrefixAsync_Should_Succeed()
{
SetCacheItem("demo:1", "1");
SetCacheItem("demo:2", "2");
SetCacheItem("demo:3", "3");
SetCacheItem("demo:4", "4");

await _provider.RemoveByPrefixAsync("demo");

var demo1 = _provider.Get<string>("demo:1");
var demo2 = _provider.Get<string>("demo:2");
var demo3 = _provider.Get<string>("demo:3");
var demo4 = _provider.Get<string>("demo:4");

Assert.False(demo1.HasValue);
Assert.False(demo2.HasValue);
Assert.False(demo3.HasValue);
Assert.False(demo4.HasValue);
}

private void SetCacheItem(string cacheKey, string cacheValue)
{
var cacheBytes = new byte[] { 0x01 };

A.CallTo(() => _serializer.Serialize(cacheValue)).Returns(cacheBytes);
A.CallTo(() => _serializer.Deserialize<string>(A<byte[]>.Ignored)).Returns(cacheValue);

_provider.Set(cacheKey, cacheValue, _defaultTs);

var val = _provider.Get<string>(cacheKey);
Assert.Equal(cacheValue, val.Value);
}
}
}
54 changes: 54 additions & 0 deletions test/EasyCaching.UnitTests/CachingTests/SQLiteCachingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,59 @@ public async Task Refresh_Async_Should_Succeed()

Assert.Equal("NewValue", act.Value);
}

[Fact]
public void RemoveByPrefix_Should_Succeed()
{
var cacheKey1 = "PREFIX:1";
var cacheValue1 = "value1";
_provider.Set(cacheKey1, cacheValue1, _defaultTs);

var cacheKey2 = "PREFIX:2";
var cacheValue2 = "value2";
_provider.Set(cacheKey2, cacheValue2, _defaultTs);


var tmpRes1 = _provider.Get<string>(cacheKey1);
var tmpRes2 = _provider.Get<string>(cacheKey2);

Assert.Equal(cacheValue1, tmpRes1.Value);
Assert.Equal(cacheValue2, tmpRes2.Value);

_provider.RemoveByPrefix("PREFIX");

var res1 = _provider.Get<string>(cacheKey1);
var res2 = _provider.Get<string>(cacheKey2);

Assert.False(res1.HasValue);
Assert.False(res2.HasValue);
}

[Fact]
public async Task RemoveByPrefixAsync_Should_Succeed()
{
var cacheKey1 = "PREFIX:1";
var cacheValue1 = "value1";
await _provider.SetAsync(cacheKey1, cacheValue1, _defaultTs);

var cacheKey2 = "PREFIX:2";
var cacheValue2 = "value2";
await _provider.SetAsync(cacheKey2, cacheValue2, _defaultTs);


var tmpRes1 = await _provider.GetAsync<string>(cacheKey1);
var tmpRes2 = await _provider.GetAsync<string>(cacheKey2);

Assert.Equal(cacheValue1, tmpRes1.Value);
Assert.Equal(cacheValue2, tmpRes2.Value);

await _provider.RemoveByPrefixAsync("PREFIX");

var res1 = await _provider.GetAsync<string>(cacheKey1);
var res2 = await _provider.GetAsync<string>(cacheKey2);

Assert.False(res1.HasValue);
Assert.False(res2.HasValue);
}
}
}

0 comments on commit 864c16a

Please sign in to comment.