Skip to content

Commit 202c7e5

Browse files
authored
Merge pull request #47 from pub-comp/hotfix/aop-list-init
same change as in CacheAttribute init now in CacheListAttribute
2 parents 4b27550 + 1236bae commit 202c7e5

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

AopCaching.UnitTests/AopMultiCacheTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ public void TestInitialize()
2828
cache1.ClearAll();
2929
}
3030

31+
[TestMethod]
32+
public void TestMultiMissingCache()
33+
{
34+
var service = new MultiService();
35+
var results = service.GetItemsNoCache(new[] { "k1" });
36+
Assert.AreEqual("1", results.First().Value);
37+
results = service.GetItemsNoCache(new[] { "k1" });
38+
Assert.AreEqual("2", results.First().Value);
39+
}
40+
41+
[TestMethod]
42+
public async Task TestMultiMissingCacheAsync()
43+
{
44+
var service = new MultiService();
45+
var results = await service.GetItemsNoCacheAsync(new[] { "k1" });
46+
Assert.AreEqual("1", results.First().Value);
47+
results = await service.GetItemsNoCacheAsync(new[] { "k1" });
48+
Assert.AreEqual("2", results.First().Value);
49+
}
50+
3151
[TestMethod]
3252
public void TestMultiCacheWith1Items()
3353
{

AopCaching.UnitTests/Mocks/MultiService.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ namespace PubComp.Caching.AopCaching.UnitTests.Mocks
77
{
88
public class MultiService
99
{
10+
private int getItemsNoCacheCounter;
11+
12+
[CacheList("CacheMissing", typeof(MockDataKeyConverter))]
13+
public IList<MockData> GetItemsNoCache(IList<string> keys)
14+
{
15+
++getItemsNoCacheCounter;
16+
return new List<MockData> { new MockData { Id = keys.FirstOrDefault(), Value = getItemsNoCacheCounter.ToString() } };
17+
}
18+
19+
[CacheList("CacheMissing", typeof(MockDataKeyConverter))]
20+
public async Task<IList<MockData>> GetItemsNoCacheAsync(IList<string> keys)
21+
{
22+
++getItemsNoCacheCounter;
23+
await Task.Delay(10);
24+
return new List<MockData> { new MockData { Id = keys.FirstOrDefault(), Value = getItemsNoCacheCounter.ToString() } };
25+
}
26+
1027
[CacheList(typeof(MockDataKeyConverter))]
1128
public IList<MockData> GetItems(IList<string> keys)
1229
{

AopCaching/AopCaching.csproj

Lines changed: 2 additions & 2 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.1</Version>
8+
<Version>5.0.2</Version>
99
<AssemblyVersion>5.0.0.0</AssemblyVersion>
10-
<FileVersion>5.0.1.0</FileVersion>
10+
<FileVersion>5.0.2.0</FileVersion>
1111
</PropertyGroup>
1212
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
1313
<NoWarn>1701;1702;1591</NoWarn>

AopCaching/CacheListAttribute.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections;
99
using System.Threading.Tasks;
1010
using PubComp.Caching.Core.Attributes;
11+
using NLog;
1112

1213
namespace PubComp.Caching.AopCaching
1314
{
@@ -16,7 +17,6 @@ public class CacheListAttribute : MethodInterceptionAspect
1617
{
1718
private string cacheName;
1819
private ICache cache;
19-
private long initialized = 0L;
2020
private string className;
2121
private string methodName;
2222
private string[] parameterTypeNames;
@@ -206,10 +206,13 @@ public sealed override void CompileTimeInitialize(MethodBase method, AspectInfo
206206

207207
public sealed override void OnInvoke(MethodInterceptionArgs args)
208208
{
209-
if (Interlocked.Read(ref initialized) == 0L)
209+
if (this.cache == null)
210210
{
211211
this.cache = CacheManager.GetCache(this.cacheName);
212-
Interlocked.Exchange(ref initialized, 1L);
212+
if (this.cache == null)
213+
{
214+
LogManager.GetCurrentClassLogger().Warn($"AOP cache list [{this.cacheName}] is not initialized, define NoCache if needed!");
215+
}
213216
}
214217

215218
var cacheToUse = this.cache;
@@ -280,10 +283,13 @@ public sealed override void OnInvoke(MethodInterceptionArgs args)
280283

281284
public sealed override async Task OnInvokeAsync(MethodInterceptionArgs args)
282285
{
283-
if (Interlocked.Read(ref initialized) == 0L)
286+
if (this.cache == null)
284287
{
285288
this.cache = CacheManager.GetCache(this.cacheName);
286-
Interlocked.Exchange(ref initialized, 1L);
289+
if (this.cache == null)
290+
{
291+
LogManager.GetCurrentClassLogger().Warn($"AOP cache list [{this.cacheName}] is not initialized, define NoCache if needed!");
292+
}
287293
}
288294

289295
var cacheToUse = this.cache;

0 commit comments

Comments
 (0)