《PHP教程:PHP緩存集成庫phpFastCache用法》要點:
本文介紹了PHP教程:PHP緩存集成庫phpFastCache用法,希望對您有用。如果有疑問,可以聯系我們。
PHP應用本文實例講述了PHP緩存集成庫phpFastCache用法.分享給大家供大家參考.具體分析如下:
PHP應用phpFastCache是一個開源的PHP緩存庫,只提供一個簡單的PHP文件,可方便集成到已有項目,支持多種緩存辦法,包括:apc, memcache, memcached, wincache, files, pdo and mpdo.可通過簡單的API來定義緩存的有效時間.
PHP應用// 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();
PHP應用// In your Class, Functions, PHP Pages
// try to get from Cache first. product_page = YOUR Identity Keyword
$products = $cache->get("product_page");
PHP應用if($products == null) {
??? $products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;
??? // set products in to cache in 600 seconds = 10 minutes
??? $cache->set("product_page", $products,600);
}
PHP應用// Output Your Contents $products HERE
PHP應用$cache = phpFastCache("memcached");
PHP應用// try to get from Cache first.
$results = $cache->get("identity_keyword")
PHP應用if($results == null) {
??? $results = cURL->get("http://www.youtube.com/api/json/url/keyword/page");
??? // Write to Cache Save API Calls next time
??? $cache->set("identity_keyword", $results, 3600*24);
}
PHP應用foreach($results as $video) {
??? // Output Your Contents HERE
}
PHP應用全頁緩存
PHP應用// keyword = Webpage_URL
$keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
$html = __c("files")->get($keyword_webpage);
PHP應用if($html == null) {
??? ob_start();
??? /*
??????? ALL OF YOUR CODE GO HERE
??????? RENDER YOUR PAGE, DB QUERY, WHATEVER
??? */
PHP應用??? // GET HTML WEBPAGE
??? $html = ob_get_contents();
??? // Save to Cache 30 minutes
??? __c("files")->set($keyword_webpage,$html, 1800);
}
PHP應用echo $html;
PHP應用掛件緩存
PHP應用$html = $cache->widget_1;
PHP應用if($html == null) {
??? $html = Render Your Page || Widget || "Hello World";
??? // Save to Cache 30 minutes
??? $cache->widget_1 = array($html, 1800);
}
PHP應用echo or return your $html;
PHP應用同時使用多種緩存
PHP應用$cache1 = phpFastCache();
PHP應用$cache2 = __c("memcache");
$server = array(array("127.0.0.1",11211,100), array("128.5.1.3",11215,80));
$cache2->option("server", $server);
PHP應用$cache3 = new phpFastCache("apc");
PHP應用// How to Write?
$cache1->set("keyword1", "string|number|array|object", 300);
$cache2->keyword2 = array("something here", 600);
__c()->keyword3 = array("array|object", 3600*24);
PHP應用// How to Read?
$data = $cache1->get("keyword1");
$data = $cache2->keyword2;
$data = __c()->keyword3;
$data = __c()->get("keyword4");
PHP應用// Free to Travel between any caching methods
PHP應用$cache1 = phpFastCache("files");
$cache1->set("keyword1", $value, $time);
$cache1->memcache->set("keyword1", $value, $time);
$cache1->apc->set("whatever", $value, 300);
PHP應用$cache2 = __c("apc");
$cache2->keyword1 = array("so cool", 300);
$cache2->files->keyword1 = array("Oh yeah!", 600);
PHP應用$data = __c("memcache")->get("keyword1");
$data = __c("files")->get("keyword2");
$data = __c()->keyword3;
PHP應用// Multiple ? No Problem
PHP應用$list = $cache1->getMulti(array("key1","key2","key3"));
$cache2->setMulti(array("key1","value1", 300),
????????????????? array("key2","value2", 600),
????????????????? array("key3","value3", 1800),
????????????????? );
PHP應用$list = $cache1->apc->getMulti(array("key1","key2","key3"));
__c()->memcache->getMulti(array("a","b","c"));
PHP應用// want more? Check out document in source code
PHP應用希望本文所述對大家的PHP程序設計有所贊助.
維易PHP培訓學院每天發布《PHP教程:PHP緩存集成庫phpFastCache用法》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。