A method cache Fody weaver which emulate PostSharp Caching API. Cache key builder and cache store are implementations so the weaving is much about fowarding the method context to them, allowing maximum flexibility. It handles Task, ValueTask, Stream and IEnumerable (with a IValueAdapter to be provided) cache values and have an aspnet DI API. See the test cover for specific examples. Default cache store implementation use .NET MemoryCache.
Anabasis.MethodCache.Fody
Anabasis.MethodCache.AspNet
See the samples for some idea of what the API provide.
Another go at method caching weaving using IMemoryCache => SpatialFocus.MethodCache.Fody
See also Fody usage.
PM> Install-Package Fody
PM> Install-Package Anabasis.MethodCache.Fody
Add <Anabasis.MethodCache/>
to FodyWeavers.xml
<Weavers>
<Anabasis.MethodCache/>
</Weavers>
Before weaving:
public class Sample
{
[Cache]
public string SampleTest(int a, string b)
{
return a + b;
}
}
After weaving:
public class Sample
{
[Cache]
public string SampleTest(int a, string b)
{
var cacheKey = CachingServices.KeyBuilder.CreateKey("SampleNamespace.Sample.SampleTest", new[] { "a", "b" }, new object[] { a, b });
if (CachingServices.Backend.TryGetValue<string>(cacheKey, out var cachedValue))
{
return cachedValue;
}
var result = a + b;
CachingServices.Backend.SetValue(cacheKey, result);
return result;
}
}