I need a way to detect changes of my database because I’m implementing a caching system on a client’s site. (caching the whole page, not just the queries)
At the top of the every page (i have a “master page” so just one place in code) I query the database to see if its size is changed like so.
I sum the size and get a “unique” number. Compare it to the previous one, and if the values are the same, deliver the cached page, if not, run the “normal” page and then cache that.
So I think in theory this could work, because I have just 1 database query, and based on that deliver the site (cached or uncached depending on the result) to the user.
Could this work, and if not – why?
EDIT:
what about cheking for the last update timestamp? LINK
Who is updating the database? Suggest that the process causing the change should be the one invalidating your cache. When a user/admin causes a change to occur to the table, you could:
Suggest size is not a good indicator of ‘change occurred’. Forget rows – what if a bool/bit value changes? That’d satisfy the condition to invalidate the cache, but the ‘size’ would always be the same. You could improve this with a last-change-datetime, but you’re still having the same basic problem – querying the database to check for a change flag. Kind of defeats the purpose of a cache.
Consider another approach – reload your cache every n minutes. Whether there’s been a change or not, just reload it. n will be a value that fits in your business requirements. You could have stale cache, and be serving old/stale data for at most n minutes. This wouldn’t be my suggestions, but something to consider.