Skip to content

Commit c9f8792

Browse files
committed
Updated README.md
1 parent 2af26bd commit c9f8792

1 file changed

Lines changed: 88 additions & 3 deletions

File tree

README.md

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,97 @@
11
# SpatialFocus.MethodCache.Fody
2+
3+
A method cache [Fody](https://github.com/Fody/Home/) plugin
4+
5+
[![Nuget](https://img.shields.io/nuget/v/SpatialFocus.MethodCache.Fody)](https://www.nuget.org/packages/SpatialFocus.MethodCache.Fody/)
6+
[![Build & Publish](https://github.com/SpatialFocus/MethodCache.Fody/workflows/Build%20&%20Publish/badge.svg)](https://github.com/SpatialFocus/MethodCache.Fody/actions)
27
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FSpatialFocus%2FMethodCache.Fody.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FSpatialFocus%2FMethodCache.Fody?ref=badge_shield)
38

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.
410

11+
## Usage
512

13+
See also [Fody usage](https://github.com/Fody/Home/blob/master/pages/usage.md).
614

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:
839

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+
```
1091

1192
## License
12-
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FSpatialFocus%2FMethodCache.Fody.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2FSpatialFocus%2FMethodCache.Fody?ref=badge_large)
93+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FSpatialFocus%2FMethodCache.Fody.svg?type=large)](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

Comments
 (0)