-
Notifications
You must be signed in to change notification settings - Fork 2
/
CacheAspect.cs
48 lines (44 loc) · 1.75 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
39
40
41
42
43
44
45
46
47
48
using CarRental.Core.CrossCuttingConcerns.Caching;
using CarRental.Core.Utilities.Interceptors;
using CarRental.Core.Utilities.IoC;
using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
namespace CarRental.Core.Aspects.Autofac.Caching
{
/// <summary>
/// It has 6o minutes as default value for the duration.
/// </summary>
public class CacheAspect : MethodInterception
{
private int _duration;
private ICacheManager _cacheManager;
public CacheAspect(int duration = 60)
{
_duration = duration;
_cacheManager = ServiceTool.ServiceProvider.GetService<ICacheManager>(); // specifies which service to use.
}
/// <summary>
/// Checks cache memory for the related operation.
/// If founds any matching data with the operation, returns the data.
/// Else the operation proceeds and the result set of data will be added to the cache for a given time.
/// </summary>
/// <param name="invocation">Related method.</param>
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);
}
}
}