Skip to content

Instantly share code, notes, and snippets.

@hugodahl
Created January 18, 2012 16:57
Show Gist options
  • Save hugodahl/1634035 to your computer and use it in GitHub Desktop.
Save hugodahl/1634035 to your computer and use it in GitHub Desktop.
// Disclaimer, was written in LINQpad, so may need a bit of tweaking on Main() to run in Visual Studio
// but the general idea is there. Can just call this Main() from VS's generated Main().
void Main()
{
// Returns an int, and we're happy
var x = GetCachedValue("Int", new object(), () => {return 1;});
Console.WriteLine(x);
// Returns a string, and it's all good still
var y = GetCachedValue("String", new object(), () => {return "Bubba";});
Console.WriteLine(y);
// Uses a call with specified parameters and returns a List<Object>
var z = GetCachedValue<List<Object>>("List<object>", new object(), () => GetStuffInObjectList(1, false, "Hello", "world"));
}
private List<object> GetStuffInObjectList(int a, bool b, string c, string d)
{
return new List<Object>{a, b, c, d};
}
private TCachedValueType GetCachedValue<TCachedValueType>(string cacheKey, object cacheLock, Func<TCachedValueType> method)
{
lock(cacheLock)
{
TCachedValueType result = method();
Console.WriteLine("{0} - {1}", cacheKey, result);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment