【问题标题】:Enabling PHP APC Query Caching启用 PHP APC 查询缓存
【发布时间】:2014-05-26 03:03:11
【问题描述】:

我已经编写了我的第一个功能性 PHP Web 应用程序,名为 Heater。它使用 Google Charts 库和 AWS Redshift backend 呈现交互式日历热图。

现在我已经开始工作了,我已经开始提高性能了。我已经安装了 APC 并验证它工作正常。

我的问题是如何在 Redshift 前启用查询缓存?

这是我现在如何加载数据的示例:

getRsData.php:



    <?php
        $id=$_GET["id"];
        $action=$_GET["action"];
        $connect = $rec = "";
        $connect = pg_connect('host=myredshift.redshift.amazonaws.com port=5439 dbname=mydbname user=dmourati password=mypasword');
        if ($action == "upload")
                $rec = pg_query($connect,"SELECT date,SUM(upload_count) as upload_count from dwh.mytable where enterprise_id='$id' GROUP BY date");
...

    ?>

有些查询需要超过 5 秒的时间,这会对用户体验产生负面影响。数据移动缓慢,因为它每天只更新一次。我想在 Redshift 查询前面使用本地 APC 缓存,然后每天一次通过 cron(或类似的)使其无效,以允许更新的数据流入。我最终想创建一个缓存预热脚本,但是目前没有必要。

对文档的任何指示或提示都是有帮助的。我花了一些时间在谷歌上搜索,但大多数文档只是关于文档缓存而不是查询缓存(如果有意义的话)。这是一个运行 AWS Linux 和 PHP 5.3 以及 apc-3.1.15 的独立主机。

谢谢。

编辑以添加输入验证

if (!preg_match("/^[0-9]*$/",$id)) {
        $idErr = "Only numbers allowed";
        }

if (empty($_GET["action"])) {
     $actionErr = "Action is required";
   } else {
     $action = test_input($action);
   }

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

【问题讨论】:

标签: php postgresql amazon-web-services apc


【解决方案1】:

这似乎不需要 APC,因为您要缓存一天相对较长的数据。

下面的代码将您的查询结果缓存在一个文件 ($cache_path) 中。在查询 redshift 之前,它会检查给定企业 ID 的缓存文件是否存在并且是在同一天创建的。如果是这样并且如果代码可以成功检索缓存,则从缓存中返回行,但如果文件不存在或无法从缓存中检索行,代码将查询数据库并写入缓存.

查询/缓存的结果在$rows中返回

<?php
    $id=$_GET["id"];
    $action=$_GET["action"];
    $connect = $rec = "";
    $connect = pg_connect('host=myredshift.redshift.amazonaws.com port=5439 dbname=mydbname user=dmourati password=mypasword');
    if ($action == "upload") {

        $cache_path = "/my_cache_path/upload_count/$id";

        if(!file_exists($cache_path) 
            || date('Y-m-d',filemtime($cache_path)) < date('Y-m-d')
            || false === $rows = unserialize(file_get_contents($cache_path))) {

            $rows = array();

            $rec = pg_query($connect,"SELECT date,SUM(upload_count) as upload_count from dwh.mytable where enterprise_id='$id' GROUP BY date");
            while($r = pg_fetch_assoc($rec)) {
                $rows[] = $r;
            }

            file_put_contents($cache_path,serialize($rows));
        }
    }
?>

【讨论】:

  • 感谢您的回答。你能详细说明一下第一句话吗,我不明白你在说什么。我的意思是 Redshift 数据每天更新。
  • @dmourati 我的意思是缓存最多每天更新一次。 APC 优于文件缓存的优势在于 APC 在内存中,因此比文件缓存更快,但对于这种情况,文件缓存似乎足够了,因为它不经常更新(如果缓存的数据很大,可能更可取)。
【解决方案2】:

如果您不想要文件缓存,只需使用 FastCache 之类的缓存类

http://www.phpfastcache.com/

它可以自动找到apc或任何其他缓存解决方案。

使用真的很简单

    <?php
    // In your config file
    include("phpfastcache/phpfastcache.php");
    phpFastCache::setup("storage","auto");

    // phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "sqlite" and    "xcache"
    // You don't need to change your code when you change your caching system. Or simple keep it auto
    $cache = phpFastCache();

    // In your Class, Functions, PHP Pages
    // try to get from Cache first. product_page = YOUR Identity Keyword
    $products = $cache->get("product_page");

    if($products == null) {
        $products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;
        // set products in to cache in 600 seconds = 10 minutes
        $cache->set("product_page", $products,600);
    }

    // Output Your Contents $products HERE
?>

这个例子也是来自http://www.phpfastcache.com/

希望对您有所帮助,并在您非常酷的项目中玩得开心!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多