-
Notifications
You must be signed in to change notification settings - Fork 11
/
CacheAspect.cs
38 lines (35 loc) · 1.24 KB
/
CacheAspect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Core.CrossCuttingConcerns.Caching;
using Core.Utilities.Interceptors;
using Core.Utilities.IoC;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Castle.DynamicProxy;
using System.Linq;
namespace Core.Aspects.Autofac.Caching
{
public class CacheAspect : MethodInterception
{
private int _duration;
private ICacheManager _cacheManager;
public CacheAspect(int duration = 60)
{
_duration = duration;
_cacheManager = ServiceTool.ServiceProvider.GetService<ICacheManager>();
}
public override void Intercept(IInvocation invocation)
{
var methodName = string.Format($"{invocation.Method.ReflectedType.FullName}.{invocation.Method.Name}");
var arguments = invocation.Arguments.ToList();
var key = $"{methodName}({string.Join(",", arguments.Select(x => x?.ToString() ?? "<Null>"))})";
if (_cacheManager.IsAdd(key))
{
invocation.ReturnValue = _cacheManager.Get(key);
return;
}
invocation.Proceed();
_cacheManager.Add(key, invocation.ReturnValue, _duration);
}
}
}