Skip to content

Optionally create default InMemory cache if ICache not found #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions AopCaching.UnitTests/AopCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
6 changes: 3 additions & 3 deletions AopCaching.UnitTests/AopCaching.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<RootNamespace>PubComp.Caching.AopCaching.UnitTests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="PostSharp" Version="6.8.7" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="PostSharp" Version="6.10.13" />
<PackageReference Include="System.Runtime.Caching" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions AopCaching.UnitTests/Mocks/Service2.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -21,6 +23,19 @@ public async Task<int> MethodToCache0Async()
return ++methodToCache0Counter;
}

[Cache("CacheInitMissing", true)]
public int MethodToCacheMissing()
{
return ++methodToCache0NameMissing;
}

[Cache("CacheInitMissingAsync", true)]
public async Task<int> MethodToCacheMissingAsync()
{
await Task.Delay(10);
return ++methodToCache0NameMissing;
}

[Cache("localCache")]
public IEnumerable<string> MethodToCache1()
{
Expand Down
9 changes: 5 additions & 4 deletions AopCaching/AopCaching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<AssemblyName>PubComp.Caching.AopCaching</AssemblyName>
<RootNamespace>PubComp.Caching.AopCaching</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>5.0.2</Version>
<Version>5.0.3</Version>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<FileVersion>5.0.2.0</FileVersion>
<FileVersion>5.0.3.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;1591</NoWarn>
Expand All @@ -16,11 +16,12 @@
<NoWarn>1701;1702;1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.5.10" />
<PackageReference Include="PostSharp" Version="6.8.7" />
<PackageReference Include="NLog" Version="5.0.1" />
<PackageReference Include="PostSharp" Version="6.10.13" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\SystemRuntime\SystemRuntime.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="&quot;$(SolutionDir).NuGetPack\NuGetPack.exe&quot; &quot;$(ProjectPath)&quot; &quot;$(TargetPath)&quot; $(ConfigurationName)" />
Expand Down
33 changes: 27 additions & 6 deletions AopCaching/CacheAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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!");
}
}

Expand All @@ -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!");
}
}

Expand Down
8 changes: 4 additions & 4 deletions Core.UnitTests/Core.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="NLog" Version="4.5.10" />
<PackageReference Include="PostSharp" Version="6.8.7" />
<PackageReference Include="NLog" Version="5.0.1" />
<PackageReference Include="PostSharp" Version="6.10.13" />
<PackageReference Include="System.Runtime.Caching" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion DemoSynchronizedClient/DemoSynchronizedClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<RootNamespace>PubComp.Caching.DemoSynchronizedClient</RootNamespace>
</PropertyGroup>
<ItemGroup>
<packageReference Include="NLog" Version="4.5.10" />
<packageReference Include="NLog" Version="5.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
Expand Down
4 changes: 2 additions & 2 deletions MongoDbCaching.UnitTests/MongoDbCaching.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<RootNamespace>PubComp.Caching.MongoDbCaching.UnitTests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core.UnitTests\Core.UnitTests.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion MongoDbCaching/MongoDbCaching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<NoWarn>1701;1702;1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PubComp.NoSql.MongoDbDriver" Version="4.1.0" />
<PackageReference Include="PubComp.NoSql.MongoDbDriver" Version="4.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
Expand Down
6 changes: 3 additions & 3 deletions RedisCaching.UnitTests/RedisCaching.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<RootNamespace>PubComp.Caching.RedisCaching.UnitTests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="PostSharp" Version="6.8.7" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="PostSharp" Version="6.10.13" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AopCaching\AopCaching.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion RedisCaching/RedisCaching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.1" />
<PackageReference Include="NLog" Version="4.5.10" />
<PackageReference Include="NLog" Version="5.0.1" />
<PackageReference Include="StackExchange.Redis" Version="2.0.505" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions SystemRuntime.UnitTests/SystemRuntime.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<NoWarn>1701;1702;1998</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core.UnitTests\Core.UnitTests.csproj" />
Expand Down
Loading