phpFastCache.com を試す
これは簡単な例です。CSS をキャッシュすると、次のようになります。
require_once("phpfastcache/phpfastcache.php");
$css = __c()->get("csspage.user_id_something");
if($css == null) {
// handle your css function here
$css = "your handle function here";
// write to cache 1 hour
__c()->set("csspage.user_id_something", $css, 3600);
}
echo $css;
PHP Web ページ全体をキャッシュする: phpFastCache を使用して Web ページ全体を簡単にキャッシュすることもできます。これは単純な例ですが、実際のコードでは、2 つのファイル (cache_start.php と cache_end.php) に分割する必要があります。cache_start.php は ob_start(); まで最初のコードを保存します。cache_end.php は GET HTML WEBPAGE から開始されます。次に、index.php の先頭に cache_start.php が含まれ、ファイルの最後に cache_end.php が含まれます。
<?php
// use Files Cache for Whole Page / Widget
// keyword = Webpage_URL
$keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
$html = __c("files")->get($keyword_webpage);
if($html == null) {
ob_start();
/*
ALL OF YOUR CODE GO HERE
RENDER YOUR PAGE, DB QUERY, WHATEVER
*/
// GET HTML WEBPAGE
$html = ob_get_contents();
// Save to Cache 30 minutes
__c("files")->set($keyword_webpage,$html, 1800);
}
echo $html;
?>
データベース呼び出しを減らす データベースの PHP キャッシング クラス : あなたの Web サイトにはオンラインの訪問者が 10,000 人いて、動的ページは、ページが読み込まれるたびに 10,000 件の同じクエリをデータベースに送信する必要があります。phpFastCache を使用すると、ページは 1 つのクエリのみを DB に送信し、キャッシュを使用して 9,999 人の他の訪問者にサービスを提供します。
<?php
// In your config file
include("phpfastcache/phpfastcache.php");
phpFastCache::setup("storage","auto");
// phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "sqlite" and "xcache"
// You don't need to change your code when you change your caching system. Or simple keep it auto
$cache = phpFastCache();
// In your Class, Functions, PHP Pages
// try to get from Cache first.
// product_page = YOUR Identity Keyword
$products = $cache->product_page;
if($products == null) {
$products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;
// set products in to cache in 600 seconds = 10 minutes
$cache->product_page = array($products,600);
}
foreach($products as $product) {
// Output Your Contents HERE
}
?>