Skip to content

thomasraynal/Anabasis.MethodCache.Fody

Repository files navigation

Anabasis.MethodCache.Fody

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

NuGet

Anabasis.MethodCache.AspNet

NuGet

See the samples for some idea of what the API provide.

Another go at method caching weaving using IMemoryCache => SpatialFocus.MethodCache.Fody

Usage

See also Fody usage.

NuGet installation

PM> Install-Package Fody
PM> Install-Package Anabasis.MethodCache.Fody

Add to FodyWeavers.xml

Add <Anabasis.MethodCache/> to FodyWeavers.xml

<Weavers>
    <Anabasis.MethodCache/>
</Weavers>

Overview

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;

    }
}

About

Fody method cache weaver

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages