开发者

Storing User data in cache vs Querying data everytime

If I store all my user info, upon login, in cache and just have a "my account" page that displays the user info from cache, is that a good practice? I'm wondering if a person changes his email or does any type of database modification then the cached variables will be the wrong ones. If I have to update the cache variables every time something happens to the database, is this even a good practice compared 开发者_开发百科to just querying the data whenever needed?

-Thanks


How often will the user info data will be changed? If its not too frequently, you can have the sliding expiration for the cache.

http://msdn.microsoft.com/en-us/library/aa478965.aspx

Also note that, since user data is different for each user, you need to store as many amount of user info data as the user logged in.


Do you get performance issues if the data is not cached, and you run the query each time? (You'll probably need to measure this to find out). If not, the extra complexity and cache invalidation issues (as you describe) mean you should probably just run the query.


The more relevant question is how often are you using the user data. Do you need to access it:

  • every request
  • only the member page
  • just display a name somewhere

If it's just the member's page, like I'm understanding, then its probably best to hit the DB (if no perf issues).

If you're using the data elsewhere or on every page, then you should probably do some sort of sliding-expiration cache. And invalidate it from the update action. If you stick to a standard format for every insertion to the cache, it'll be easy enough to invalidate.

And remember, to code as if the cache will be empty, hit the db, insert into the cache, and return the user info.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜