0
    $new = $this->memcache->get("posts");
    if(empty($new)){

        // Get the new posts
        $new = Posts::getNow("10");

        // Save them in memcache
        $this->memcache->set('posts', serialize($new), 0, 60*3); // Cache time is 3 min

    // If we found them in cache - load them from there
    } else {

        // Get data from memcache
        $new = unserialize($this->memcache->get("posts"));
    }

コードは非常に単純で、そこからキャッシュ ロードにデータがあれば、それらを再度取得する必要はありません。興味深いのは、サイトを表示すると div が空でデータがない場合がありますが、ページをリロードするとデータがあります。キャッシュが消去されているときにサイトのビューが表示される可能性はありますか?

4

1 に答える 1

1

それはタイミングでなければなりません。キャッシュからデータを 2 回取得します。これらの呼び出しの間にデータが期限切れになる可能性があり、それを制御することはできません

すでに取得したデータをシリアル化解除するだけです。

$data = $this->memcache->get("posts");
if(empty($data)){
    // Get the new posts
    $new = Posts::getNow("10");
    // Save them in memcache
    $this->memcache->set('posts', serialize($new), 0, 60*3);
} else {
    // Unserialize data instead of retrieving it from cache for second time.
    $new = unserialize($data);
}
于 2012-06-20T12:10:52.840 に答える