【问题标题】:How can we use memcache for updated data我们如何使用 memcache 来更新数据
【发布时间】:2016-06-26 06:36:23
【问题描述】:

我的代码正在从数据库中获取数据。从数据库加载数据大约需要 5 秒。所以我使用 memcached 缓存来缓存数据库数据。所以页面加载大约需要 1 秒。但问题是当我更新数据库中的数据我的网页显示旧的缓存数据。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);

请帮忙解决这个问题。

【问题讨论】:

    标签: php caching memcached


    【解决方案1】:

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

    请尝试执行以下代码sn-p

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

    在上面的代码中,sn -p key 存储在 memcache 服务器上的值的过期时间设置为 30 秒。

    请参考以下网址中提到的文档。

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

    【讨论】:

    • 但是@rubin 设置新值我需要在过期后从数据库中获取数据。加载需要时间。
    • 如何在使用 memcache 更改数据时删除旧缓存并生成新缓存?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多