views:

22

answers:

1

First of all it might be worth looking at this question: http://stackoverflow.com/questions/445050/how-can-i-cache-objects-in-asp-net-mvc

There some pseudo code that almost does what i want:

public class CacheExtensions
{
  public static T GetOrStore<T>(this Cache cache, string key, Func<T> generator)
  {
    var result = cache[key];
    if(result == null)
    {
      result = generator();
      cache[key] = result;
    }
     return (T)result;
   }
}

However, what I'd really like to do, is auto-generate the "key" from the generator. I figure i need to change the method signature to:

public static T GetOrStore<T>(this Cache cache,
                System.Linq.Expressions.Expression<Func<T>> generator)

I want to use the method name, but also any parameters and their values to generate the key. I can get the method body from the expression, and the paramter names (sort of), but I have no idea how to get the paramter values...?

Or am I going about this the wrong way? Any ideas much appreciated.

A: 

When calling a function that produces a collection i want to cache i pass all my function's parameters and function name to the cache function which creates a key from it.

All my classes implement an interface that has and ID field so i can use it in my cache keys.

I'm sure there's a nicer way but somehow i gotta sleep at times too.

I also pass 1 or more keywords that i can use to invalidate related collections.

Jeroen