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 相关文章推荐
弄了个检测传输的参数是否为数字的Function
Dec 06 PHP
深入解析php之apc
May 15 PHP
简单的php缓存类分享     php缓存机制
Jan 22 PHP
PHP中file_get_contents高?用法实例
Sep 24 PHP
使用PHP生成PDF方法详解
Jan 23 PHP
thinkphp autoload 命名空间自定义 namespace
Jul 17 PHP
WordPress中邮件的一些修改和自定义技巧
Dec 15 PHP
将PHP的session数据存储到数据库中的代码实例
Jun 24 PHP
PHP 闭包详解及实例代码
Sep 28 PHP
PHP页面跳转实现延时跳转的方法
Dec 10 PHP
ThinkPHP 模板substr的截取字符串函数详解
Jan 09 PHP
php格式文件打开的四种方法
Feb 24 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导入大量数据到mysql性能优化技巧
2014/12/29 PHP
使用PHP uniqid函数生成唯一ID
2015/11/18 PHP
PHP7+Nginx的配置与安装教程详解
2016/05/10 PHP
PHP获取真实客户端的真实IP
2017/03/07 PHP
php curl操作API接口类完整示例
2019/05/21 PHP
[JS]点出统计器
2020/10/11 Javascript
JavaScript与C# Windows应用程序交互方法
2007/06/29 Javascript
从盛大通行证上摘下来的身份证验证js代码
2011/01/11 Javascript
弹出层之1:JQuery.Boxy (一) 使用介绍
2011/10/06 Javascript
node+express+jade制作简单网站指南
2014/11/26 Javascript
MVC+jQuery.Ajax异步实现增删改查和分页
2020/12/22 Javascript
BootStrap创建响应式导航条实例代码
2016/05/31 Javascript
Node.js环境下JavaScript实现单链表与双链表结构
2016/06/12 Javascript
jQuery Raty 一款不错的星级评分插件
2016/08/24 Javascript
Javascript中this绑定的3种方法与比较
2016/10/13 Javascript
教大家轻松制作Bootstrap漂亮表格(table)
2016/12/13 Javascript
实现点击下箭头变上箭头来回切换的两种方法【推荐】
2016/12/14 Javascript
浅谈JavaScript中promise的使用
2017/01/11 Javascript
javascript表单正则应用
2017/02/04 Javascript
基于JS实现翻书效果的页面切换样式
2017/02/16 Javascript
AngularJS实现根据不同条件显示不同控件
2017/04/20 Javascript
BootStrap的双日历时间控件使用
2017/07/25 Javascript
详解Angular5 服务端渲染实战
2018/01/04 Javascript
vue-cli 默认路由再子路由选中下的选中状态问题及解决代码
2018/09/06 Javascript
VUE2.0 ElementUI2.0表格el-table自适应高度的实现方法
2018/11/28 Javascript
使用python编写批量卸载手机中安装的android应用脚本
2014/07/21 Python
简单介绍Python中的filter和lambda函数的使用
2015/04/07 Python
Python解析树及树的遍历
2016/02/03 Python
python利用OpenCV2实现人脸检测
2020/04/16 Python
python爬虫豆瓣网的模拟登录实现
2019/08/21 Python
在服务器上安装python3.8.2环境的教程详解
2020/04/26 Python
Monnier Freres中文官网:法国领先的奢侈品配饰在线零售商
2017/11/01 全球购物
BISSELL官网:北美吸尘器第一品牌
2019/03/14 全球购物
会计电算化专业个人的自我评价
2013/11/24 职场文书
学雷锋志愿服务月活动总结
2014/03/09 职场文书
大学生心理健康教育心得体会
2016/01/12 职场文书