Search code examples
cachingappfabric

Caching pattern on Appfabric (Whole list Vs individual items)


I have a web application that stores project in the database. I have decided to use App Farbic Caching to speed performance. What would be the best pattern regarding the below (or on which criteria should I decide): store each project separately in the cache. OR store the whole list in the cache (i.e. one key which represent the list of items)?

Many Thanks,

Joseph


Solution

  • It depends. There are a couple of considerations. If the list was potentially enormous, the content of the individual cache key could get very large (obviously this could be mitigated by enabling local caching). Serializing and de-serializing a large object graph like this is going to consume time and resources on your client. You may however want to do this, as you may in your application want to execute a linq to objects query against your list after it has been de-serialized back from the cache.

    If the queries you execute against the list are well defined, you could cache multiple flavors of the list under different cache keys - instead of people, you could have PeopleMale, PeopleFemale, PeopleAmerican, PeopleIrish, PeopleFrench etc. If you do this you could potentially have the same person appearing under multiple cached person lists and you would have to manage this. For example, I have a female person with dual american and irish citizenship. If I edit that person so the gender changes from female to make and the citizenship is changed to dutch, it would be necessary to ensure that four keys are invalidated PeopleMale, PeopleFemale, PeopleAmerican, PeopleIrish.

    The example I've given above could get tricky to manage - whether its worth it or not really depends on your exact use case.

    In general, where possible, I'd advise you to only use cache keys containing lists for relatively non-volatile reference data (countries, status types, nationalities etc).

    Hope this helps.