开发者

How do you organize and maintain cache keys and flush them?

Our system uses AppFabric Caching on Azure and we have multiple kinds of apps and 开发者_开发技巧roles that are sharing the same cached values. I'm looking for some recommendations on how to organize all the keys and also have the ability to invalidate/flush entries when they have been altered.

I've played around with the idea of having a static class with a set of methods that create the keys. For example:

string CreateUserByIdKey(int userId) - Returns "User_5"
string CreateWidgetsByCompanyKey(int companyId) - Returns "Widgets_Company_5"

This way I have a semi-structure way to create and use keys across different applications. But this doesn't feel very elegant and maintainable. It also requires me to create special flush methods that know which of these keys need to be invalidated when the data has been updated.

What is a better way to do this?


Our approach is to use a CacheManager with a strategy extension. The CacheManager is located in a Core part of the solution, and that core is referenced through all of the roles.

For things that are shared, like strategies for generating cache keys, and even endpoint names, we have a list of WellKnownComponents. This is a TT generated file (well, files), that can be extended with partial classes.

What I would do in your case is add the cache key id generating function to my well known components class, and then reference through the application, e.g.

var seeker = CacheManager.Get<RequestOnly>(WellKnownComponents.Seeker.Caching.GetSeekerKey(seekerId), () => GetSeekerFromDataBase(seekerId));

This is approximately how we are doing it. And yes, you still need special flush methods for invalidations, but the upside part is you can both separate and combine them where it's important and relevant (e.g. your one component, instead of around the solution).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜