2014-09-23 75 views
3

我发现phpfastcahce缓存MySQL结果的类。在支持WINCACHE,内存缓存,文件,X-Cache功能,APC缓存,说细节:php缓存动态索引页面

在示例代码

PHP Caching Class For Database : 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"); 
    // 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 
    } 
?> 

现在,在我的我的网站的索引我有什么块显示最新消息,最好的消息,世界新闻.....为缓存我的索引,我必须缓存MySQL每个块的结果(last news, best news, world news .....)使用phpfastcache和管理页面删除所有缓存如果我编辑现有消息或添加新消息?这是一个真正的方法?

什么是最好的方式对于缓存MySQL结果我的索引页使用phpfastcache(任何方法)?!

+1

不是特定于phpfastcache(我并不熟悉),但通常在使用任何缓存时,您需要有关于如何处理缓存未命中的策略(即,查询数据库并将结果填充到缓存中),并处理从缓存中取消/清除项目(在更改为基础数据时清除缓存项,对缓存项应用TTL)等。 – 2014-09-23 21:48:38

+0

确实解答了您的问题吗? – 2015-01-19 08:07:25

回答

3

phpfastcache着uderstand你的数据被更改或不

后在数据库中更改具体数据

先在您的主页缓存代码必须是这样的,你必须做一些事情:

$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);