From 3092efb5502994fba4140e60004ad70c842094e3 Mon Sep 17 00:00:00 2001 From: Noah Lerner Date: Wed, 29 Jun 2022 21:36:42 +0300 Subject: [PATCH 1/8] Add mustBeConfigured parameter which will fail cache access if cache not configured --- AopCaching.UnitTests/AopCacheTests.cs | 16 ++++++++++++++++ AopCaching.UnitTests/Mocks/Service2.cs | 16 ++++++++++++++++ AopCaching/CacheAttribute.cs | 15 ++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/AopCaching.UnitTests/AopCacheTests.cs b/AopCaching.UnitTests/AopCacheTests.cs index b71c4f3..d0e045e 100644 --- a/AopCaching.UnitTests/AopCacheTests.cs +++ b/AopCaching.UnitTests/AopCacheTests.cs @@ -71,6 +71,22 @@ public async Task TestMissingNameCacheAsync() Assert.AreEqual(2, result); } + [TestMethod] + [ExpectedException(typeof(CacheException))] + public void TestMissingNameCache_MustBeConfigured() + { + var service = new Service2(); + service.MethodToCache0Throws(); + } + + [TestMethod] + [ExpectedException(typeof(CacheException))] + public async Task TestMissingNameCacheAsync_MustBeConfigured() + { + var service = new Service2(); + await service.MethodToCache0ThrowsAsync(); + } + [TestMethod] public void TestNamedCache1() { diff --git a/AopCaching.UnitTests/Mocks/Service2.cs b/AopCaching.UnitTests/Mocks/Service2.cs index 6c8343d..aa555e3 100644 --- a/AopCaching.UnitTests/Mocks/Service2.cs +++ b/AopCaching.UnitTests/Mocks/Service2.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace PubComp.Caching.AopCaching.UnitTests.Mocks { @@ -21,6 +22,21 @@ public async Task MethodToCache0Async() return ++methodToCache0Counter; } + [Cache("CacheMissing", true)] + public int MethodToCache0Throws() + { + Assert.Fail($"This code should not be reached"); + return 0; + } + + [Cache("CacheMissing", true)] + public async Task MethodToCache0ThrowsAsync() + { + await Task.Delay(10); + Assert.Fail($"This code should not be reached"); + return 0; + } + [Cache("localCache")] public IEnumerable MethodToCache1() { diff --git a/AopCaching/CacheAttribute.cs b/AopCaching/CacheAttribute.cs index dd30db6..148a7d0 100644 --- a/AopCaching/CacheAttribute.cs +++ b/AopCaching/CacheAttribute.cs @@ -8,6 +8,7 @@ using System.Reflection; using System.Threading; using System.Threading.Tasks; +using PubComp.Caching.Core.Config.Loaders; namespace PubComp.Caching.AopCaching { @@ -15,6 +16,7 @@ namespace PubComp.Caching.AopCaching public class CacheAttribute : MethodInterceptionAspect { private string cacheName; + private bool mustBeConfigured; private ICache cache; private string className; private string methodName; @@ -27,9 +29,14 @@ public CacheAttribute() { } - public CacheAttribute(string cacheName) + public CacheAttribute(string cacheName) : this(cacheName, false) + { + } + + public CacheAttribute(string cacheName, bool mustBeConfigured) { this.cacheName = cacheName; + this.mustBeConfigured = mustBeConfigured; } public sealed override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo) @@ -69,6 +76,9 @@ public sealed override void OnInvoke(MethodInterceptionArgs args) this.cache = CacheManager.GetCache(this.cacheName); if (this.cache == null) { + if (this.mustBeConfigured) + throw new CacheException($"This cache has mustBeConfigured = true, so it must be configured."); + LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!"); } } @@ -94,6 +104,9 @@ public sealed override async Task OnInvokeAsync(MethodInterceptionArgs args) this.cache = CacheManager.GetCache(this.cacheName); if (this.cache == null) { + if (this.mustBeConfigured) + throw new CacheException($"This cache has mustBeConfigured = true, so it must be configured."); + LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!"); } } From 557772fec96215620633f74ae980b9554a211ca8 Mon Sep 17 00:00:00 2001 From: Noah Lerner Date: Wed, 29 Jun 2022 21:50:24 +0300 Subject: [PATCH 2/8] version --- AopCaching/AopCaching.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AopCaching/AopCaching.csproj b/AopCaching/AopCaching.csproj index 25db13f..3737ff8 100644 --- a/AopCaching/AopCaching.csproj +++ b/AopCaching/AopCaching.csproj @@ -5,9 +5,9 @@ PubComp.Caching.AopCaching PubComp.Caching.AopCaching true - 5.0.2 + 5.0.3 5.0.0.0 - 5.0.2.0 + 5.0.3.0 1701;1702;1591 From 74281c7b83eb44165bca9559c48ec667fd549cf7 Mon Sep 17 00:00:00 2001 From: Noah Lerner Date: Wed, 29 Jun 2022 22:25:08 +0300 Subject: [PATCH 3/8] constructor improvements --- AopCaching/CacheAttribute.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/AopCaching/CacheAttribute.cs b/AopCaching/CacheAttribute.cs index 148a7d0..27a1a3f 100644 --- a/AopCaching/CacheAttribute.cs +++ b/AopCaching/CacheAttribute.cs @@ -16,7 +16,6 @@ namespace PubComp.Caching.AopCaching public class CacheAttribute : MethodInterceptionAspect { private string cacheName; - private bool mustBeConfigured; private ICache cache; private string className; private string methodName; @@ -25,7 +24,9 @@ public class CacheAttribute : MethodInterceptionAspect private bool isClassGeneric; private bool isMethodGeneric; - public CacheAttribute() + private readonly bool mustBeConfigured; + + public CacheAttribute() : this(null, false) { } From 7b34d829f6e3353b88936dfcc5d21c291e0e7986 Mon Sep 17 00:00:00 2001 From: Noah Lerner Date: Mon, 4 Jul 2022 10:32:35 +0300 Subject: [PATCH 4/8] upgrade postsharp --- AopCaching/CacheAttribute.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/AopCaching/CacheAttribute.cs b/AopCaching/CacheAttribute.cs index 27a1a3f..bee5a22 100644 --- a/AopCaching/CacheAttribute.cs +++ b/AopCaching/CacheAttribute.cs @@ -6,13 +6,11 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Threading; using System.Threading.Tasks; -using PubComp.Caching.Core.Config.Loaders; namespace PubComp.Caching.AopCaching { - [Serializable] + //[Serializable] public class CacheAttribute : MethodInterceptionAspect { private string cacheName; From 9380d994acf4bdd9f36ee7f4982738c8b6bd7706 Mon Sep 17 00:00:00 2001 From: Noah Lerner Date: Mon, 4 Jul 2022 10:33:42 +0300 Subject: [PATCH 5/8] upgrade postsharp & change attribute to PSerializable --- AopCaching.UnitTests/AopCaching.UnitTests.csproj | 2 +- AopCaching/AopCaching.csproj | 2 +- AopCaching/CacheAttribute.cs | 6 +++--- Core.UnitTests/Core.UnitTests.csproj | 2 +- MongoDbCaching/MongoDbCaching.csproj | 2 +- RedisCaching.UnitTests/RedisCaching.UnitTests.csproj | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/AopCaching.UnitTests/AopCaching.UnitTests.csproj b/AopCaching.UnitTests/AopCaching.UnitTests.csproj index 4c70c2f..701ce53 100644 --- a/AopCaching.UnitTests/AopCaching.UnitTests.csproj +++ b/AopCaching.UnitTests/AopCaching.UnitTests.csproj @@ -7,7 +7,7 @@ - + diff --git a/AopCaching/AopCaching.csproj b/AopCaching/AopCaching.csproj index 3737ff8..1498161 100644 --- a/AopCaching/AopCaching.csproj +++ b/AopCaching/AopCaching.csproj @@ -17,7 +17,7 @@ - + diff --git a/AopCaching/CacheAttribute.cs b/AopCaching/CacheAttribute.cs index bee5a22..5ce62b2 100644 --- a/AopCaching/CacheAttribute.cs +++ b/AopCaching/CacheAttribute.cs @@ -1,5 +1,6 @@ using NLog; using PostSharp.Aspects; +using PostSharp.Serialization; using PubComp.Caching.Core; using PubComp.Caching.Core.Attributes; using System; @@ -10,7 +11,7 @@ namespace PubComp.Caching.AopCaching { - //[Serializable] + [PSerializable] public class CacheAttribute : MethodInterceptionAspect { private string cacheName; @@ -21,8 +22,7 @@ public class CacheAttribute : MethodInterceptionAspect private int[] indexesNotToCache; private bool isClassGeneric; private bool isMethodGeneric; - - private readonly bool mustBeConfigured; + private bool mustBeConfigured; public CacheAttribute() : this(null, false) { diff --git a/Core.UnitTests/Core.UnitTests.csproj b/Core.UnitTests/Core.UnitTests.csproj index ba73a92..b5dc284 100644 --- a/Core.UnitTests/Core.UnitTests.csproj +++ b/Core.UnitTests/Core.UnitTests.csproj @@ -16,7 +16,7 @@ - + diff --git a/MongoDbCaching/MongoDbCaching.csproj b/MongoDbCaching/MongoDbCaching.csproj index cc90209..73b946c 100644 --- a/MongoDbCaching/MongoDbCaching.csproj +++ b/MongoDbCaching/MongoDbCaching.csproj @@ -15,7 +15,7 @@ 1701;1702;1591 - + diff --git a/RedisCaching.UnitTests/RedisCaching.UnitTests.csproj b/RedisCaching.UnitTests/RedisCaching.UnitTests.csproj index 02ac7dc..d7271bc 100644 --- a/RedisCaching.UnitTests/RedisCaching.UnitTests.csproj +++ b/RedisCaching.UnitTests/RedisCaching.UnitTests.csproj @@ -7,7 +7,7 @@ - + From e9c9c7804908926e84c6f832e3cd65bbd13ed600 Mon Sep 17 00:00:00 2001 From: Noah Lerner Date: Mon, 4 Jul 2022 17:27:44 +0300 Subject: [PATCH 6/8] feature - init cache if missing --- AopCaching.UnitTests/AopCacheTests.cs | 16 +++++---- AopCaching.UnitTests/Mocks/Service2.cs | 13 ++++---- AopCaching/AopCaching.csproj | 1 + AopCaching/CacheAttribute.cs | 33 ++++++++++++------- WebApi/TestHost.WebApi.csproj | 19 +++++------ WebApi/packages.config | 8 ++--- .../WebApiExtended.NETCore.Swashbuckle.csproj | 8 ++--- .../WebApiExtended.NETCore.csproj | 8 ++--- 8 files changed, 59 insertions(+), 47 deletions(-) diff --git a/AopCaching.UnitTests/AopCacheTests.cs b/AopCaching.UnitTests/AopCacheTests.cs index d0e045e..a04b49d 100644 --- a/AopCaching.UnitTests/AopCacheTests.cs +++ b/AopCaching.UnitTests/AopCacheTests.cs @@ -72,19 +72,23 @@ public async Task TestMissingNameCacheAsync() } [TestMethod] - [ExpectedException(typeof(CacheException))] - public void TestMissingNameCache_MustBeConfigured() + public void TestMissingNameCache_InitIfMissing() { var service = new Service2(); - service.MethodToCache0Throws(); + var miss = service.MethodToCacheMissing(); + var hit = service.MethodToCacheMissing(); + Assert.AreEqual(miss, hit); + Assert.IsNotNull(CacheManager.GetCache("CacheMissing")); } [TestMethod] - [ExpectedException(typeof(CacheException))] - public async Task TestMissingNameCacheAsync_MustBeConfigured() + public async Task TestMissingNameCacheAsync_InitIfMissing() { var service = new Service2(); - await service.MethodToCache0ThrowsAsync(); + var miss = await service.MethodToCacheMissingAsync(); + var hit = await service.MethodToCacheMissingAsync(); + Assert.AreEqual(miss, hit); + Assert.IsNotNull(CacheManager.GetCache("CacheMissingAsync")); } [TestMethod] diff --git a/AopCaching.UnitTests/Mocks/Service2.cs b/AopCaching.UnitTests/Mocks/Service2.cs index aa555e3..61d1df0 100644 --- a/AopCaching.UnitTests/Mocks/Service2.cs +++ b/AopCaching.UnitTests/Mocks/Service2.cs @@ -8,6 +8,7 @@ namespace PubComp.Caching.AopCaching.UnitTests.Mocks public class Service2 { private int methodToCache0Counter; + private int methodToCache0NameMissing; [Cache("CacheMissing")] public int MethodToCache0() @@ -23,18 +24,16 @@ public async Task MethodToCache0Async() } [Cache("CacheMissing", true)] - public int MethodToCache0Throws() + public int MethodToCacheMissing() { - Assert.Fail($"This code should not be reached"); - return 0; + return ++methodToCache0NameMissing; } - [Cache("CacheMissing", true)] - public async Task MethodToCache0ThrowsAsync() + [Cache("CacheMissingAsync", true)] + public async Task MethodToCacheMissingAsync() { await Task.Delay(10); - Assert.Fail($"This code should not be reached"); - return 0; + return ++methodToCache0NameMissing; } [Cache("localCache")] diff --git a/AopCaching/AopCaching.csproj b/AopCaching/AopCaching.csproj index 1498161..68a3cfc 100644 --- a/AopCaching/AopCaching.csproj +++ b/AopCaching/AopCaching.csproj @@ -21,6 +21,7 @@ + diff --git a/AopCaching/CacheAttribute.cs b/AopCaching/CacheAttribute.cs index 5ce62b2..ac0ecbc 100644 --- a/AopCaching/CacheAttribute.cs +++ b/AopCaching/CacheAttribute.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Reflection; using System.Threading.Tasks; +using PubComp.Caching.SystemRuntime; namespace PubComp.Caching.AopCaching { @@ -22,7 +23,8 @@ public class CacheAttribute : MethodInterceptionAspect private int[] indexesNotToCache; private bool isClassGeneric; private bool isMethodGeneric; - private bool mustBeConfigured; + private bool initializeIfMissing; + private ILogger logger; public CacheAttribute() : this(null, false) { @@ -32,10 +34,11 @@ public CacheAttribute(string cacheName) : this(cacheName, false) { } - public CacheAttribute(string cacheName, bool mustBeConfigured) + public CacheAttribute(string cacheName, bool initializeIfMissing) { this.cacheName = cacheName; - this.mustBeConfigured = mustBeConfigured; + this.initializeIfMissing = initializeIfMissing; + this.logger = LogManager.GetCurrentClassLogger(); } public sealed override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo) @@ -75,10 +78,14 @@ public sealed override void OnInvoke(MethodInterceptionArgs args) this.cache = CacheManager.GetCache(this.cacheName); if (this.cache == null) { - if (this.mustBeConfigured) - throw new CacheException($"This cache has mustBeConfigured = true, so it must be configured."); - - LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!"); + if (this.initializeIfMissing) + { + this.cache = new InMemoryCache(this.cacheName, TimeSpan.FromDays(1)); + CacheManager.SetCache(this.cacheName, this.cache); + this.logger.Warn($"AOP cache [{this.cacheName}] is not initialized, initializing cache!"); + } + else + this.logger.Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!"); } } @@ -103,10 +110,14 @@ public sealed override async Task OnInvokeAsync(MethodInterceptionArgs args) this.cache = CacheManager.GetCache(this.cacheName); if (this.cache == null) { - if (this.mustBeConfigured) - throw new CacheException($"This cache has mustBeConfigured = true, so it must be configured."); - - LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!"); + if (this.initializeIfMissing) + { + this.cache = new InMemoryCache(this.cacheName, TimeSpan.FromDays(1)); + CacheManager.SetCache(this.cacheName, this.cache); + this.logger.Warn($"AOP cache [{this.cacheName}] is not initialized, initializing cache!"); + } + else + this.logger.Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!"); } } diff --git a/WebApi/TestHost.WebApi.csproj b/WebApi/TestHost.WebApi.csproj index f6687c6..5fe4945 100644 --- a/WebApi/TestHost.WebApi.csproj +++ b/WebApi/TestHost.WebApi.csproj @@ -1,5 +1,5 @@  - + @@ -81,8 +81,8 @@ ..\packages\Pipelines.Sockets.Unofficial.1.0.0\lib\netstandard2.0\Pipelines.Sockets.Unofficial.dll - - ..\packages\PostSharp.Redist.6.0.28\lib\net45\PostSharp.dll + + ..\packages\PostSharp.Redist.6.10.13\lib\net45\PostSharp.dll ..\packages\StackExchange.Redis.2.0.505\lib\net461\StackExchange.Redis.dll @@ -102,11 +102,8 @@ ..\packages\System.Diagnostics.PerformanceCounter.4.5.0\lib\net461\System.Diagnostics.PerformanceCounter.dll - - ..\packages\System.IO.Pipelines.4.5.0\lib\netstandard2.0\System.IO.Pipelines.dll - - ..\packages\System.Memory.4.5.0\lib\netstandard2.0\System.Memory.dll + ..\packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll @@ -144,7 +141,7 @@ ..\packages\System.Threading.Channels.4.5.0\lib\netstandard2.0\System.Threading.Channels.dll - ..\packages\System.Threading.Tasks.Extensions.4.5.0\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll + ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll @@ -306,11 +303,11 @@ - - + + - + - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -268,28 +251,16 @@ - + - + - - - - - - - - - - - - @@ -310,14 +281,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + - - - + + - - + + + + + + + + \ No newline at end of file diff --git a/WebApi/packages.config b/WebApi/packages.config index 871f79a..f85f5eb 100644 --- a/WebApi/packages.config +++ b/WebApi/packages.config @@ -1,20 +1,20 @@  - - - - - - - - - - - + + + + + + + + + + + - - + + @@ -22,26 +22,26 @@ - + - - + + - + - + diff --git a/WebApiExtended.NETCore.Swashbuckle/WebApiExtended.NETCore.Swashbuckle.csproj b/WebApiExtended.NETCore.Swashbuckle/WebApiExtended.NETCore.Swashbuckle.csproj index 14680e9..3ac4df6 100644 --- a/WebApiExtended.NETCore.Swashbuckle/WebApiExtended.NETCore.Swashbuckle.csproj +++ b/WebApiExtended.NETCore.Swashbuckle/WebApiExtended.NETCore.Swashbuckle.csproj @@ -18,8 +18,8 @@ 1701;1702;1591 - - + + diff --git a/WebApiExtended.NETCore/WebApiExtended.NETCore.csproj b/WebApiExtended.NETCore/WebApiExtended.NETCore.csproj index a3c0c04..ad29833 100644 --- a/WebApiExtended.NETCore/WebApiExtended.NETCore.csproj +++ b/WebApiExtended.NETCore/WebApiExtended.NETCore.csproj @@ -16,8 +16,8 @@ 1701;1702;1591 - - + + diff --git a/WebApiExtended.Swashbuckle/WebApiExtended.Swashbuckle.csproj b/WebApiExtended.Swashbuckle/WebApiExtended.Swashbuckle.csproj index 4b647cf..5d94236 100644 --- a/WebApiExtended.Swashbuckle/WebApiExtended.Swashbuckle.csproj +++ b/WebApiExtended.Swashbuckle/WebApiExtended.Swashbuckle.csproj @@ -18,9 +18,9 @@ 1701;1702;1591 - - - + + + diff --git a/WebApiExtended/WebApiExtended.csproj b/WebApiExtended/WebApiExtended.csproj index 32f5c45..c649a6e 100644 --- a/WebApiExtended/WebApiExtended.csproj +++ b/WebApiExtended/WebApiExtended.csproj @@ -16,8 +16,8 @@ 1701;1702;1591 - - + +