我试图从这段代码中删除一些重复,并让它轻松支持具有更多参数的函数。
您将如何改进此代码并允许更复杂的功能?
另外,我担心我的密钥生成,一些对象不会明显地序列化为字符串,而只是返回它们的类型名,而不是唯一值。建议?
编辑:我使用了 ChaosPandion 的答案,并把它归结为
using System;
using System.Web.Caching;
public static class Memoize
{
public static Cache LocalCache = System.Web.HttpRuntime.Cache ?? System.Web.HttpContext.Current.Cache;
public static TResult ResultOf<TArg1, TResult>(Func<TArg1, TResult> func, long durationInSeconds, TArg1 arg1)
{
var key = HashArguments(func.Method.Name, arg1);
return ResultOf(key, durationInSeconds, () => func(arg1));
}
public static TResult ResultOf<TArg1, TArg2, TResult>(Func<TArg1, TArg2, TResult> func, long durationInSeconds, TArg1 arg1, TArg2 arg2)
{
var key = HashArguments(func.Method.Name, arg1, arg2);
return ResultOf(key, durationInSeconds, () => func(arg1, arg2));
}
public static TResult ResultOf<TArg1, TArg2, TArg3, TResult>(Func<TArg1, TArg2, TArg3, TResult> func, long durationInSeconds, TArg1 arg1, TArg2 arg2, TArg3 arg3)
{
var key = HashArguments(func.Method.Name, arg1, arg2, arg3);
return ResultOf(key, durationInSeconds, () => func(arg1, arg2, arg3));
}
public static TResult ResultOf<TArg1, TArg2, TArg3, TArg4, TResult>(Func<TArg1, TArg2, TArg3, TArg4, TResult> func, long durationInSeconds, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4)
{
var key = HashArguments(func.Method.Name, arg1, arg2, arg3, arg4);
return ResultOf(key, durationInSeconds, () => func(arg1, arg2, arg3, arg4));
}
private static TResult ResultOf<TResult>(string key, long durationInSeconds, Func<TResult> func)
{
return LocalCache.Get(key) != null
? (TResult)LocalCache.Get(key)
: CacheResult(key, durationInSeconds, func());
}
public static void Reset()
{
var enumerator = LocalCache.GetEnumerator();
while (enumerator.MoveNext())
LocalCache.Remove(enumerator.Key.ToString());
}
private static T CacheResult<T>(string key, long durationInSeconds, T value)
{
LocalCache.Insert(key, value, null, DateTime.Now.AddSeconds(durationInSeconds), new TimeSpan());
return value;
}
private static string HashArguments(params object[] args)
{
if (args == null)
return "noargs";
var result = 23;
for (var i = 0; i < args.Length; i++)
{
var arg = args[i];
if (arg == null)
{
result *= (31 * i + 1);
continue;
}
result *= (31 * arg.GetHashCode());
}
return result.ToString();
}
}