Caching paginated results, purging on update - how to solve?

前端 未结 8 981
忘掉有多难
忘掉有多难 2021-02-04 18:08

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

8条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 18:20

    One possible solution is not to paginate the cache of threads in a forum, but rather put the thread information in to Forum::getThreads|$iForumId. Then in your PHP code only pull out the ones you want for that given page, e.g.

    $page = 2;
    $threads_per_page = 25;
    $start_thread = $page * $threads_per_page;
    
    // Pull threads from cache (assuming $cache class for memcache interface..)
    $threads = $cache->get("Forum::getThreads|$iForumId");
    
    // Only take the ones we need
    for($i=$start_thread; $i<=$start_thread+$threads_per_page; $i++)
    {
        // Thread display logic here...
        showThread($threads[$i]);
    }
    

    This means that you do have a bit more work to do pulling them out on each page, but now only have to worry about invalidating the cache in one place on update / addition of new thread.

提交回复
热议问题