问题描述
如何缓存具有mysql查询的PHP页面.任何例子都是很好的和有益的.
How to cache PHP page which has mysql query. Any example will be great and helpful.
推荐答案
我正在使用 phpFastCache (用于共享托管,如果您不这样做的话)不想触摸php.ini和root来设置memcached).查看示例菜单.他们有完整的示例示例,非常简单.
I am using phpFastCache ( for shared hosting, if you don't want to touch php.ini and root to setup memcached). Check out the Example Menu. They have full detail example, and very easy.
首先使用phpFastCache :: set设置,然后使用phpFastCache :: get-完成!
First you set with phpFastCache::set and then get with phpFastCache::get - DONE!
示例:减少数据库调用
您的网站有10,000个在线访问者,并且您的动态页面必须在每次页面加载时向数据库发送10,000个相同的查询.使用phpFastCache,您的页面仅向数据库发送1条查询,并使用缓存为9,999位其他访问者提供服务.
Your website have 10,000 visitors who are online, and your dynamic page have to send 10,000 same queries to database on every page load. With phpFastCache, your page only send 1 query to DB, and use the cache to serve 9,999 other visitors.
<?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页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!