RSCG – cachesourcegenerator

RSCG – cachesourcegenerator
 
 

name cachesourcegenerator
nuget https://www.nuget.org/packages/cachesourcegenerator/
link https://github.com/jeppevammenkristensen/cachesourcegenerator
author Jeppe Roi Kristensen

Caching methods results

 

This is how you can use cachesourcegenerator .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CacheSourceGenerator" Version="0.4.1" />
    <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
  </ItemGroup>
	<ItemGroup>
		<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
	</ItemGroup>
 <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>
</Project>


The code that you will use is


using CacheDemo;
using Microsoft.Extensions.Caching.Memory;
var f=new FibTest(new MemoryCache(new MemoryCacheOptions()));
Console.WriteLine(f.FibMemo(5));
Console.WriteLine("and now with cache hit:");
Console.WriteLine(f.FibMemo(5));



using Microsoft.Extensions.Caching.Memory;
using CacheSourceGenerator;
namespace CacheDemo;
internal partial class FibTest
{
    private readonly IMemoryCache _memoryCache;

    public FibTest(IMemoryCache memoryCache)
    {
        _memoryCache = memoryCache;
    }
    void ProcessCacheEntry(ICacheEntry entry)
    {
        entry.SlidingExpiration = TimeSpan.FromMinutes(2);
    }

    [GenerateMemoryCache(MethodName = "FibMemo", CacheEnricherProcessor = nameof(ProcessCacheEntry))]
    public int Fib(int n)
    {
        if (n <= 1)
        {
            return n;
        }
        Console.WriteLine($"Calculating Fib({n})");
        //return Fib(n - 1) + Fib(n - 2);
        return FibMemo(n - 1) + FibMemo(n - 2);
    }
}

 

The code that is generated is

#nullable enable
//autogenerated
using Microsoft.Extensions.Caching.Memory;
using CacheSourceGenerator;
using System;

namespace CacheDemo;
internal partial class FibTest
{
    public int FibMemo(int n)
    {
        var _key_ = new
        {
            _MethodName = "Fib",
            _ClassName = "FibTest",
            n
        };
        IMemoryCache _cache_ = _memoryCache;
        var _result_ = _cache_.GetOrCreate(_key_, _entry_ =>
        {
            ProcessCacheEntry(_entry_);
            OnCallingFib(n);
            var _callResult_ = Fib(n);
            OnCalledFib(n, _callResult_);
            ;
            return _callResult_;
        });
        return _result_;
    }

    public void FibMemo_Evict(int n)
    {
        var _key_ = new
        {
            _MethodName = "Fib",
            _ClassName = "FibTest",
            n
        };
        IMemoryCache _cache_ = _memoryCache;
        _cache_.Remove(_key_);
    }

    partial void OnCallingFib(int n);
    partial void OnCalledFib(int n, int _returned_);
}

// <auto-generated/>
#nullable enable
namespace CacheSourceGenerator
{
    [System.AttributeUsage(System.AttributeTargets.Method)]
    public class GenerateMemoryCacheAttribute : System.Attribute
    {
       /// <summary>
       /// The name of the generated cache method 
       /// </summary>
       public string MethodName { get;init; } = default!;
       
       /// <summary>
       /// The name of a method in the current class that takes
       /// an CacheEntry and processes it 
       /// </summary>
       public string? CacheEnricherProcessor { get;set; }
       
       
       /// <summary>
        /// The name of a method in the current class that can 
        /// generate a custom cache key. The method must take the same parameters
        /// as the method being decorated. But can return any type.
        /// </summary>
       public string? KeyGenerator {get;set;}
       
       /// <summary>
       /// Set this to true to not generate an evict method
       /// </summary>
       public bool SuppressEvictMethod {get;set;}
    }
}
// <auto-generated/>
#nullable enable
namespace CacheSourceGenerator
{
    [System.AttributeUsage(System.AttributeTargets.Parameter)]
    public class IgnoreKeyAttribute : System.Attribute
    {
        
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/cachesourcegenerator