diff --git a/AopCaching.UnitTests/AopCacheTests.cs b/AopCaching.UnitTests/AopCacheTests.cs index b71c4f3..f38dd01 100644 --- a/AopCaching.UnitTests/AopCacheTests.cs +++ b/AopCaching.UnitTests/AopCacheTests.cs @@ -71,6 +71,26 @@ public async Task TestMissingNameCacheAsync() Assert.AreEqual(2, result); } + [TestMethod] + public void TestMissingNameCache_InitIfMissing() + { + var service = new Service2(); + var miss = service.MethodToCacheMissing(); + var hit = service.MethodToCacheMissing(); + Assert.AreEqual(miss, hit); + Assert.IsNotNull(CacheManager.GetCache("CacheInitMissing")); + } + + [TestMethod] + public async Task TestMissingNameCacheAsync_InitIfMissing() + { + var service = new Service2(); + var miss = await service.MethodToCacheMissingAsync(); + var hit = await service.MethodToCacheMissingAsync(); + Assert.AreEqual(miss, hit); + Assert.IsNotNull(CacheManager.GetCache("CacheInitMissingAsync")); + } + [TestMethod] public void TestNamedCache1() { diff --git a/AopCaching.UnitTests/AopCaching.UnitTests.csproj b/AopCaching.UnitTests/AopCaching.UnitTests.csproj index 4c70c2f..15c875c 100644 --- a/AopCaching.UnitTests/AopCaching.UnitTests.csproj +++ b/AopCaching.UnitTests/AopCaching.UnitTests.csproj @@ -5,9 +5,9 @@ PubComp.Caching.AopCaching.UnitTests - - - + + + diff --git a/AopCaching.UnitTests/Mocks/Service2.cs b/AopCaching.UnitTests/Mocks/Service2.cs index 6c8343d..0ab12d5 100644 --- a/AopCaching.UnitTests/Mocks/Service2.cs +++ b/AopCaching.UnitTests/Mocks/Service2.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace PubComp.Caching.AopCaching.UnitTests.Mocks { public class Service2 { private int methodToCache0Counter; + private int methodToCache0NameMissing; [Cache("CacheMissing")] public int MethodToCache0() @@ -21,6 +23,19 @@ public async Task MethodToCache0Async() return ++methodToCache0Counter; } + [Cache("CacheInitMissing", true)] + public int MethodToCacheMissing() + { + return ++methodToCache0NameMissing; + } + + [Cache("CacheInitMissingAsync", true)] + public async Task MethodToCacheMissingAsync() + { + await Task.Delay(10); + return ++methodToCache0NameMissing; + } + [Cache("localCache")] public IEnumerable MethodToCache1() { diff --git a/AopCaching/AopCaching.csproj b/AopCaching/AopCaching.csproj index 25db13f..aa4cc25 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 @@ -16,11 +16,12 @@ 1701;1702;1591 - - + + + diff --git a/AopCaching/CacheAttribute.cs b/AopCaching/CacheAttribute.cs index dd30db6..26512c2 100644 --- a/AopCaching/CacheAttribute.cs +++ b/AopCaching/CacheAttribute.cs @@ -1,17 +1,18 @@ using NLog; using PostSharp.Aspects; +using PostSharp.Serialization; using PubComp.Caching.Core; using PubComp.Caching.Core.Attributes; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Threading; using System.Threading.Tasks; +using PubComp.Caching.SystemRuntime; namespace PubComp.Caching.AopCaching { - [Serializable] + [PSerializable] public class CacheAttribute : MethodInterceptionAspect { private string cacheName; @@ -22,14 +23,20 @@ public class CacheAttribute : MethodInterceptionAspect private int[] indexesNotToCache; private bool isClassGeneric; private bool isMethodGeneric; + private bool initializeIfMissing; - public CacheAttribute() + public CacheAttribute() : this(null, false) { } - public CacheAttribute(string cacheName) + public CacheAttribute(string cacheName) : this(cacheName, false) + { + } + + public CacheAttribute(string cacheName, bool initializeIfMissing) { this.cacheName = cacheName; + this.initializeIfMissing = initializeIfMissing; } public sealed override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo) @@ -69,7 +76,14 @@ public sealed override void OnInvoke(MethodInterceptionArgs args) this.cache = CacheManager.GetCache(this.cacheName); if (this.cache == null) { - 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); + LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, initializing cache!"); + } + else + LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!"); } } @@ -94,7 +108,14 @@ public sealed override async Task OnInvokeAsync(MethodInterceptionArgs args) this.cache = CacheManager.GetCache(this.cacheName); if (this.cache == null) { - 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); + LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, initializing cache!"); + } + else + LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!"); } } diff --git a/Core.UnitTests/Core.UnitTests.csproj b/Core.UnitTests/Core.UnitTests.csproj index ba73a92..cb002f9 100644 --- a/Core.UnitTests/Core.UnitTests.csproj +++ b/Core.UnitTests/Core.UnitTests.csproj @@ -12,11 +12,11 @@ - - + + - - + + diff --git a/DemoSynchronizedClient/DemoSynchronizedClient.csproj b/DemoSynchronizedClient/DemoSynchronizedClient.csproj index 7f7e313..571abe2 100644 --- a/DemoSynchronizedClient/DemoSynchronizedClient.csproj +++ b/DemoSynchronizedClient/DemoSynchronizedClient.csproj @@ -6,7 +6,7 @@ PubComp.Caching.DemoSynchronizedClient - + diff --git a/MongoDbCaching.UnitTests/MongoDbCaching.UnitTests.csproj b/MongoDbCaching.UnitTests/MongoDbCaching.UnitTests.csproj index 1dcedce..cd6d0cb 100644 --- a/MongoDbCaching.UnitTests/MongoDbCaching.UnitTests.csproj +++ b/MongoDbCaching.UnitTests/MongoDbCaching.UnitTests.csproj @@ -5,8 +5,8 @@ PubComp.Caching.MongoDbCaching.UnitTests - - + + 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..2b269d7 100644 --- a/RedisCaching.UnitTests/RedisCaching.UnitTests.csproj +++ b/RedisCaching.UnitTests/RedisCaching.UnitTests.csproj @@ -5,9 +5,9 @@ PubComp.Caching.RedisCaching.UnitTests - - - + + + diff --git a/RedisCaching/RedisCaching.csproj b/RedisCaching/RedisCaching.csproj index 5704142..d7b2554 100644 --- a/RedisCaching/RedisCaching.csproj +++ b/RedisCaching/RedisCaching.csproj @@ -17,7 +17,7 @@ - + diff --git a/SystemRuntime.UnitTests/SystemRuntime.UnitTests.csproj b/SystemRuntime.UnitTests/SystemRuntime.UnitTests.csproj index b01653a..7a20a9b 100644 --- a/SystemRuntime.UnitTests/SystemRuntime.UnitTests.csproj +++ b/SystemRuntime.UnitTests/SystemRuntime.UnitTests.csproj @@ -11,9 +11,9 @@ 1701;1702;1998 - - - + + + diff --git a/WebApi/TestHost.WebApi.csproj b/WebApi/TestHost.WebApi.csproj index f6687c6..06a89b8 100644 --- a/WebApi/TestHost.WebApi.csproj +++ b/WebApi/TestHost.WebApi.csproj @@ -1,6 +1,5 @@  - - + @@ -50,11 +49,11 @@ - - ..\packages\Unity.5.5.5\lib\net45\CommonServiceLocator.dll + + ..\packages\CommonServiceLocator.2.0.6\lib\net46\CommonServiceLocator.dll - - ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll + + ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll @@ -72,8 +71,8 @@ ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll - - ..\packages\NLog.4.5.10\lib\net45\NLog.dll + + ..\packages\NLog.5.0.1\lib\net46\NLog.dll ..\packages\Owin.1.0\lib\net40\Owin.dll @@ -81,17 +80,17 @@ ..\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 - ..\packages\Swashbuckle.Core.5.5.3\lib\net40\Swashbuckle.Core.dll + ..\packages\Swashbuckle.Core.5.6.0\lib\net40\Swashbuckle.Core.dll - ..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll + ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll @@ -102,19 +101,16 @@ ..\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 ..\packages\Microsoft.Net.Http.2.2.29\lib\net45\System.Net.Http.Extensions.dll - - ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.9\lib\net45\System.Net.Http.Formatting.dll ..\packages\Microsoft.Net.Http.2.2.29\lib\net45\System.Net.Http.Primitives.dll @@ -125,8 +121,8 @@ ..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll - - ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll @@ -143,12 +139,12 @@ ..\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.4\lib\net461\System.Threading.Tasks.Extensions.dll - - ..\packages\Microsoft.AspNet.Cors.5.2.3\lib\net45\System.Web.Cors.dll + + ..\packages\Microsoft.AspNet.Cors.5.2.9\lib\net45\System.Web.Cors.dll @@ -159,34 +155,34 @@ - ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll + ..\packages\Microsoft.AspNet.WebPages.3.2.9\lib\net45\System.Web.Helpers.dll - - ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll + + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.9\lib\net45\System.Web.Http.dll - - ..\packages\Microsoft.AspNet.WebApi.Cors.5.2.3\lib\net45\System.Web.Http.Cors.dll + + ..\packages\Microsoft.AspNet.WebApi.Cors.5.2.9\lib\net45\System.Web.Http.Cors.dll - - ..\packages\Microsoft.AspNet.WebApi.Owin.5.2.3\lib\net45\System.Web.Http.Owin.dll + + ..\packages\Microsoft.AspNet.WebApi.Owin.5.2.9\lib\net45\System.Web.Http.Owin.dll - - ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll + + ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.9\lib\net45\System.Web.Http.WebHost.dll - - ..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll + + ..\packages\Microsoft.AspNet.Mvc.5.2.9\lib\net45\System.Web.Mvc.dll - ..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll + ..\packages\Microsoft.AspNet.Razor.3.2.9\lib\net45\System.Web.Razor.dll - ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll + ..\packages\Microsoft.AspNet.WebPages.3.2.9\lib\net45\System.Web.WebPages.dll - ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll + ..\packages\Microsoft.AspNet.WebPages.3.2.9\lib\net45\System.Web.WebPages.Deployment.dll - ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll + ..\packages\Microsoft.AspNet.WebPages.3.2.9\lib\net45\System.Web.WebPages.Razor.dll @@ -305,12 +301,13 @@ - - - + + + - + + - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -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 eaa1fd5..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 dea25cd..3ac4df6 100644 --- a/WebApiExtended.NETCore.Swashbuckle/WebApiExtended.NETCore.Swashbuckle.csproj +++ b/WebApiExtended.NETCore.Swashbuckle/WebApiExtended.NETCore.Swashbuckle.csproj @@ -1,13 +1,13 @@  - netcoreapp2.1 + netcoreapp3.1 true PubComp.Caching.WebApiExtended.NETCore.Swashbuckle PubComp.Caching.WebApiExtended.Net.Core.Swashbuckle true - 5.0.0 - 5.0.0.0 - 5.0.0.0 + 6.0.0 + 6.0.0.0 + 6.0.0.0 true true @@ -18,8 +18,8 @@ 1701;1702;1591 - - + + diff --git a/WebApiExtended.NETCore/WebApiExtended.NETCore.csproj b/WebApiExtended.NETCore/WebApiExtended.NETCore.csproj index 4177ffa..ad29833 100644 --- a/WebApiExtended.NETCore/WebApiExtended.NETCore.csproj +++ b/WebApiExtended.NETCore/WebApiExtended.NETCore.csproj @@ -1,13 +1,13 @@  - netcoreapp2.1 + netcoreapp3.1 true PubComp.Caching.WebApiExtended.NETCore PubComp.Caching.WebApiExtended.Net.Core true - 5.0.0 - 5.0.0.0 - 5.0.0.0 + 6.0.0 + 6.0.0.0 + 6.0.0.0 1701;1702;1591 @@ -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 - - + +