I am building a WCF service. I need to store reference data in the cache which I will look up every time I receive input from the method... What is the right way to do this?
There are many ways you can do this. One fairly easy is to host the System.Web.Cache object yourself and use that to store the reference data. There's a good example of that here: http://kjellsj.blogspot.com/2007/11/wcf-caching-claims-using.html
Rather than expiring the cache data every so often, you can actually just make sure to invalidate the cache whenever the underlying data you are caching changes.
See this example from info Q http://www.infoq.com/news/2011/04/Attribute-Caching
[Cache.Cacheable("UserTransactionCache")]
public DataTable GetAllTransactionsForUser(int userId)
{
return new DataProvider().GetAllTransactionsForUser(userId);
}
[Cache.TriggerInvalidation("UserTransactionCache")]
public void DeleteAllTransactionsForUser(int userId)
{
...
}
this is a good article: http://cascadeofinsights.com/post/1410736927/introducing-attribute-based-caching
You could take a look at Velocity. This is Microsoft's distributed in-memory caching framework. But this may be a little bit too beta...
If you are using .NET 4, the recommended way is to use MemoryCache
The WCF REST Starter Kit has caching, here is an article about using it... with sample code.
http://weblogs.asp.net/gsusx/archive/2008/10/29/adding-caching-to-wcf-restful-services-using-the-rest-starter-kit.aspx