github の問題で既に述べたように、静的メソッド キャッシュは 1.3.1 で追加されました。
MethodCache.Fody が設計されているため、キャッシュされるべきメソッドを含む Cache Getter をクラスに追加し、キャッシュを実装する必要もあります。独自のキャッシュをプログラムするか、既存のキャッシュ ソリューションへのアダプターを使用できます ( https://github.com/Dresel/MethodCacheのドキュメントを参照)。
サンプルの最小コード (基本的なディクショナリ キャッシュの実装を含む) は次のようになります。
namespace ConsoleApplication
{
using System;
using System.Collections.Generic;
using System.Threading;
using MethodCache.Attributes;
public class Program
{
private static DictionaryCache Cache { get; set; }
[Cache]
private static int Calc(int b)
{
Thread.Sleep(5000);
return b + 5;
}
private static void Main(string[] args)
{
Cache = new DictionaryCache();
Console.WriteLine("Begin calc 1...");
var v = Calc(5);
// Will return the cached value
Console.WriteLine("Begin calc 2...");
v = Calc(5);
Console.WriteLine("end calc 2...");
}
}
public class DictionaryCache
{
public DictionaryCache()
{
Storage = new Dictionary<string, object>();
}
private Dictionary<string, object> Storage { get; set; }
// Note: The methods Contains, Retrieve, Store must exactly look like the following:
public bool Contains(string key)
{
return Storage.ContainsKey(key);
}
public T Retrieve<T>(string key)
{
return (T)Storage[key];
}
public void Store(string key, object data)
{
Storage[key] = data;
}
}
}
ただし、より洗練されたソリューションでは、サービス クラスを使用し、ICache インターフェイスの Getter とコンストラクタによるキャッシュの注入を行います。ICache は、既存のキャッシング ソリューションをラップできます。