Skip to content

Commit 4b27550

Browse files
ybqwertyYagel
and
Yagel
authored
Replaced problematic CacheAttribute interlocked initialization (#46)
Also increased PostSharp version to support net5 Co-authored-by: Yagel <[email protected]>
1 parent 809ce73 commit 4b27550

File tree

7 files changed

+55
-13
lines changed

7 files changed

+55
-13
lines changed

AopCaching.UnitTests/AopCacheTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ public void TestCacheWithImplicitName()
5151
Assert.AreEqual(2, cache1.Misses);
5252
}
5353

54+
[TestMethod]
55+
public void TestMissingNameCache()
56+
{
57+
var service = new Service2();
58+
var result = service.MethodToCache0();
59+
Assert.AreEqual(1, result);
60+
result = service.MethodToCache0();
61+
Assert.AreEqual(2, result);
62+
}
63+
64+
[TestMethod]
65+
public async Task TestMissingNameCacheAsync()
66+
{
67+
var service = new Service2();
68+
var result = await service.MethodToCache0Async();
69+
Assert.AreEqual(1, result);
70+
result = await service.MethodToCache0Async();
71+
Assert.AreEqual(2, result);
72+
}
73+
5474
[TestMethod]
5575
public void TestNamedCache1()
5676
{

AopCaching.UnitTests/AopCaching.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
99
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
10-
<PackageReference Include="PostSharp" Version="6.0.28" />
10+
<PackageReference Include="PostSharp" Version="6.8.7" />
1111
<PackageReference Include="System.Runtime.Caching" Version="4.5.0" />
1212
</ItemGroup>
1313
<ItemGroup>

AopCaching.UnitTests/Mocks/Service2.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ namespace PubComp.Caching.AopCaching.UnitTests.Mocks
66
{
77
public class Service2
88
{
9+
private int methodToCache0Counter;
10+
11+
[Cache("CacheMissing")]
12+
public int MethodToCache0()
13+
{
14+
return ++methodToCache0Counter;
15+
}
16+
17+
[Cache("CacheMissing")]
18+
public async Task<int> MethodToCache0Async()
19+
{
20+
await Task.Delay(10);
21+
return ++methodToCache0Counter;
22+
}
23+
924
[Cache("localCache")]
1025
public IEnumerable<string> MethodToCache1()
1126
{

AopCaching/AopCaching.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<AssemblyName>PubComp.Caching.AopCaching</AssemblyName>
66
<RootNamespace>PubComp.Caching.AopCaching</RootNamespace>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
8-
<Version>5.0.0</Version>
8+
<Version>5.0.1</Version>
99
<AssemblyVersion>5.0.0.0</AssemblyVersion>
10-
<FileVersion>5.0.0.0</FileVersion>
10+
<FileVersion>5.0.1.0</FileVersion>
1111
</PropertyGroup>
1212
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
1313
<NoWarn>1701;1702;1591</NoWarn>
@@ -16,7 +16,8 @@
1616
<NoWarn>1701;1702;1591</NoWarn>
1717
</PropertyGroup>
1818
<ItemGroup>
19-
<PackageReference Include="PostSharp" Version="6.0.28" />
19+
<PackageReference Include="NLog" Version="4.5.10" />
20+
<PackageReference Include="PostSharp" Version="6.8.7" />
2021
</ItemGroup>
2122
<ItemGroup>
2223
<ProjectReference Include="..\Core\Core.csproj" />

AopCaching/CacheAttribute.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using PostSharp.Aspects;
1+
using NLog;
2+
using PostSharp.Aspects;
23
using PubComp.Caching.Core;
34
using PubComp.Caching.Core.Attributes;
45
using System;
@@ -15,7 +16,6 @@ public class CacheAttribute : MethodInterceptionAspect
1516
{
1617
private string cacheName;
1718
private ICache cache;
18-
private long initialized = 0L;
1919
private string className;
2020
private string methodName;
2121
private string[] parameterTypeNames;
@@ -32,7 +32,7 @@ public CacheAttribute(string cacheName)
3232
this.cacheName = cacheName;
3333
}
3434

35-
public sealed override void CompileTimeInitialize(System.Reflection.MethodBase method, AspectInfo aspectInfo)
35+
public sealed override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
3636
{
3737
var type = method.DeclaringType;
3838

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

6565
public sealed override void OnInvoke(MethodInterceptionArgs args)
6666
{
67-
if (Interlocked.Read(ref initialized) == 0L)
67+
if (this.cache == null)
6868
{
6969
this.cache = CacheManager.GetCache(this.cacheName);
70-
Interlocked.Exchange(ref initialized, 1L);
70+
if (this.cache == null)
71+
{
72+
LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!");
73+
}
7174
}
7275

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

95101
var cacheToUse = this.cache;

Core.UnitTests/Core.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
1717
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
1818
<PackageReference Include="NLog" Version="4.5.10" />
19-
<PackageReference Include="PostSharp" Version="6.0.28" />
19+
<PackageReference Include="PostSharp" Version="6.8.7" />
2020
<PackageReference Include="System.Runtime.Caching" Version="4.5.0" />
2121
</ItemGroup>
2222
<ItemGroup>

RedisCaching.UnitTests/RedisCaching.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
99
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
10-
<PackageReference Include="PostSharp" Version="6.0.28" />
10+
<PackageReference Include="PostSharp" Version="6.8.7" />
1111
</ItemGroup>
1212
<ItemGroup>
1313
<ProjectReference Include="..\AopCaching\AopCaching.csproj" />

0 commit comments

Comments
 (0)