I’ve created a forum, and we’re implementing an apc and memcache caching solution to save the database some work.
I started implementing the cache layer with keys like ‘Categories::getAll’, and if I had user-specific data, I’d append the keys with stuff like the user ID, so you’d get 'User::getFavoriteThreads|1471'. When a user added a new favorite thread, I’d delete the cache key, and it would recreate the entry.
However, and here comes the problem:
I wanted to cache the threads in a forum. Simple enough, ‘Forum::getThreads|$iForumId’. But… With pagination, I’d have to split this into several cache entries, for example
'Forum::getThreads|$iForumId|$iLimit|$iOffset'.
Which is alright, until someone posts a new thread in the forum. I will now have to delete all the keys under 'Forum::getThreads|$iForumId', no matter what the limit and offset is.
What would be a good way of solving this problem? I’d really rather not loop through every possible limit and offset until I find something that doesn’t match anymore.
Thanks.
You might also want to have a look at the cost of storing the cache data, in terms of your effort and CPU cost, against how what the cache will buy you.
If you find that 80% of your forum views are looking at the first page of threads, then you could decide to cache that page only. That would mean both cache reads and writes are much simpler to implment.
Likewise with the list of a user’s favourite threads. If this is something that each person visits rarely then cache might not improve performance too much.