I need to implement cache for SQL queries.
Say we have SELECT id,aa,bb FROM table1 WHERE aa>1 and bb=12; – we want it to be cached.
Also, it may be SELECT id,aa,bb FROM table1 WHERE aa>25 and bb=11; or SELECT id,aa,bb FROM table2; of course.
Not a big deal — what really is a question is how to expire cache values better. On updates. Say, we added new row with INSERT ... or updated existing data with UPDATE ....
We need to expire all cache which had SELECTs values, as they may not be accurate any more.
How to do this better?
In the simplest case — we use the whole “SQL query” ask key in the cache. But at the moment of INSERT/UPDATE we have no idea which queries to expire (which keys?)
One thing I’ve done before is to make a list of cache keys and store that in the cache as well.
When I need to cache something I grab the list, add the key to it, and store it back in the cache. Then when I need to clear the cache for a particular set, I grab the list and remove every key in the list from the cache and remove the list from the cache.
I get the impression this is how some cache libraries for example in NHibernate work too, but I’m not entirely sure about that.