forked from ahmet-cetinkaya/ReCapProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cache, Transaction, Performance aspects
- Loading branch information
Ahmet Çetinkaya
committed
Mar 6, 2021
1 parent
8621c43
commit f17c7b3
Showing
10 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Linq; | ||
using Castle.DynamicProxy; | ||
using Core.CrossCuttingConcerns.Caching; | ||
using Core.Utilities.Interceptors; | ||
using Core.Utilities.IoC; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Core.Aspects.Autofac.Caching | ||
{ | ||
public class CacheAspect : MethodInterception | ||
{ | ||
private readonly ICacheManager _cacheManager; | ||
private readonly int _duration; | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Castle.DynamicProxy; | ||
using Core.CrossCuttingConcerns.Caching; | ||
using Core.Utilities.Interceptors; | ||
using Core.Utilities.IoC; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Core.Aspects.Autofac.Caching | ||
{ | ||
public class CacheRemoveAspect : MethodInterception | ||
{ | ||
private readonly ICacheManager _cacheManager; | ||
private readonly string _pattern; | ||
|
||
public CacheRemoveAspect(string pattern) | ||
{ | ||
_pattern = pattern; | ||
_cacheManager = ServiceTool.ServiceProvider.GetService<ICacheManager>(); | ||
} | ||
|
||
protected override void OnSuccess(IInvocation invocation) | ||
{ | ||
_cacheManager.RemoveByPattern(_pattern); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Diagnostics; | ||
using Castle.DynamicProxy; | ||
using Core.Utilities.Interceptors; | ||
using Core.Utilities.IoC; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Core.Aspects.Autofac.Performance | ||
{ | ||
public class PerformanceAspect : MethodInterception | ||
{ | ||
private readonly int _interval; | ||
private readonly Stopwatch _stopwatch; | ||
|
||
public PerformanceAspect(int interval) | ||
{ | ||
_interval = interval; | ||
_stopwatch = ServiceTool.ServiceProvider.GetService<Stopwatch>(); | ||
} | ||
|
||
|
||
protected override void OnBefore(IInvocation invocation) | ||
{ | ||
_stopwatch.Start(); | ||
} | ||
|
||
protected override void OnAfter(IInvocation invocation) | ||
{ | ||
if (_stopwatch.Elapsed.TotalSeconds > _interval) | ||
Debug.WriteLine( | ||
$"Performance : {invocation.Method.DeclaringType.FullName}.{invocation.Method.Name}-->{_stopwatch.Elapsed.TotalSeconds}"); | ||
_stopwatch.Reset(); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Core/Aspects/Autofac/Transaction/TransactionScopeAspect.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Transactions; | ||
using Castle.DynamicProxy; | ||
using Core.Utilities.Interceptors; | ||
|
||
namespace Core.Aspects.Autofac.Transaction | ||
{ | ||
public class TransactionScopeAspect : MethodInterception | ||
{ | ||
public override void Intercept(IInvocation invocation) | ||
{ | ||
using (var transactionScope = new TransactionScope()) | ||
{ | ||
try | ||
{ | ||
invocation.Proceed(); | ||
transactionScope.Complete(); | ||
} | ||
catch (Exception e) | ||
{ | ||
transactionScope.Dispose(); | ||
throw; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Core.CrossCuttingConcerns.Caching | ||
{ | ||
public interface ICacheManager | ||
{ | ||
void Add(string key, object value, int duration); | ||
T Get<T>(string key); | ||
object Get(string key); | ||
bool IsAdd(string key); | ||
void Remove(string key); | ||
void RemoveByPattern(string pattern); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
Core/CrossCuttingConcerns/Caching/Microsoft/MemoryCacheManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text.RegularExpressions; | ||
using Core.Utilities.IoC; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Core.CrossCuttingConcerns.Caching.Microsoft | ||
{ | ||
public class MemoryCacheManager : ICacheManager | ||
{ | ||
private readonly IMemoryCache _memoryCache; | ||
|
||
public MemoryCacheManager(IMemoryCache memoryCache) | ||
{ | ||
_memoryCache = ServiceTool.ServiceProvider.GetService<IMemoryCache>(); | ||
} | ||
|
||
public void Add(string key, object value, int duration) | ||
{ | ||
_memoryCache.Set(key, value, TimeSpan.FromMinutes(duration)); | ||
} | ||
|
||
public T Get<T>(string key) | ||
{ | ||
return _memoryCache.Get<T>(key); | ||
} | ||
|
||
public object Get(string key) | ||
{ | ||
return _memoryCache.Get(key); | ||
} | ||
|
||
public bool IsAdd(string key) | ||
{ | ||
return _memoryCache.TryGetValue(key, out _); | ||
} | ||
|
||
public void Remove(string key) | ||
{ | ||
_memoryCache.Remove(key); | ||
} | ||
|
||
public void RemoveByPattern(string pattern) | ||
{ | ||
var cacheEntriesCollectionDefinition = typeof(MemoryCache).GetProperty("EntriesCollection", | ||
BindingFlags.NonPublic | BindingFlags.Instance); | ||
var cacheEntriesCollection = cacheEntriesCollectionDefinition.GetValue(_memoryCache) as dynamic; | ||
var cacheCollectionValues = new List<ICacheEntry>(); | ||
|
||
foreach (var cacheItem in cacheEntriesCollection) | ||
{ | ||
ICacheEntry cacheItemValue = cacheItem.GetType().GetProperty("Value").GetValue(cacheItem, null); | ||
cacheCollectionValues.Add(cacheItemValue); | ||
} | ||
|
||
var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
var keysToRemove = cacheCollectionValues | ||
.Where(d => regex.IsMatch(d.Key.ToString())) | ||
.Select(d => d.Key) | ||
.ToList(); | ||
|
||
foreach (var key in keysToRemove) _memoryCache.Remove(key); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters