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学习 计数器实例代码
Jun 15 PHP
PHP类的使用 实例代码讲解
Dec 28 PHP
Apache 配置详解(最好的APACHE配置教程)
Jul 04 PHP
php whois查询API制作方法
Jun 23 PHP
PHP中文分词 自动获取关键词介绍
Nov 13 PHP
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单
May 15 PHP
php抽象类用法实例分析
Jul 07 PHP
PHP解决中文乱码
Apr 28 PHP
php中文乱码问题的终极解决方案汇总
Aug 01 PHP
php实现的三个常用加密解密功能函数示例
Nov 06 PHP
Ajax+PHP实现的分类列表框功能示例
Feb 11 PHP
Laravel5.5 手动分页和自定义分页样式的简单实现
Oct 15 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
yii实现创建验证码实例解析
2014/07/31 PHP
简单实现php上传文件功能
2017/09/21 PHP
Laravel Reponse响应客户端示例详解
2020/09/03 PHP
百度Popup.js弹出框进化版 拖拽小框架发布 兼容IE6/7/8,Firefox,Chrome
2010/04/13 Javascript
在VS2008中使用jQuery智能感应的方法
2010/12/30 Javascript
JQuery的$和其它JS发生冲突的快速解决方法
2014/01/24 Javascript
javascript 拷贝节点cloneNode()使用介绍
2014/04/03 Javascript
node.js中的events.EventEmitter.listenerCount方法使用说明
2014/12/08 Javascript
node.js中的fs.truncate方法使用说明
2014/12/15 Javascript
jquery原理以及学习技巧介绍
2015/11/11 Javascript
JavaScript蒙板(model)功能的简单实现代码
2016/08/04 Javascript
jQuery实现Select左右复制移动内容
2016/08/05 Javascript
JS如何生成一个不重复的ID的函数
2016/12/25 Javascript
利用Plupload.js解决大文件上传问题, 带进度条和背景遮罩层
2017/03/15 Javascript
js判断是否是手机页面
2017/03/17 Javascript
详解Vue使用 vue-cli 搭建项目
2017/04/20 Javascript
Angualrjs 表单验证的两种方式(失去焦点验证和点击提交验证)
2017/05/09 Javascript
react native仿微信PopupWindow效果的实例代码
2017/08/07 Javascript
微信小程序 数据绑定及运算的简单实例
2017/09/20 Javascript
Angular中点击li标签实现更改颜色的核心代码
2017/12/08 Javascript
vue中动态绑定表单元素的属性方法
2018/02/23 Javascript
前端js中的事件循环eventloop机制详解
2019/05/15 Javascript
通过JS深度判断两个对象字段相同
2019/06/14 Javascript
[01:01:25]DOTA2上海特级锦标赛B组资格赛#2 Fnatic VS Spirit第三局
2016/02/27 DOTA
Python3.x爬虫下载网页图片的实例讲解
2018/05/22 Python
使用Python进行体育竞技分析(预测球队成绩)
2019/05/16 Python
用python实现名片管理系统
2020/06/18 Python
详解如何用HTML5 Canvas API控制图片的缩放变换
2016/03/22 HTML / CSS
html5清空画布方法(三种)
2017/10/16 HTML / CSS
浪漫婚礼主持词
2014/03/14 职场文书
社区综治宣传月活动总结
2014/07/02 职场文书
学校做一个有道德的人活动方案
2014/08/23 职场文书
兵马俑导游词
2015/02/02 职场文书
幽灵公主观后感
2015/06/09 职场文书
2017年大学生寒假社会实践活动总结
2016/04/06 职场文书
使用canvas仿Echarts实现金字塔图的实例代码
2021/11/11 HTML / CSS