如何缓存有 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/