PHP缓存集成库phpFastCache用法
作者:shichen2014 发布时间:2023-11-14 02:35:19
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- 以前学习ASP.NET MVC时,学习与应用,操作过数据显示,添加,编辑,更新和删除等功能。很多方法是相通的,看自己是怎样来进行方便,快捷,
- 本文实例讲述了Python机器学习库scikit-learn安装与基本使用。分享给大家供大家参考,具体如下:引言scikit-learn是P
- 本例详细介绍了如何在wiondws XP下安装与配置MySQL5.0.37 ,图文并茂,相信对初学mysql的朋友有所帮助。1 点击MySQ
- 开发网站,往往需要用数据库保存数据。我们该如何创建数据库与创建数据表呢?方法其实很简单,下面就由小编教你如何用Navicat for MyS
- 环境准备数据库版本:MySQL 5.7.20-log建表 SQLDROP TABLE IF EXISTS `t_ware_sale_stat
- 防止Application对象在多线程访问中出现错误asp代码处理代码如下(VB):<%Application.Lock()Appli
- 下面写一个给大家做参考啊 create procedure sp_find(pfind varchar(500) BEGIN DECLAR
- 1. 概述对于社区,没有一个明确的定义,有很多对社区的定义,如社区是指在一个网络中,有一组节点,它们彼此都相似,而组内的节点与网络中的其他节
- 有件东西我观察了很多年,那就是很少有开发者会去使用SQL Server中的一个非常有用的东西——EX
- 以前的Dreamweaver中是没有图片处理功能的,即使你要处理也只能使用CSS中的相关滤镜进行一些效
- 从 Google 的一个细节说起:整个虚线框都是“Next”的可点击区域。看似不经意,却直接提升了细节的可用性。其它页码也巧妙地和上面的字母
- 项目场景:在做目标检测时,重新进行标注会耗费大量的时间,如果能够批量对xml中的信息进行修改,那么将会节省大量的时间,接下来将详细介绍如何修
- 音频文件放入和.py文件同级的目录下import winsound # 导入此模块实现声音播放功能import time # 导入此模块,获
- 前言:Requests简介Requests 是使用Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,真正的为
- ASP错误大全 Microsoft VBScript语法错误(0x800A03E9)-->内存不足 Microsoft VBScript语法
- 这篇文章主要介绍了django 简单实现登录验证给你,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- (以下内容部分内容参考了http://adomas.org/javascript-mouse-wheel/ )之前js 仿Photoshop
- 注:可视化工具 Navicat 15 for MySQL当我们某天忘记我们的root用户密码,登录不到数据库时,只需要修改root用户的密码
- 函数函数的英文单词是 Function,这个单词还有着功能的意思。在 Go 语言中,函数是实现某一特定功能的代码块。函数代表着某个功能,可以
- 最近学习javascript,碰到caller和callee的问题,去网上百度了很多。搜到的内容大同小益,整理总结了一下与大家分享。call