我找到了 phpfastcahce缓存 MySQL 结果的类。在支持 WinCache、MemCache、Files、X-Cache、APC Cache 的细节中说:
数据库的 PHP 缓存类:您的网站有 10,000 名在线访问者,并且您的动态页面必须在每次页面加载时向数据库发送 10,000 个相同的查询。使用 phpFastCache,您的页面仅向数据库发送 1 个查询,并使用缓存为 9,999 名其他访问者提供服务。
在示例代码中:
<?php
// In your config file
include("php_fast_cache.php");
// This is Optional Config only. You can skip these lines.
// phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "pdo", "mpdo" and "xcache"
// You don't need to change your code when you change your caching system. Or simple keep it auto
phpFastCache::$storage = "auto";
// End Optionals
// 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 = 10 minutes
phpFastCache::set("products_page",$products,600);
}
foreach($products as $product) {
// Output Your Contents HERE
}
?>
现在,在我的网站索引中,我有任何 block 用于显示最新消息、最佳新闻、世界新闻......为了缓存我的索引,我必须为每个 block 缓存 MySQL
结果(最新消息,最佳新闻,世界新闻.....)
如果我编辑现有新闻或添加新新闻,则使用 phpfastcache 并在管理页面中删除所有缓存?这才是真正的方法?
对于缓存 MySQL
结果我的索引页面使用 phpfastcache(任何方法)的最佳方法是什么?!
最佳答案
phpfastcache 无法理解您的数据是否已更改
你必须在你的数据库中的特定数据改变后做一些事情
首先在你的主页缓存代码必须是这样的:
$lastnews = phpFastCache::get('index_lastnews');
$bestnews = phpFastCache::get('index_bestnews');
$worldnews = phpFastCache::get('index_worldnews');
if($lastnews == null) {
$lastnews = YOUR DB QUERIES || GET_DATA_FUNCTION;
phpFastCache::set('index_lastnews',$lastnews,600);
}
if($bestnews == null) {
$bestnews = YOUR DB QUERIES || GET_DATA_FUNCTION;
phpFastCache::set('index_bestnews',$bestnews,600);
}
. . .
当特定数据更改时,在您的管理页面中缓存代码必须是这样的:
AFTER DATABASE insert | update ....
您可以通过以下两种方式替换旧缓存:
1)删除缓存(删除缓存后,第一次访问后自动重建缓存)
phpFastCache::delete('index_lastnews');
2) 更新缓存
$lastnews = YOUR DB QUERIES || GET_DATA_FUNCTION;
phpFastCache::set("index_lastnews",$lastnews,600);
关于php缓存动态索引页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26002825/