PHP缓存集成库phpFastCache用法


Posted in PHP onDecember 15, 2014

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

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

<?php

// 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调用性能
<?php

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

}

全页缓存

<?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 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程序设计有所帮助。

PHP 相关文章推荐
杏林同学录(七)
Oct 09 PHP
用PHP+java实现自动新闻滚动窗口
Oct 09 PHP
PHP base64+gzinflate压缩编码和解码代码
Oct 03 PHP
深入解析PHP 5.3.x 的strtotime() 时区设定 警告信息修复
Aug 05 PHP
php中调用其他系统http接口的方法说明
Feb 28 PHP
JavaScript创建命名空间的5种写法
Jun 24 PHP
thinkphp实现like模糊查询实例
Oct 29 PHP
php格式输出文件var_export函数实例
Nov 15 PHP
搭建Vim为自定义的PHP开发工具的一些技巧
Dec 11 PHP
php使用number_format函数截取小数的方法分析
May 27 PHP
php根据年月获取当月天数及日期数组的方法
Nov 30 PHP
PHP+redis实现的限制抢购防止商品超发功能详解
Sep 19 PHP
php图片的二进制转换实现方法
Dec 15 #PHP
php第一次无法获取cookie问题处理
Dec 15 #PHP
php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法
Dec 15 #PHP
php实现按指定大小等比缩放生成上传图片缩略图的方法
Dec 15 #PHP
php实现可用于mysql,mssql,pg数据库操作类
Dec 13 #PHP
PHP中Memcache操作类及用法实例
Dec 12 #PHP
PHP实现PDO的mysql数据库操作类
Dec 12 #PHP
You might like
php imagecreatetruecolor 创建高清和透明图片代码小结
2010/05/15 PHP
实战mysql导出中文乱码及phpmyadmin导入中文乱码的解决方法
2010/06/11 PHP
PHP setcookie设置Cookie用法(及设置无效的问题)
2011/07/13 PHP
php扩展开发入门demo示例
2019/09/23 PHP
正则表达式判断是否存在中文和全角字符和判断包含中文字符串长度
2008/09/27 Javascript
CCPry JS类库 代码
2009/10/30 Javascript
用js实现计算加载页面所用的时间
2010/04/02 Javascript
关于js中alert弹出窗口文本换行问题简单详细说明
2012/12/11 Javascript
jquery删除提示框弹出是否删除对话框
2014/01/07 Javascript
深入剖析JavaScript中的枚举功能
2014/03/06 Javascript
js中实现多态采用和继承类似的方法
2014/08/22 Javascript
JS函数修改html的元素内容,及修改属性内容的方法
2016/10/28 Javascript
JS变量及其作用域
2017/03/29 Javascript
JS验证全角与半角及相互转化的介绍
2017/05/18 Javascript
使用Require.js封装原生js轮播图的实现代码
2017/06/15 Javascript
Vue2 Vue-cli中使用Typescript的配置详解
2017/07/24 Javascript
inner join 内联与left join 左联的实例代码
2017/09/18 Javascript
了解ESlint和其相关操作小结
2018/05/21 Javascript
详解webpack-dev-server使用方法
2018/09/14 Javascript
JavaScript中concat复制数组方法浅析
2019/01/20 Javascript
jQuery AJAX与jQuery事件的分析讲解
2019/02/18 jQuery
微信小程序身份证验证方法实现详解
2019/06/28 Javascript
微信小程序iBeacon测距及稳定程序的实现解析
2019/07/31 Javascript
[58:46]OG vs NAVI 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
python通过ftplib登录到ftp服务器的方法
2015/05/08 Python
详解Python装饰器由浅入深
2016/12/09 Python
python利用Tesseract识别验证码的方法示例
2019/01/21 Python
python可视化实现KNN算法
2019/10/16 Python
国际贸易专业求职信
2014/06/04 职场文书
2014年帮扶工作总结
2014/11/26 职场文书
物业工程部主管岗位职责
2015/04/16 职场文书
消防宣传语大全
2015/07/13 职场文书
2019垃圾分类宣传口号汇总
2019/08/16 职场文书
python前后端自定义分页器
2022/04/13 Python
html中相对位置与绝对位置的具体使用
2022/05/15 HTML / CSS
Python使用pandas导入xlsx格式的excel文件内容操作代码
2022/12/24 Python