我们如何将内存缓存用于更新的数据


How can we use memcache for updated data

我的代码正在从数据库中获取数据。从数据库加载数据大约需要 5 秒。所以我使用 memcached 缓存来缓存数据库 data.so 加载页面大约需要 1 秒。但问题是当我更新数据库中的数据时,我的网页显示旧的缓存 data.so memcache 中是否有任何自动更新机制,或者如何使用 memcache 显示经常更新的数据。这是我的代码:

$memcache = new Memcache;
    $memcache->connect('localhost',portno) or die ("Could not connect");
    $result = $memcache->get('key'. $_SESSION['front_app']['employee_id'].$month.$year); 
        if(!$result){
            foreach ($call_arr as $c) {
                $result[] = $a->get_submitted_status($start_date, $end_date,id); /*this fetches data from database*/
            } 
        } 
        $memcache->set('key'.id.$month.$year,$result);

请帮助解决此问题。

作为 memcache 中自动更新机制的解决方案,您可以在将数据项存储在 memcache 服务器中时配置数据的有效期

请尝试执行以下代码片段

$memcache_object = new Memcache();
$memcache_object->connect('localhost', 11211);
$memcache_object->set('key', 'value', MEMCACHE_COMPRESSED, 30);
echo $memcache_object->get('key');

在上面的代码片段中,存储在memcache服务器上的密钥值的到期期限设置为30秒。

请参阅以下 URL 中提到的文档。

http://php.net/manual/en/memcache.set.php