php - 如何缓存动态PHP页面

标签 php caching

如何缓存有 mysql 查询的 PHP 页面。任何示例都会非常有用。

最佳答案

我正在使用 phpFastCache (对于共享主机,如果您不想触摸 php.ini 和 root 来设置 memcached)。查看示例菜单。他们有完整的详细示例,而且非常简单。

首先使用 phpFastCache::set 设置,然后使用 phpFastCache::get 获取 - 完成!

示例:减少数据库调用

您的网站有 10,000 名在线访问者,您的动态页面必须在每次页面加载时向数据库发送 10,000 个相同的查询。使用 phpFastCache,您的页面仅向数据库发送 1 个查询,并使用缓存为其他 9,999 个访问者提供服务。

<?php
    // In your config file
    include("php_fast_cache.php");
    phpFastCache::$storage = "auto";
    // you can set it to files, apc, memcache, memcached, pdo, or wincache
    // I like auto

    // In your Class, Functions, PHP Pages
    // try to get from Cache first.
    $products = phpFastCache::get("products_page");

    if($products == null) {
        $products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;
        // set products in to cache in 600 seconds = 5 minutes
        phpFastCache::set("products_page",$products,600);
    }

   OUTPUT or RETURN your $products
?>

关于php - 如何缓存动态PHP页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3787125/

相关文章:

database - 更新数据库后如何更新redis?

PHP - 创建注销链接

php - 使用ajax将post数据提交到php文件

php - 从数据库表中查找第一个可用标识符

caching - AppFabric 缓存 : ErrorCode<ERRCA0017>:SubStatus<ES0001>:There is a temporary failure. 请稍后重试

java - 防止单个表使用缓存

php - 正则表达式查找一个词然后从考虑中排除特定词

PHPUNIT 无需安装

javascript - 如何防止 Ajax 缓存

javascript - 为什么浏览器仍然从服务器拉取资源?