Skip to content

Commit 906738c

Browse files
committed
Added NoCache attribute, closes #1
1 parent 945d832 commit 906738c

File tree

8 files changed

+92
-5
lines changed

8 files changed

+92
-5
lines changed

src/SpatialFocus.MethodCache.Fody/Extensions/MethodDefinitionExtension.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ public static bool HasCacheAttribute(this MethodDefinition methodDefinition, Ref
2828
classAttribute => classAttribute.AttributeType.Resolve().Equals(cacheAttributeType));
2929
}
3030

31+
public static bool HasNoCacheAttribute(this MethodDefinition methodDefinition, References references)
32+
{
33+
if (methodDefinition == null)
34+
{
35+
throw new ArgumentNullException(nameof(methodDefinition));
36+
}
37+
38+
if (references == null)
39+
{
40+
throw new ArgumentNullException(nameof(references));
41+
}
42+
43+
TypeReference noCacheAttributeType = references.NoCacheAttributeType.Resolve();
44+
45+
return methodDefinition.CustomAttributes.Any(classAttribute =>
46+
classAttribute.AttributeType.Resolve().Equals(noCacheAttributeType));
47+
}
48+
3149
public static bool IsEligibleForWeaving(this MethodDefinition methodDefinition, References references)
3250
{
3351
if (methodDefinition == null)

src/SpatialFocus.MethodCache.Fody/ModuleWeaver.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@ namespace SpatialFocus.MethodCache.Fody
1212

1313
public partial class ModuleWeaver : BaseModuleWeaver
1414
{
15-
public override bool ShouldCleanReference => true;
15+
public override bool ShouldCleanReference => false;
1616

1717
public override void Execute()
1818
{
1919
References references = Fody.References.Init(this);
2020

2121
foreach (WeavingCandidate weavingCandidate in ModuleDefinition.GetWeavingCandidates(references))
2222
{
23-
if (weavingCandidate.ClassDefinition.HasCacheAttribute(references) && !weavingCandidate.MethodDefinitions.Any(x => x.IsEligibleForWeaving(references)))
23+
if (weavingCandidate.ClassDefinition.HasCacheAttribute(references) && !weavingCandidate.MethodDefinitions
24+
.Where(x => !x.HasNoCacheAttribute(references))
25+
.Any(x => x.IsEligibleForWeaving(references)))
2426
{
25-
WriteWarning($"Class {weavingCandidate.ClassDefinition.Resolve().FullName} contains [Cache] attribute but does not contain eligible methods for caching");
27+
WriteWarning(
28+
$"Class {weavingCandidate.ClassDefinition.Resolve().FullName} contains [Cache] attribute but does not contain eligible methods for caching");
2629
continue;
2730
}
2831

2932
if (!weavingCandidate.ClassDefinition.IsEligibleForWeaving(references))
3033
{
31-
WriteWarning($"Class {weavingCandidate.ClassDefinition.Name} contains [Cache] attribute but does not contain a single non-inherited property implementing IMemoryCache interface");
34+
WriteWarning(
35+
$"Class {weavingCandidate.ClassDefinition.Name} contains [Cache] attribute but does not contain a single non-inherited property implementing IMemoryCache interface");
3236
continue;
3337
}
3438

@@ -49,6 +53,11 @@ public override void Execute()
4953
continue;
5054
}
5155

56+
if (methodDefinition.HasNoCacheAttribute(references))
57+
{
58+
continue;
59+
}
60+
5261
MethodWeavingContext methodWeavingContext = new MethodWeavingContext(classWeavingContext, methodDefinition);
5362

5463
MemoryCache.AddMethodVariables(methodWeavingContext);

src/SpatialFocus.MethodCache.Fody/References.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ protected References(ModuleWeaver moduleWeaver)
2929

3030
public TypeReference MemoryCacheInterface { get; protected set; }
3131

32+
public TypeReference NoCacheAttributeType { get; set; }
33+
3234
public MethodReference SetMethod { get; protected set; }
3335

3436
public MethodReference TryGetValueMethod { get; protected set; }
@@ -49,6 +51,9 @@ public static References Init(ModuleWeaver moduleWeaver)
4951
TypeDefinition cacheAttributeType = moduleWeaver.FindTypeDefinition("SpatialFocus.MethodCache.CacheAttribute");
5052
references.CacheAttributeType = moduleWeaver.ModuleDefinition.ImportReference(cacheAttributeType);
5153

54+
TypeDefinition noCacheAttributeType = moduleWeaver.FindTypeDefinition("SpatialFocus.MethodCache.NoCacheAttribute");
55+
references.NoCacheAttributeType = moduleWeaver.ModuleDefinition.ImportReference(noCacheAttributeType);
56+
5257
TypeDefinition type = moduleWeaver.FindTypeDefinition(typeof(Type).FullName);
5358
references.TypeType = moduleWeaver.ModuleDefinition.ImportReference(type);
5459

@@ -67,7 +72,9 @@ public static References Init(ModuleWeaver moduleWeaver)
6772
references.TryGetValueMethod =
6873
moduleWeaver.ModuleDefinition.ImportReference(cacheExtensions.Methods.Single(x => x.Name == "TryGetValue"));
6974

70-
references.SetMethod = moduleWeaver.ModuleDefinition.ImportReference(cacheExtensions.Methods.Single(x => x.Name == "Set" && x.HasParameters && x.Parameters.Count == 3));
75+
references.SetMethod =
76+
moduleWeaver.ModuleDefinition.ImportReference(cacheExtensions.Methods.Single(x =>
77+
x.Name == "Set" && x.HasParameters && x.Parameters.Count == 3));
7178

7279
return references;
7380
}

src/SpatialFocus.MethodCache.Sample.Library/BasicSample.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SpatialFocus.MethodCache.Sample.Library
66
{
7+
using System;
78
using Microsoft.Extensions.Caching.Memory;
89

910
// MethodCache.Fody will look for classes and methods decorated with the Cache attribute
@@ -18,11 +19,19 @@ public BasicSample(IMemoryCache memoryCache)
1819
// MethodCache.Fody will look for a property implementing the IMemoryCache
1920
protected IMemoryCache MemoryCache { get; }
2021

22+
#pragma warning disable CA1062 // Validate arguments of public methods
2123
#pragma warning disable CA1822 // Mark members as static
2224
public int Add(int a, int b)
2325
{
2426
return a + b;
2527
}
28+
29+
[NoCache]
30+
public int GetRandomNumber(Random random)
31+
{
32+
return random.Next();
33+
}
34+
#pragma warning restore CA1062 // Validate arguments of public methods
2635
#pragma warning restore CA1822 // Mark members as static
2736
}
2837
}

src/SpatialFocus.MethodCache.Sample.Library/BasicSampleWeaved.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ public int Add(int a, int b)
3333
MemoryCache.Set<int>(key, value);
3434
return value;
3535
}
36+
37+
public int GetRandomNumber(Random random)
38+
{
39+
// The NoCache attribute was specified for this method, hence no additional code weaved
40+
return random.Next();
41+
}
3642
}
3743
}

src/SpatialFocus.MethodCache.TestAssembly/BasicTestClass.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public int Add(int a, int b)
2121
{
2222
return a + b;
2323
}
24+
25+
[NoCache]
26+
public int UncachedAdd(int a, int b)
27+
{
28+
return a + b;
29+
}
2430
#pragma warning restore CA1822 // Mark members as static
2531
}
2632
}

src/SpatialFocus.MethodCache.Tests/MemoryCacheBasicTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,21 @@ public void BasicTest3Create2AndGet2()
6767
Assert.Equal(2, mockMemoryCache.CountSets);
6868
Assert.Equal(2, mockMemoryCache.CountGets);
6969
}
70+
71+
[Fact]
72+
public void BasicTest4NoCache()
73+
{
74+
using MockMemoryCache mockMemoryCache = new MockMemoryCache();
75+
76+
dynamic instance = TestHelpers.CreateInstance<BasicTestClass>(MemoryCacheBasicTests.TestResult.Assembly, mockMemoryCache);
77+
78+
dynamic result1 = instance.UncachedAdd(1, 2);
79+
dynamic result2 = instance.UncachedAdd(2, 2);
80+
81+
Assert.Equal(3, result1);
82+
Assert.Equal(4, result2);
83+
Assert.Equal(0, mockMemoryCache.CountSets);
84+
Assert.Equal(0, mockMemoryCache.CountGets);
85+
}
7086
}
7187
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// <copyright file="NoCacheAttribute.cs" company="Spatial Focus GmbH">
2+
// Copyright (c) Spatial Focus GmbH. All rights reserved.
3+
// </copyright>
4+
5+
namespace SpatialFocus.MethodCache
6+
{
7+
using System;
8+
9+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
10+
public sealed class NoCacheAttribute : Attribute
11+
{
12+
public NoCacheAttribute()
13+
{
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)