AppFabric Cache 'Design' - Caching Individual Items or Collections?

戏子无情 提交于 2019-12-10 13:58:31

问题


We are in the process of scaling one of our web applications from a single server to a web farm. The application currently uses the Http runtime cache to cache the reference data for the application. The data is 'similar' to product catalog:

  • categories (ie List)
  • products (ie List)

As the data is updated very infrequently, we also pre-compute some lookups

  • ProductsByCategory (ie. Dictionary)

The collections are currently cached as whole objects... ie the entire list / dictionary is get and put. The collections are typically used as I usually need to populate drop down lists / list boxes / etc.

The reference data needs to be kept in sync across the servers in the farm. Enter AppFabric...

  1. Is our above caching model still appropriate for AppFabric caching? The examples I have come across appear to put individual item in the cache as opposed to entire collections (with 'regions' being used for bulk get operations)

  2. What is the best way, if at all, to keep the reference data updated 'together'... ie if the categories are refreshed I need the products refreshed to reflect the latest categories.


回答1:


Support for the 'bulk put'-type operation is a bit, um, limited at the moment (read: nonexistent). But a little thought suggests this is probably correct - if you threw a collection of objects at the cache, it wouldn't know how to give them meaningful keys or tags (of which more in a moment). You could fake it with an extension method that accepted a collection of objects and looked at maybe a Name or Id property on each object to put the objects into the cache with, but under the covers it would still boil down to putting objects into the cache one at a time.

For getting a set of objects out of the cache, however, there is another option to the GetObjectsInRegion(regionname) method. If, at the point where the object is inserted to the cache you add a tag to it e.g. for a product, tag it with the category, later on you can get all the objects back for a particular tag with the GetObjectsByTag method. The great thing (I think) about using tags is that you can put any number of them on an object in the cache e.g. for a product, you could tag it with the category but also with the supplier and, say, a price band. This gives you a lot more flexibility in how you can query your cached items - you can query by any individual tag, or you can do AND/OR queries with the GetObjectsByAllTags/GetObjectsByAnyTag methods.




回答2:


Came across this same issue myself. Use local cache, use individual entities in a region, and use region-level notification callbacks



来源:https://stackoverflow.com/questions/4201131/appfabric-cache-design-caching-individual-items-or-collections

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!