C#与委托缓存

C#与委托缓存,c#,caching,delegates,C#,Caching,Delegates,我正在为我的应用程序开发一个简单的缓存类 using System; namespace Program { public class Cache { public delegate int CacheMethodInt(); public static int Get(CacheMethodInt method) { //todo: generate cachekey here return method.Invoke(); } }

我正在为我的应用程序开发一个简单的缓存类

using System;
namespace Program {
  public class Cache {
    public delegate int CacheMethodInt();
    public static int Get(CacheMethodInt method) {
      //todo: generate cachekey here
      return method.Invoke();
    }
  }

  public class Calculator {
    public int Add(int x, int y) {
      return x + y;
    }
  }

  class Program {
    static void Main(string[] args) {
      Calculator c = new Calculator();
      Console.WriteLine(Cache.Get(() => c.Add(1, 2)));
    }
  }
}
Cache:Get
中,我需要检查返回值是否已被缓存,如果已缓存,则在不调用方法的情况下返回它。问题是我不知道如何生成一个好的cachekey。在这种情况下,我希望这样:

Calculator:Add:1(int):2(int)

是否可以在
缓存:get
中获取此信息?我正在使用.NET2.0。

可以使用反射

作为替代方案,在一个项目中,我使用Postsharp实现同样的目的。作为一种更通用、更通用的方法

而且不要忘记缓存失效或过期

相关问题显示如何获取MethodInfo和方法名称:


当您拥有MethodInfo时,您无法获得所需的所有数据

缓存重新计算如此便宜的数据是没有意义的。使用[家庭作业]标签回答家庭作业问题。