Currently looking into distributed memory cache solutions, such as Memcached and Microsoft's AppFabric caching. It seems to me that one of the difficult hurdles in implemented a distributed cache for an application is determining appropriate keys for the hash.
A naive approach is to hard code hash keys in your application so that all instances across all cluster nodes use the same key for the same data.
A slightly less naive approach for SQL select statements would be to perhaps generate an MD5 on the select statement itself. These select statements may be dynamically generated by an ORM, so you're not stuck with pre-determined keys.
But what if you want to generate keys based on the uniqueness of a delegate in one of you .NET classes? Consider the following:
public class MyExpensiveObject
{
MemCachedClient client = new MemCachedClient();
public static MyExpensiveObject LoadAll()
{
MyExpensiveObject cached = client.Get<MyExpensiveObject >(Utility.GetKey<MyExpensiveObject>(LoadAllDelegate));
if(cached == null)
{
cached = LoadAllDelegate();
client.Store(Utility.GetKey<MyExpensiveObject>(LoadAllDelegate), cached);
}
return cached;
}
public static List<MyExpensiveObject> LoadAllDelegate()
{
// return a list of all MyObjects from SQL, or wherever
}
}
public static class Utility
{
public static string GetKey<TType>(Func<TType> func)
{
// How do I do this, such that each delegate returns the same Key value across application instances?
// I know this won't work, it seems to return same value within an app domain, but different app domains return different value (which I expected)
return func.GetHashCode().ToString();
}
}
Is there a way to uniquely identify a delegate such that we know they are the same thing, across app domains? Ideally the solution would work for anonymous functions and lamdas as well.
Am I shooting for the moon? :)