• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP缓存集成库phpFastCache用法_php技巧

php 搞代码 3年前 (2022-01-26) 31次浏览 已收录 0个评论

本文实例讲述了PHP缓存集成库phpFastCache用法。分享给大家供大家参考。具体分析如下:

phpFastCache是一个开源的PHP缓存库,只提供一个简单的PHP文件,可方便集成到已有项目,支持多种缓存方法,包括:apc, memcache, memcached, wincache, files, pdo and mpdo。可通过简单的API来定义缓存的有效时间。

<?php<br />// In your config file<br />include("phpfastcache/phpfastcache.php");<br />phpFastCache::setup("storage","auto");</p><p>// phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "sqlite" and "xcache"<br />// You don't need to change your code when you change your caching system. Or simple keep it auto<br />$cache = phpFastCache();<i>本文@来#源gaodai$ma#com搞$$代**码网</i><strong>搞代gaodaima码</strong></p><p>// In your Class, Functions, PHP Pages<br />// try to get from Cache first. product_page = YOUR Identity Keyword<br />$products = $cache->get("product_page");</p><p>if($products == null) {<br />    $products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;<br />    // set products in to cache in 600 seconds = 10 minutes<br />    $cache->set("product_page", $products,600);<br />}</p><p>// Output Your Contents $products HERE<br />

提高cURL和API调用性能

<?php<br />include("phpfastcache/phpfastcache.php");</p><p>$cache = phpFastCache("memcached");</p><p>// try to get from Cache first.<br />$results = $cache->get("identity_keyword")</p><p>if($results == null) {<br />    $results = cURL->get("http://www.youtube.com/api/json/url/keyword/page");<br />    // Write to Cache Save API Calls next time<br />    $cache->set("identity_keyword", $results, 3600*24);<br />}</p><p>foreach($results as $video) {<br />    // Output Your Contents HERE<br />}

全页缓存

<?php<br />// use Files Cache for Whole Page / Widget</p><p>// keyword = Webpage_URL<br />$keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);<br />$html = __c("files")->get($keyword_webpage);</p><p>if($html == null) {<br />    ob_start();<br />    /*<br />        ALL OF YOUR CODE GO HERE<br />        RENDER YOUR PAGE, DB QUERY, WHATEVER<br />    */</p><p>    // GET HTML WEBPAGE<br />    $html = ob_get_contents();<br />    // Save to Cache 30 minutes<br />    __c("files")->set($keyword_webpage,$html, 1800);<br />}</p><p>echo $html;

挂件缓存

<?php<br />// use Files Cache for Whole Page / Widget<br />$cache = phpFastCache("files");</p><p>$html = $cache->widget_1;</p><p>if($html == null) {<br />    $html = Render Your Page || Widget || "Hello World";<br />    // Save to Cache 30 minutes<br />    $cache->widget_1 = array($html, 1800);<br />}</p><p>echo or return your $html;

同时使用多种缓存

<?php<br />// in your config files<br />include("phpfastcache/phpfastcache.php");<br />// auto | memcache | files ...etc. Will be default for $cache = __c();<br />phpFastCache::$storage = "auto";</p><p>$cache1 = phpFastCache();</p><p>$cache2 = __c("memcache");<br />$server = array(array("127.0.0.1",11211,100), array("128.5.1.3",11215,80));<br />$cache2->option("server", $server);</p><p>$cache3 = new phpFastCache("apc");</p><p>// How to Write?<br />$cache1->set("keyword1", "string|number|array|object", 300);<br />$cache2->keyword2 = array("something here", 600);<br />__c()->keyword3 = array("array|object", 3600*24);</p><p>// How to Read?<br />$data = $cache1->get("keyword1");<br />$data = $cache2->keyword2;<br />$data = __c()->keyword3;<br />$data = __c()->get("keyword4");</p><p>// Free to Travel between any caching methods</p><p>$cache1 = phpFastCache("files");<br />$cache1->set("keyword1", $value, $time);<br />$cache1->memcache->set("keyword1", $value, $time);<br />$cache1->apc->set("whatever", $value, 300);</p><p>$cache2 = __c("apc");<br />$cache2->keyword1 = array("so cool", 300);<br />$cache2->files->keyword1 = array("Oh yeah!", 600);</p><p>$data = __c("memcache")->get("keyword1");<br />$data = __c("files")->get("keyword2");<br />$data = __c()->keyword3;</p><p>// Multiple ? No Problem</p><p>$list = $cache1->getMulti(array("key1","key2","key3"));<br />$cache2->setMulti(array("key1","value1", 300),<br />                  array("key2","value2", 600),<br />                  array("key3","value3", 1800),<br />                  );</p><p>$list = $cache1->apc->getMulti(array("key1","key2","key3"));<br />__c()->memcache->getMulti(array("a","b","c"));</p><p>// want more? Check out document in source code

希望本文所述对大家的PHP程序设计有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP缓存集成库phpFastCache用法_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址