溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

phpFastCache怎么在PHP項(xiàng)目中使用

發(fā)布時間:2020-12-15 16:24:07 來源:億速云 閱讀:160 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)phpFastCache怎么在PHP項(xiàng)目中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

phpFastCache是一個開源的PHP緩存庫,只提供一個簡單的PHP文件,可方便集成到已有項(xiàng)目,支持多種緩存方法,包括:apc, memcache, memcached, wincache, files, pdo and mpdo??赏ㄟ^簡單的API來定義緩存的有效時間。

<?php// In your config fileinclude("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)用性能<?phpinclude("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}

全頁緩存

<?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
// 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;

同時使用多種緩存

<?php
// in your config filesinclude("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

看完上述內(nèi)容,你們對phpFastCache怎么在PHP項(xiàng)目中使用有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI