Skip to content

Commit

Permalink
🔨 Refactoring Caching Interceptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Jan 26, 2018
1 parent 87d5a20 commit dc82367
Show file tree
Hide file tree
Showing 13 changed files with 606 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public interface IDateTimeService : EasyCaching.Core.Internal.IEasyCaching
{
[EasyCachingInterceptor(Expiration = 10)]
[EasyCachingAble(Expiration = 10)]
string GetCurrentUtcTime();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IDateTimeService

public class DateTimeService : IDateTimeService , IEasyCaching
{
[EasyCachingInterceptor(Expiration = 10)]
[EasyCachingAble(Expiration = 10)]
public string GetCurrentUtcTime()
{
return System.DateTime.UtcNow.ToString();
Expand Down
107 changes: 107 additions & 0 deletions src/EasyCaching.Core/DefaultEasyCachingKeyGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
namespace EasyCaching.Core
{
using EasyCaching.Core.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// Default easycaching key generator.
/// </summary>
public class DefaultEasyCachingKeyGenerator : IEasyCachingKeyGenerator
{
/// <summary>
/// The link char of cache key.
/// </summary>
private char _linkChar = ':';

/// <summary>
/// Gets the cache key.
/// </summary>
/// <returns>The cache key.</returns>
/// <param name="methodInfo">Method info.</param>
/// <param name="prefix">Prefix.</param>
public string GetCacheKey(MethodInfo methodInfo, string prefix)
{
if(string.IsNullOrWhiteSpace(prefix))
{
var typeName = methodInfo.DeclaringType.Name;
var methodName = methodInfo.Name;

var methodArguments = this.FormatArgumentsToPartOfCacheKey(methodInfo.GetParameters());

return this.GenerateCacheKey(typeName, methodName, methodArguments);
}
else
{
var methodArguments = this.FormatArgumentsToPartOfCacheKey(methodInfo.GetParameters());

return this.GenerateCacheKey(string.Empty, prefix, methodArguments);
}
}

/// <summary>
/// Formats the arguments to part of cache key.
/// </summary>
/// <returns>The arguments to part of cache key.</returns>
/// <param name="methodArguments">Method arguments.</param>
private IList<string> FormatArgumentsToPartOfCacheKey(IList<ParameterInfo> methodArguments)
{
return methodArguments.Select(this.GetArgumentValue).ToList();
}

/// <summary>
/// Generates the cache key.
/// </summary>
/// <returns>The cache key.</returns>
/// <param name="typeName">Type name.</param>
/// <param name="methodName">Method name.</param>
/// <param name="parameters">Parameters.</param>
private string GenerateCacheKey(string typeName, string methodName, IList<string> parameters)
{
var builder = new StringBuilder();

builder.Append(typeName);
builder.Append(_linkChar);

builder.Append(methodName);
builder.Append(_linkChar);

foreach (var param in parameters)
{
builder.Append(param);
builder.Append(_linkChar);
}

var str = builder.ToString().TrimEnd(_linkChar);

using (SHA1 sha1 = SHA1.Create())
{
byte[] data = sha1.ComputeHash(Encoding.UTF8.GetBytes(str));
return Convert.ToBase64String(data, Base64FormattingOptions.None);
}
}

/// <summary>
/// Gets the argument value.
/// </summary>
/// <returns>The argument value.</returns>
/// <param name="arg">Argument.</param>
private string GetArgumentValue(object arg)
{
if (arg is int || arg is long || arg is string)
return arg.ToString();

if (arg is DateTime)
return ((DateTime)arg).ToString("yyyyMMddHHmmss");

if (arg is ICachable)
return ((ICachable)arg).CacheKey;

return null;
}
}
}
19 changes: 19 additions & 0 deletions src/EasyCaching.Core/IEasyCachingKeyGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace EasyCaching.Core
{
using System.Reflection;

/// <summary>
/// Easycaching key generator.
/// </summary>
public interface IEasyCachingKeyGenerator
{

/// <summary>
/// Gets the cache key.
/// </summary>
/// <returns>The cache key.</returns>
/// <param name="methodInfo">Method info.</param>
/// <param name="prefix">Prefix.</param>
string GetCacheKey(MethodInfo methodInfo, string prefix);
}
}
53 changes: 49 additions & 4 deletions src/EasyCaching.Core/Internal/EasyCachingInterceptorAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,60 @@
public class EasyCachingInterceptorAttribute : Attribute
{
/// <summary>
/// Gets or sets the expiration.
/// Gets or sets a value indicating whether is hybrid provider.
/// </summary>
/// <value><c>true</c> if is hybrid provider; otherwise, <c>false</c>.</value>
public bool IsHybridProvider { get; set; } = false;

/// <summary>
/// Gets or sets the cache key prefix.
/// </summary>
/// <value>The cache key prefix.</value>
public string CacheKeyPrefix { get; set; } = string.Empty;
}

/// <summary>
/// Easycaching able attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public class EasyCachingAbleAttribute : EasyCachingInterceptorAttribute
{
/// <summary>
/// Gets or sets the expiration. The default value is 30 second.
/// </summary>
/// <value>The expiration.</value>
public int Expiration { get; set; } = 30;
}

/// <summary>
/// Easycaching put attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public class EasyCachingPutAttribute : EasyCachingInterceptorAttribute
{
/// <summary>
/// Gets or sets the expiration. The default value is 30 second.
/// </summary>
/// <value>The expiration.</value>
public int Expiration { get; set; } = 30;
}

/// <summary>
/// Easycaching evict attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public class EasyCachingEvictAttribute : EasyCachingInterceptorAttribute
{
/// <summary>
/// Gets or sets a value indicating whether evict all cached values which are
/// </summary>
/// <value><c>true</c> if all; otherwise, <c>false</c>.</value>
public bool All { get; set; } = false;

/// <summary>
/// Gets or sets the parameter count.
/// Gets or sets a value indicating whether is before.
/// </summary>
/// <value>The parameter count.</value>
public int ParamCount { get; set; } = 5;
/// <value><c>true</c> if is before; otherwise, <c>false</c>.</value>
public bool IsBefore { get; set; } = false;
}
}
9 changes: 1 addition & 8 deletions src/EasyCaching.Core/Internal/ServerEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@
/// Defines an endpoint.
/// </summary>
public class ServerEndPoint
{
/// <summary>
/// Initializes a new instance of the <see cref="T:EasyCaching.Redis.ServerEndPoint"/> class.
/// </summary>
public ServerEndPoint()
{
}

{
/// <summary>
/// Initializes a new instance of the <see cref="T:EasyCaching.Redis.ServerEndPoint"/> class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using global::AspectCore.Configuration;
using global::AspectCore.Extensions.DependencyInjection;
using global::AspectCore.Injector;
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;
Expand All @@ -19,6 +20,8 @@ public static class AspectCoreInterceptorServiceCollectionExtensions
/// <param name="services">Services.</param>
public static IServiceProvider ConfigureAspectCoreInterceptor(this IServiceCollection services)
{

services.AddSingleton<IEasyCachingKeyGenerator,DefaultEasyCachingKeyGenerator>();
var container = services.ToServiceContainer();

container.Configure(config =>
Expand Down

0 comments on commit dc82367

Please sign in to comment.