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 相关文章推荐
用php实现的下载css文件中的图片的代码
Feb 08 PHP
php 获取当前访问的url文件名的方法小结
Feb 08 PHP
用PHP实现小写金额转换大写金额的代码(精确到分)
Jan 10 PHP
PHP实现的博客欢迎提示功能(很特别哦)
Jun 05 PHP
PHP实现的多彩标签效果代码分享
Aug 21 PHP
php的XML文件解释类应用实例
Sep 22 PHP
php实现在多维数组中查找特定value的方法
Jul 29 PHP
PHP实现导出带样式的Excel
Aug 28 PHP
利用php的ob缓存机制实现页面静态化方法
Jul 09 PHP
PHP面向对象五大原则之开放-封闭原则(OCP)详解
Apr 04 PHP
Laravel中的chunk组块结果集处理与注意问题
Aug 15 PHP
PHP时间函数使用详解
Mar 21 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
为查询结果建立向后/向前按钮
2006/10/09 PHP
php开启与关闭错误提示适用于没有修改php.ini的权限
2014/10/16 PHP
php使用递归函数实现数字累加的方法
2015/03/16 PHP
详解WordPress中用于合成数组的wp_parse_args()函数
2015/12/18 PHP
ImageZoom 图片放大镜效果(多功能扩展篇)
2010/04/14 Javascript
jQuery结合PHP+MySQL实现二级联动下拉列表[实例]
2011/11/15 Javascript
深入理解JavaScript系列(6):S.O.L.I.D五大原则之单一职责SRP
2012/01/15 Javascript
JS逆序遍历实现代码
2014/12/02 Javascript
js实现鼠标移到链接文字弹出一个提示层的方法
2015/05/11 Javascript
Bootstrap网格系统详解
2016/04/26 Javascript
基于jQuery实现页面搜索功能
2020/03/26 Javascript
Angular2中Bootstrap界面库ng-bootstrap详解
2016/10/18 Javascript
jQuery实现根据生日计算年龄 星座 生肖
2016/11/23 Javascript
JavaScript实现汉字转换为拼音的库文件示例
2016/12/22 Javascript
webpack处理 css\less\sass 样式的方法
2017/08/21 Javascript
jQuery中extend函数简单用法示例
2017/10/11 jQuery
详解vue移动端项目的适配(以mint-ui为例)
2018/08/17 Javascript
JavaScript面向对象编程小游戏---贪吃蛇代码实例
2019/05/15 Javascript
JavaScript判断浏览器运行环境的详细方法
2019/06/30 Javascript
vue中uni-app 实现小程序登录注册功能
2019/10/12 Javascript
在Vue中创建可重用的 Transition的方法
2020/06/02 Javascript
Nodejs环境实现socket通信过程解析
2020/07/03 NodeJs
Map与WeakMap类型在JavaScript中的使用详解
2020/11/18 Javascript
[04:52]第二届DOTA2亚洲邀请赛主赛事第一天比赛集锦:OG娜迦海妖放大配合谜团大中3人
2017/04/02 DOTA
[00:56]跨越时空加入战场 全新祈求者身心“失落奇艺侍祭”展示
2019/07/20 DOTA
用python读写excel的方法
2014/11/18 Python
Python制作爬虫抓取美女图
2016/01/20 Python
Python 利用scrapy爬虫通过短短50行代码下载整站短视频
2018/10/29 Python
Python+Kepler.gl轻松制作酷炫路径动画的实现示例
2020/06/02 Python
python-jwt用户认证食用教学的实现方法
2021/01/19 Python
css3 flex实现div内容水平垂直居中的几种方法
2020/03/27 HTML / CSS
HTML5网页音乐播放器的示例代码
2017/11/09 HTML / CSS
实例教程 利用html5和css3打造一款创意404页面
2014/10/20 HTML / CSS
学校隐患排查制度
2015/08/05 职场文书
企业内部管理控制:采购授权审批制度范本
2020/01/19 职场文书
windows10 家庭版下FTP服务器搭建教程
2022/08/05 Servers