php缓存集成库phpfastcache用法,phpphpfastcache本文实例讲述了php缓存集成库phpfastcache用法。分享给大家供大家参考。具体分析如下:
phpfastcache是一个开源的php缓存库,只提供一个简单的php文件,可方便集成到已有项目,支持多种缓存方法,包括:apc, memcache, memcached, wincache, files, pdo and mpdo。可通过简单的api来定义缓存的有效时间。
复制代码 代码如下:
// 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调用性能
复制代码 代码如下:
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
}
全页缓存
复制代码 代码如下:
// 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;
同时使用多种缓存
复制代码 代码如下:
// 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
希望本文所述对大家的php程序设计有所帮助。
http://www.bkjia.com/phpjc/928230.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/928230.htmltecharticlephp缓存集成库phpfastcache用法,phpphpfastcache 本文实例讲述了php缓存集成库phpfastcache用法。分享给大家供大家参考。具体分析如下: phpfastcac...