【问题标题】:php cache dynamic index pagephp缓存动态索引页面
【发布时间】:2014-09-24 03:38:20
【问题描述】:

我找到phpfastcahce 缓存 MySQL 结果的类。详细说明支持 WinCache、MemCache、Files、X-Cache、APC Cache 并说:

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
    }
?>

现在,在我的网站索引中,我有任何块来显示最新消息、最佳新闻、世界新闻.....为了缓存我的索引,我必须使用 phpfastcache 为每个块(last news, best news, world news ..... ) 缓存 MySQL 结果如果我编辑现有新闻或添加新新闻,则在管理页面中删除所有缓存?这是真道?

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

【问题讨论】:

  • 不特定于 phpfastcache(我对此并不熟悉),但通常在使用任何缓存时,您需要制定有关如何处理缓存未命中的策略(即查询数据库并使用结果填充缓存) 并处理缓存中的无效/清除项目(在更改为基础数据时清除缓存条目,为缓存项目应用 TTL)等。
  • 回答解决了你的问题吗?

标签: php mysql caching


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多