I find the article titled How Basecamp Next got to be so damn fast without using much client-side UI from 37 signals (here) but couldn’t understand how the cache invalidating works.
let say:
i have a post object in array form like this:
$post = array(
'id' => 3232,
'title' => 'Test Post'
)
I’am saving it in the cache with cache key named: post-3232. And i have a latest posts array like this:
$latest = array(
array(
'id' => 3232,
'title' => 'Test Post'
),
array(
'id' => 3233,
'title' => 'Test Post 1'
),
array(
'id' => 3234,
'title' => 'Test Post 2'
)
)
I’m saving this list in cache with the cache key: latest-posts-140320121947
How can my latest posts list knows if any of the posts are deleted, changed or a new post is inserted?
I can’t figure out it from the article. maybe i can find some help from StackOverflow.
In simple terms, they can’t – the issue with caching is that you are deliberately serving old data. Sometimes that’s okay, of course.
But, you can invalidate some caches. So, if you rewrite a single post, you should invalidate the cache for it, so that the next time it is viewed it re-caches with the new data. Single items like this are easy.
But if you have a cache that contains a large amount of data (say the cached versions of a paginated screen, like Latest Posts, one cache item per page) then it is often too awkward/slow to update a single table row within it. If so, then it might be better to serve this screen uncached. But if Latest Posts just contains the last 10 items, invalidate the whole cache for that and redraw it.
You could have the cached copy of this page contain a cached item for each table row (i.e. several cached rows in a larger cached table); however, the smaller your cache area, the less benefit you get. But that would certainly make it easier to invalidate just one row on its own.