PHP緩存集成庫(kù)phpFastCache用法
本文實(shí)例講述了PHP緩存集成庫(kù)phpFastCache用法。分享給大家供大家參考。具體分析如下:
phpFastCache是一個(gè)開(kāi)源的PHP緩存庫(kù),只提供一個(gè)簡(jiǎn)單的PHP文件,可方便集成到已有項(xiàng)目,支持多種緩存方法,包括:apc, memcache, memcached, wincache, files, pdo and mpdo??赏ㄟ^(guò)簡(jiǎn)單的API來(lái)定義緩存的有效時(shí)間。
// 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->get("product_page");
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);
}
// Output Your Contents $products HERE
提高cURL和API調(diào)用性能
include("phpfastcache/phpfastcache.php");
$cache = phpFastCache("memcached");
// try to get from Cache first.
$results = $cache->get("identity_keyword")
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);
}
foreach($results as $video) {
// Output Your Contents HERE
}
全頁(yè)緩存
// 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;
掛件緩存
// use Files Cache for Whole Page / Widget
$cache = phpFastCache("files");
$html = $cache->widget_1;
if($html == null) {
$html = Render Your Page || Widget || "Hello World";
// Save to Cache 30 minutes
$cache->widget_1 = array($html, 1800);
}
echo or return your $html;
同時(shí)使用多種緩存
// in your config files
include("phpfastcache/phpfastcache.php");
// auto | memcache | files ...etc. Will be default for $cache = __c();
phpFastCache::$storage = "auto";
$cache1 = phpFastCache();
$cache2 = __c("memcache");
$server = array(array("127.0.0.1",11211,100), array("128.5.1.3",11215,80));
$cache2->option("server", $server);
$cache3 = new phpFastCache("apc");
// How to Write?
$cache1->set("keyword1", "string|number|array|object", 300);
$cache2->keyword2 = array("something here", 600);
__c()->keyword3 = array("array|object", 3600*24);
// How to Read?
$data = $cache1->get("keyword1");
$data = $cache2->keyword2;
$data = __c()->keyword3;
$data = __c()->get("keyword4");
// Free to Travel between any caching methods
$cache1 = phpFastCache("files");
$cache1->set("keyword1", $value, $time);
$cache1->memcache->set("keyword1", $value, $time);
$cache1->apc->set("whatever", $value, 300);
$cache2 = __c("apc");
$cache2->keyword1 = array("so cool", 300);
$cache2->files->keyword1 = array("Oh yeah!", 600);
$data = __c("memcache")->get("keyword1");
$data = __c("files")->get("keyword2");
$data = __c()->keyword3;
// Multiple ? No Problem
$list = $cache1->getMulti(array("key1","key2","key3"));
$cache2->setMulti(array("key1","value1", 300),
array("key2","value2", 600),
array("key3","value3", 1800),
);
$list = $cache1->apc->getMulti(array("key1","key2","key3"));
__c()->memcache->getMulti(array("a","b","c"));
// want more? Check out document in source code
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php中簡(jiǎn)單的對(duì)稱加密算法實(shí)現(xiàn)
最近突發(fā)奇想要往數(shù)據(jù)庫(kù)里保存一些機(jī)密的東西,然后就想著怎么讓別人即使進(jìn)入到了數(shù)據(jù)庫(kù)也看不懂存儲(chǔ)的是什么,那么只有加密了;可是我們自己還要看呢,那只能找一些對(duì)稱加密的算法了,我們想看的時(shí)候再解密回來(lái)。下面就介紹了php中簡(jiǎn)單的對(duì)稱加密算法實(shí)現(xiàn)。2017-01-01
PHP隨機(jī)數(shù) C擴(kuò)展隨機(jī)數(shù)
這篇文章主要介紹了PHP隨機(jī)數(shù),C擴(kuò)展隨機(jī)數(shù)的相關(guān)資料,還為大家介紹了PHP唯一ID生成擴(kuò)展ukey,感興趣的小伙伴們可以參考一下2016-05-05
PHP讀取TXT文本內(nèi)容的五種實(shí)用方法小結(jié)
PHP作為一種流行的服務(wù)器端腳本語(yǔ)言,提供了多種方法來(lái)讀取TXT文本內(nèi)容,本文主要為大家詳細(xì)介紹五種不同的PHP方法,希望對(duì)大家有所幫助2024-01-01
幾個(gè)優(yōu)化WordPress中JavaScript加載體驗(yàn)的插件介紹
這篇文章主要介紹了幾個(gè)優(yōu)化WordPress中JavaScript加載體驗(yàn)的插件,一般來(lái)說(shuō)在WordPress中加載JavaScript最好使用wp_enqueue_script()函數(shù)以減少問(wèn)題提高效率,需要的朋友可以參考下2015-12-12
PHP 將dataurl轉(zhuǎn)成圖片image方法總結(jié)
這篇文章主要介紹了PHP 將dataurl轉(zhuǎn)成圖片image方法的相關(guān)資料,這里提供了兩種方法及實(shí)現(xiàn)方式,需要的朋友可以參考下2016-10-10
PHP封裝的數(shù)據(jù)庫(kù)模型Model類完整示例【基于PDO】
這篇文章主要介紹了PHP封裝的數(shù)據(jù)庫(kù)模型Model類,結(jié)合實(shí)例形式分析了php基于PDO針對(duì)mysql數(shù)據(jù)庫(kù)常見(jiàn)增刪改查、統(tǒng)計(jì)、判斷等相關(guān)操作封裝與使用技巧,需要的朋友可以參考下2019-03-03

