views:

38

answers:

2
+1  Q: 

Update cached data

Hi

U are caching a list of items, then Add or update or delete method called.

Approach 1)

A- Do the data change at the database. If success do B, C
B- Remove the entire list from the Cache.
C- Re-Cache the list on first read.

Approach 2)

A- Do the data change at the database, If success do B
B- Do the same change at the cached items, and save more time.



Approach(2) probably will cause nasty difficult to spot bugs, because the data come from BLL and placed directly into cache, One can't assume no bugs will happen at the sproc, I am not worry about database runtime errors, runtime errors will show up, but I am worry about invalid data, or miss calculation.

Can I proceed with approach (2) with caution? Is there a "best practice" principle which suggest not doing approach(2)?

+2  A: 

Well, personally I will not go for 2. Like you said it yourself, concurrency issues can cause nasty outcomes.

I will not drop (2) completely though if (1) is not 'good' enough.

Factors to consider:

  • Hit rates (read and edit/delete)
  • Number of records/items
  • Size of each item
  • etc etc...
o.k.w
+1  A: 

You should also take into consideration, whether the data can be changed in the database from an other applicaton (which bypasses your cache). In these cases the second approach (2) might result in showing invalid data to the user.

I would suggest to measure the performance of both solutions, and use the second approach only when it has a well defined benefit comparing to the first one.

treaschf