Injects method cache code.
This is an add-in for Fody
This project was forked by MethodCache original. And updated to support DotNet Core.
- Support dotnet core
- Support instance of class cache
- Support static of method cache
- Support property of class cache
- Support complex parameters of method cache
- Support duration control
[Cache(Duration = 3600)]
public int Add(int a, int b)
{
return a + b;
}
public int Add(int a, int b)
{
string cacheKey = string.Format("Namespace.Class.Add_{0}_{1}", new object[] { a, b });
if(Cache.Contains(cacheKey))
{
return Cache.Retrieve<int>(cacheKey);
}
int result = a + b;
Cache.Store(cacheKey, result, new Dictionary<string, object>() { {"Duration", 3600}} );
return result;
}
See also Fody usage.
Install the Cache.Fody NuGet package and update the Fody NuGet package:
PM> Install-Package Cache.Fody
PM> Update-Package Fody
The Update-Package Fody
is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.
Add <Cache/>
to FodyWeavers.xml
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
<Cache/>
</Weavers>
In addition to the actual weaving assembly the NuGet package will also add a file CacheAttribute.cs
to the target project.
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Constructor,AllowMultiple = false)]
class CacheAttribute : Attribute
{
}
See also Sample Site.
Icon courtesy of The Noun Project