Skip to content

Commit

Permalink
🚧 SQLite and Redis Caching provider impl RemoveByPrefix method.
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Jan 31, 2018
1 parent dc1c53d commit 1969ba1
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace EasyCaching.Demo.HybridCache.Controllers
{
using EasyCaching.HybridCache;
{
using EasyCaching.Core;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;

[Route("api/[controller]")]
public class ValuesController : Controller
{
Expand Down
12 changes: 12 additions & 0 deletions src/EasyCaching.Core/IEasyCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,17 @@ public interface IEasyCachingProvider
/// <param name="expiration">Expiration.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
Task RefreshAsync<T>(string cacheKey, T cacheValue, TimeSpan expiration) where T : class;

/// <summary>
/// Removes cached item by cachekey's prefix.
/// </summary>
/// <param name="prefix">Prefix of CacheKey.</param>
void RemoveByPrefix(string prefix);

/// <summary>
/// Removes cached item by cachekey's prefix async.
/// </summary>
/// <param name="prefix">Prefix of CacheKey.</param>
Task RemoveByPrefixAsync(string prefix);
}
}
7 changes: 7 additions & 0 deletions src/EasyCaching.Core/IHybridCachingProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EasyCaching.Core
{
/// <summary>
/// Hybrid caching provider.
/// </summary>
public interface IHybridCachingProvider : IEasyCachingProvider { }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace EasyCaching.HybridCache
{
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down
15 changes: 10 additions & 5 deletions src/EasyCaching.HybridCache/HybridCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
using System;
using System.Threading.Tasks;

/// <summary>
/// Hybrid caching provider.
/// </summary>
public interface IHybridCachingProvider : IEasyCachingProvider{}

/// <summary>
/// Hybrid caching provider.
/// </summary>
Expand Down Expand Up @@ -331,5 +326,15 @@ public async Task RefreshAsync<T>(string cacheKey, T cacheValue, TimeSpan expira
await this.RemoveAsync(cacheKey);
await this.SetAsync(cacheKey, cacheValue, expiration);
}

public void RemoveByPrefix(string prefix)
{
throw new NotImplementedException();
}

public Task RemoveByPrefixAsync(string prefix)
{
throw new NotImplementedException();
}
}
}
10 changes: 10 additions & 0 deletions src/EasyCaching.InMemory/InMemoryCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,15 @@ public async Task RefreshAsync<T>(string cacheKey, T cacheValue, TimeSpan expira
await this.RemoveAsync(cacheKey);
await this.SetAsync(cacheKey, cacheValue, expiration);
}

public void RemoveByPrefix(string prefix)
{
throw new NotImplementedException();
}

public Task RemoveByPrefixAsync(string prefix)
{
throw new NotImplementedException();
}
}
}
10 changes: 10 additions & 0 deletions src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,15 @@ public async Task RefreshAsync<T>(string cacheKey, T cacheValue, TimeSpan expira
await this.RemoveAsync(cacheKey);
await this.SetAsync(cacheKey, cacheValue, expiration);
}

public void RemoveByPrefix(string prefix)
{
throw new NotImplementedException();
}

public Task RemoveByPrefixAsync(string prefix)
{
throw new NotImplementedException();
}
}
}
114 changes: 113 additions & 1 deletion src/EasyCaching.Redis/DefaultRedisCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using EasyCaching.Core.Internal;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

/// <summary>
Expand All @@ -16,6 +18,11 @@ public class DefaultRedisCachingProvider : IEasyCachingProvider
/// </summary>
private readonly IDatabase _cache;

/// <summary>
/// The servers.
/// </summary>
private readonly IEnumerable<IServer> _servers;

/// <summary>
/// The db provider.
/// </summary>
Expand All @@ -28,7 +35,7 @@ public class DefaultRedisCachingProvider : IEasyCachingProvider

/// <summary>
/// <see cref="T:EasyCaching.Redis.DefaultRedisCachingProvider"/>
/// is not distributed cache.
/// is distributed cache.
/// </summary>
public bool IsDistributedCache => false;

Expand All @@ -47,6 +54,7 @@ public DefaultRedisCachingProvider(
_dbProvider = dbProvider;
_serializer = serializer;
_cache = _dbProvider.GetDatabase();
_servers = _dbProvider.GetServerList();
}

/// <summary>
Expand Down Expand Up @@ -279,5 +287,109 @@ public async Task RefreshAsync<T>(string cacheKey, T cacheValue, TimeSpan expira
await this.RemoveAsync(cacheKey);
await this.SetAsync(cacheKey, cacheValue, expiration);
}

/// <summary>
/// Removes cached item by cachekey's prefix.
/// </summary>
/// <param name="prefix">Prefix of CacheKey.</param>
public void RemoveByPrefix(string prefix)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

this.HandlePrefix(prefix);

foreach (var server in _servers)
{
this.HandleKeyDelWithTran(server, prefix);
}
}

/// <summary>
/// Removes cached item by cachekey's prefix async.
/// </summary>
/// <param name="prefix">Prefix of CacheKey.</param>
public async Task RemoveByPrefixAsync(string prefix)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

this.HandlePrefix(prefix);

foreach (var server in _servers)
{
await this.HandleKeyDelWithTranAsync(server, prefix);
}
}

/// <summary>
/// Delete keys of Redis using transaction.
/// </summary>
/// <param name="server">Server.</param>
/// <param name="prefix">Prefix.</param>
private void HandleKeyDelWithTran(IServer server, string prefix)
{
int count = 1;
do
{
var keys = server.Keys(pattern: prefix, pageSize: 100);
count = keys.Count();
if (count > 0)
{
var tran = _cache.CreateTransaction();

tran.KeyDeleteAsync(keys.ToArray());

tran.Execute(CommandFlags.FireAndForget);
}
}
while (count <= 0);
}

/// <summary>
/// Delete keys of Redis using transaction async.
/// </summary>
/// <returns>The key del with tran async.</returns>
/// <param name="server">Server.</param>
/// <param name="prefix">Prefix.</param>
private async Task HandleKeyDelWithTranAsync(IServer server, string prefix)
{
int count = 1;
do
{
var keys = server.Keys(pattern: prefix, pageSize: 100);
count = keys.Count();
if (count > 0)
{
var tran = _cache.CreateTransaction();

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

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)
{
// 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, "*");
}
}
}
}
5 changes: 5 additions & 0 deletions src/EasyCaching.SQLite/ConstSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ FROM [easycaching]
/// </summary>
public const string REMOVESQL = @"DELETE FROM [easycaching] WHERE [cachekey] = @cachekey ";

/// <summary>
/// The removebyprefixsql.
/// </summary>
public const string REMOVEBYPREFIXSQL = @"DELETE FROM [easycaching] WHERE [cachekey] like @cachekey ";

/// <summary>
/// The existssql.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions src/EasyCaching.SQLite/SQLiteCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,27 @@ public async Task RefreshAsync<T>(string cacheKey, T cacheValue, TimeSpan expira
await this.RemoveAsync(cacheKey);
await this.SetAsync(cacheKey, cacheValue, expiration);
}

/// <summary>
/// Removes cached item by cachekey's prefix.
/// </summary>
/// <param name="prefix">Prefix of CacheKey.</param>
public void RemoveByPrefix(string prefix)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

_cache.Execute(ConstSQL.REMOVEBYPREFIXSQL, new { cachekey = string.Concat(prefix, "%") });
}

/// <summary>
/// Removes cached item by cachekey's prefix async.
/// </summary>
/// <param name="prefix">Prefix of CacheKey.</param>
public async Task RemoveByPrefixAsync(string prefix)
{
ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix));

await _cache.ExecuteAsync(ConstSQL.REMOVEBYPREFIXSQL, new { cachekey = string.Concat(prefix ,"%") });
}
}
}

0 comments on commit 1969ba1

Please sign in to comment.