Skip to content

Replaced problematic CacheAttribute interlocked initialization #46

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

Merged
merged 1 commit into from
Jan 18, 2021
Merged
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 @@ -51,6 +51,26 @@ public void TestCacheWithImplicitName()
Assert.AreEqual(2, cache1.Misses);
}

[TestMethod]
public void TestMissingNameCache()
{
var service = new Service2();
var result = service.MethodToCache0();
Assert.AreEqual(1, result);
result = service.MethodToCache0();
Assert.AreEqual(2, result);
}

[TestMethod]
public async Task TestMissingNameCacheAsync()
{
var service = new Service2();
var result = await service.MethodToCache0Async();
Assert.AreEqual(1, result);
result = await service.MethodToCache0Async();
Assert.AreEqual(2, result);
}

[TestMethod]
public void TestNamedCache1()
{
Expand Down
2 changes: 1 addition & 1 deletion AopCaching.UnitTests/AopCaching.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="PostSharp" Version="6.0.28" />
<PackageReference Include="PostSharp" Version="6.8.7" />
<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
Expand Up @@ -6,6 +6,21 @@ namespace PubComp.Caching.AopCaching.UnitTests.Mocks
{
public class Service2
{
private int methodToCache0Counter;

[Cache("CacheMissing")]
public int MethodToCache0()
{
return ++methodToCache0Counter;
}

[Cache("CacheMissing")]
public async Task<int> MethodToCache0Async()
{
await Task.Delay(10);
return ++methodToCache0Counter;
}

[Cache("localCache")]
public IEnumerable<string> MethodToCache1()
{
Expand Down
7 changes: 4 additions & 3 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.0</Version>
<Version>5.0.1</Version>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<FileVersion>5.0.0.0</FileVersion>
<FileVersion>5.0.1.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;1591</NoWarn>
Expand All @@ -16,7 +16,8 @@
<NoWarn>1701;1702;1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PostSharp" Version="6.0.28" />
<PackageReference Include="NLog" Version="4.5.10" />
<PackageReference Include="PostSharp" Version="6.8.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
Expand Down
20 changes: 13 additions & 7 deletions AopCaching/CacheAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PostSharp.Aspects;
using NLog;
using PostSharp.Aspects;
using PubComp.Caching.Core;
using PubComp.Caching.Core.Attributes;
using System;
Expand All @@ -15,7 +16,6 @@ public class CacheAttribute : MethodInterceptionAspect
{
private string cacheName;
private ICache cache;
private long initialized = 0L;
private string className;
private string methodName;
private string[] parameterTypeNames;
Expand All @@ -32,7 +32,7 @@ public CacheAttribute(string cacheName)
this.cacheName = cacheName;
}

public sealed override void CompileTimeInitialize(System.Reflection.MethodBase method, AspectInfo aspectInfo)
public sealed override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
{
var type = method.DeclaringType;

Expand Down Expand Up @@ -64,10 +64,13 @@ public sealed override void CompileTimeInitialize(System.Reflection.MethodBase m

public sealed override void OnInvoke(MethodInterceptionArgs args)
{
if (Interlocked.Read(ref initialized) == 0L)
if (this.cache == null)
{
this.cache = CacheManager.GetCache(this.cacheName);
Interlocked.Exchange(ref initialized, 1L);
if (this.cache == null)
{
LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!");
}
}

var cacheToUse = this.cache;
Expand All @@ -86,10 +89,13 @@ public sealed override void OnInvoke(MethodInterceptionArgs args)
/// <inheritdoc />
public sealed override async Task OnInvokeAsync(MethodInterceptionArgs args)
{
if (Interlocked.Read(ref initialized) == 0L)
if (this.cache == null)
{
this.cache = CacheManager.GetCache(this.cacheName);
Interlocked.Exchange(ref initialized, 1L);
if (this.cache == null)
{
LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!");
}
}

var cacheToUse = this.cache;
Expand Down
2 changes: 1 addition & 1 deletion Core.UnitTests/Core.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="NLog" Version="4.5.10" />
<PackageReference Include="PostSharp" Version="6.0.28" />
<PackageReference Include="PostSharp" Version="6.8.7" />
<PackageReference Include="System.Runtime.Caching" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion RedisCaching.UnitTests/RedisCaching.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="PostSharp" Version="6.0.28" />
<PackageReference Include="PostSharp" Version="6.8.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AopCaching\AopCaching.csproj" />
Expand Down