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

那一定是时间,你从缓存中检索数据两次,一次是为了检查它在这里,第二次是为了反序列化它。数据可以在这些调用之间过期,我们无法控制它

只需反序列化您已经获得的数据:

$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 回答