试试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 缓存整个网页:您也可以使用 phpFastCache 轻松缓存整个网页。这是一个简单的示例,但在实际代码中,您应该将其拆分为 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 缓存类:您的网站有 10,000 名在线访问者,并且您的动态页面必须在每次页面加载时向数据库发送 10,000 个相同的查询。使用 phpFastCache,您的页面仅向 DB 发送 1 个查询,并使用缓存为 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
}
?>