Skip to content

Merge new commits. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
✨ Add Caching Interceptor.
  • Loading branch information
catcherwong committed Nov 28, 2017
commit 7fc1e869db81ecd0cbb3a16a1bed54653736c567
7 changes: 7 additions & 0 deletions EasyCaching.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Core", "src\Eas
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Memory", "src\EasyCaching.Memory\EasyCaching.Memory.csproj", "{B9490432-737B-4518-B851-9D40FD29B392}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyCaching.Extensions", "src\EasyCaching.Extensions\EasyCaching.Extensions.csproj", "{679ABDB5-7218-4B4E-A632-10612750CD74}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,9 +27,14 @@ Global
{B9490432-737B-4518-B851-9D40FD29B392}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9490432-737B-4518-B851-9D40FD29B392}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9490432-737B-4518-B851-9D40FD29B392}.Release|Any CPU.Build.0 = Release|Any CPU
{679ABDB5-7218-4B4E-A632-10612750CD74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{679ABDB5-7218-4B4E-A632-10612750CD74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{679ABDB5-7218-4B4E-A632-10612750CD74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{679ABDB5-7218-4B4E-A632-10612750CD74}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CE61FAA2-0233-451C-991D-4222ED61C84B} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
{B9490432-737B-4518-B851-9D40FD29B392} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
{679ABDB5-7218-4B4E-A632-10612750CD74} = {A0F5CC7E-155F-4726-8DEB-E966950B3FE9}
EndGlobalSection
EndGlobal
3 changes: 3 additions & 0 deletions src/EasyCaching.Core/EasyCaching.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="Internal\" />
</ItemGroup>
</Project>
20 changes: 0 additions & 20 deletions src/EasyCaching.Core/EasyCachingAttribute.cs

This file was deleted.

14 changes: 14 additions & 0 deletions src/EasyCaching.Core/Internal/ICachable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace EasyCaching.Core.Internal
{
/// <summary>
/// Cachable.
/// </summary>
public interface ICachable
{
/// <summary>
/// Gets the cache key.
/// </summary>
/// <value>The cache key.</value>
string CacheKey { get; }
}
}
173 changes: 173 additions & 0 deletions src/EasyCaching.Extensions/CachingInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
namespace EasyCaching.Extensions
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using AspectCore.DynamicProxy;
using AspectCore.Injector;
using EasyCaching.Core.Internal;
using EasyCaching.Core;

/// <summary>
/// Caching interceptor.
/// </summary>
public class CachingInterceptor : AbstractInterceptorAttribute
{

/// <summary>
/// Gets or sets the absolute expiration.
/// </summary>
/// <value>The absolute expiration.</value>
public int AbsoluteExpiration { get; set; } = 30;


/// <summary>
/// Gets or sets the parameter count to generate cache key.
/// </summary>
/// <value>The parameter count.</value>
public int ParamCount { get; set; } = 5;


/// <summary>
/// Gets or sets the cache provider.
/// </summary>
/// <value>The cache provider.</value>
[FromContainer]
public IEasyCachingProvider CacheProvider { get; set; }

/// <summary>
/// The link char of cache key.
/// </summary>
private char _linkChar = ':';

/// <summary>
/// Invoke the specified context and next.
/// </summary>
/// <returns>The invoke.</returns>
/// <param name="context">Context.</param>
/// <param name="next">Next.</param>
public async override Task Invoke(AspectContext context, AspectDelegate next)
{
var interceptorAttribute = GetInterceptorAttributeInfo(context.ServiceMethod);
if (interceptorAttribute != null)
{
await ProceedCaching(context, next, interceptorAttribute);
}
else
{
await next(context);
}
}

/// <summary>
/// Gets the QC aching attribute info.
/// </summary>
/// <returns>The QC aching attribute info.</returns>
/// <param name="method">Method.</param>
private CachingInterceptor GetInterceptorAttributeInfo(MethodInfo method)
{
return method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingInterceptor)) as CachingInterceptor;
}

/// <summary>
/// Proceeds the caching.
/// </summary>
/// <returns>The caching.</returns>
/// <param name="context">Context.</param>
/// <param name="next">Next.</param>
/// <param name="attribute">Attribute.</param>
private async Task ProceedCaching(AspectContext context, AspectDelegate next, CachingInterceptor attribute)
{
var cacheKey = GenerateCacheKey(context, attribute.ParamCount);

var cacheValue = CacheProvider.Get(cacheKey);
if (cacheValue != null)
{
context.ReturnValue = cacheValue;
return;
}

await next(context);

if (!string.IsNullOrWhiteSpace(cacheKey))
{
CacheEntry entry = new CacheEntry(cacheKey, context.ReturnValue, TimeSpan.FromSeconds(attribute.AbsoluteExpiration));
CacheProvider.Set(entry);
}
}

/// <summary>
/// Generates the cache key.
/// </summary>
/// <returns>The cache key.</returns>
/// <param name="context">Context.</param>
/// <param name="paramCount">Parameter count.</param>
private string GenerateCacheKey(AspectContext context, int paramCount)
{
var typeName = context.ServiceMethod.DeclaringType.Name;
var methodName = context.ServiceMethod.Name;
var methodArguments = this.FormatArgumentsToPartOfCacheKey(context.ServiceMethod.GetParameters(), paramCount);

return this.GenerateCacheKey(typeName, methodName, methodArguments);
}

/// <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);
}

return builder.ToString().TrimEnd(_linkChar);
}

/// <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>
/// <param name="paramCount">Max parameter count.</param>
private IList<string> FormatArgumentsToPartOfCacheKey(IList<ParameterInfo> methodArguments, int paramCount = 5)
{
return methodArguments.Select(this.GetArgumentValue).Take(paramCount).ToList();
}

/// <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;
}
}
}
14 changes: 14 additions & 0 deletions src/EasyCaching.Extensions/EasyCaching.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspectCore.Core" Version="0.2.4" />
<PackageReference Include="AspectCore.Extensions.DependencyInjection" Version="0.2.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EasyCaching.Core\EasyCaching.Core.csproj" />
</ItemGroup>
</Project>