using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
namespace CacheExtensions
{
static class PersistentCache
{
static Dictionary<string, object> cache = new Dictionary<string, object>();
public static R Call<T,R>(this T p, Expression<Func<T, R>> func)
{
string index = GetIndex(func);
object co;
if (cache.TryGetValue(index, out co))
{
return (R)co;
}
else
{
Func<T, R> cfunc = func.Compile();
var cf = cfunc(p);
cache[index] = cf;
return cf;
}
}
static string GetIndex<T, R>(Expression<Func<T, R>> func)
{
var mc = func.Body as MethodCallExpression;
var cc = Expression.Lambda(
Expression.Call(
makekey,
Expression.NewArrayInit(typeof(object), mc.Arguments))).Compile()
as Func<string>;
return string.Format("{0}({1})", mc.Method.Name, cc());
}
readonly static MethodInfo makekey = new Func<object[],string>(MakeKey).Method;
public static string MakeKey(params object[] args)
{
string[] a = Array.ConvertAll(args, o => o == null ? "null" : ( "'" + o + "'" ));
return string.Join(",", a);
}
}
}
Posted by: leppie | October 20, 2008
Simple memomization with LINQ
Posted in Default
Leave a Reply