|
1 | 1 | # SpatialFocus.MethodCache.Fody |
| 2 | + |
| 3 | +A method cache [Fody](https://github.com/Fody/Home/) plugin |
| 4 | + |
| 5 | +[](https://www.nuget.org/packages/SpatialFocus.MethodCache.Fody/) |
| 6 | +[](https://github.com/SpatialFocus/MethodCache.Fody/actions) |
2 | 7 | [](https://app.fossa.com/projects/git%2Bgithub.com%2FSpatialFocus%2FMethodCache.Fody?ref=badge_shield) |
3 | 8 |
|
| 9 | +Caches return values of methods decorated with a `[Cache]` Attribute. Integrates with the .NET Extension [IMemoryCache](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.caching.memory.imemorycache) interface. |
4 | 10 |
|
| 11 | +## Usage |
5 | 12 |
|
| 13 | +See also [Fody usage](https://github.com/Fody/Home/blob/master/pages/usage.md). |
6 | 14 |
|
7 | | ----- |
| 15 | +### NuGet installation |
| 16 | + |
| 17 | +Install the [SpatialFocus.MethodCache.Fody NuGet package](https://nuget.org/packages/SpatialFocus.MethodCache.Fody/) and update the [Fody NuGet package](https://nuget.org/packages/Fody/): |
| 18 | + |
| 19 | +```powershell |
| 20 | +PM> Install-Package Fody |
| 21 | +PM> Install-Package SpatialFocus.MethodCache.Fody |
| 22 | +``` |
| 23 | + |
| 24 | +The `Install-Package Fody` is required since NuGet always defaults to the oldest, and most buggy, version of any dependency. |
| 25 | + |
| 26 | +### Add to FodyWeavers.xml |
| 27 | + |
| 28 | +Add `<SpatialFocus.MethodCache/>` to [FodyWeavers.xml](https://github.com/Fody/Home/blob/master/pages/usage.md#add-fodyweaversxml) |
| 29 | + |
| 30 | +```xml |
| 31 | +<Weavers> |
| 32 | + <SpatialFocus.MethodCache/> |
| 33 | +</Weavers> |
| 34 | +``` |
| 35 | + |
| 36 | +## Overview |
| 37 | + |
| 38 | +Before code: |
8 | 39 |
|
9 | | -Made with :heart: by [Spatial Focus](https://spatial-focus.net/) |
| 40 | +```csharp |
| 41 | +[Cache] |
| 42 | +public class BasicSample |
| 43 | +{ |
| 44 | + public BasicSample(IMemoryCache memoryCache) |
| 45 | + { |
| 46 | + MemoryCache = memoryCache; |
| 47 | + } |
| 48 | + |
| 49 | + // MethodCache.Fody will look for a property implementing the Microsoft.Extensions.Caching.Memory.IMemoryCache interface |
| 50 | + protected IMemoryCache MemoryCache { get; } |
| 51 | + |
| 52 | + public int Add(int a, int b) |
| 53 | + { |
| 54 | + return a + b; |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +What gets compiled |
| 60 | + |
| 61 | +```csharp |
| 62 | +[Cache] |
| 63 | +public class BasicSample |
| 64 | +{ |
| 65 | + public BasicSample(IMemoryCache memoryCache) |
| 66 | + { |
| 67 | + MemoryCache = memoryCache; |
| 68 | + } |
| 69 | + |
| 70 | + protected IMemoryCache MemoryCache { get; } |
| 71 | + |
| 72 | + public int Add(int a, int b) |
| 73 | + { |
| 74 | + // Create a unique cache key, based on namespace, class name and method name as first parameter and corresponding |
| 75 | + // generic class parameters, generic method parameters and method parameters |
| 76 | + Tuple<string, int, int> key = new Tuple<string, int, int>("Namespace.BasicSample.Add", a, b); |
| 77 | + |
| 78 | + // Check and return if a cached value exists for key |
| 79 | + if (MemoryCache.TryGetValue(key, out int value)) |
| 80 | + { |
| 81 | + return value; |
| 82 | + } |
| 83 | + |
| 84 | + // Before each return statement, save the value that would be returned in the cache |
| 85 | + value = a + b; |
| 86 | + MemoryCache.Set<int>(key, value); |
| 87 | + return value; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
10 | 91 |
|
11 | 92 | ## License |
12 | | -[](https://app.fossa.com/projects/git%2Bgithub.com%2FSpatialFocus%2FMethodCache.Fody?ref=badge_large) |
| 93 | +[](https://app.fossa.com/projects/git%2Bgithub.com%2FSpatialFocus%2FMethodCache.Fody?ref=badge_large) |
| 94 | + |
| 95 | +---- |
| 96 | + |
| 97 | +Made with :heart: by [Spatial Focus](https://spatial-focus.net/) |
0 commit comments