PHP缓存集成库phpFastCache用法_php技巧

本文实例讲述了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程序设计有所帮助。

时间: 2024-07-29 06:40:44

PHP缓存集成库phpFastCache用法_php技巧的相关文章

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(&

简介PHP的Yii框架中缓存的一些高级用法_php技巧

页面缓存页面缓存指的是在服务器端缓存整个页面的内容.随后当同一个页面被请求时,内容将从缓存中取出,而不是重新生成. 页面缓存由 yii\filters\PageCache 类提供支持,该类是一个过滤器.它可以像这样在控制器类中使用: public function behaviors() { return [ [ 'class' => 'yii\filters\PageCache', 'only' => ['index'], 'duration' => 60, 'variations'

php动态绑定变量的用法_php技巧

本文实例讲述了php动态绑定变量的用法.分享给大家供大家参考.具体如下: private function bindVars($stmt,$params) { if ($params != null) { $types = ''; //initial sting with types foreach($params as $param) { //for each element, determine type and add if(is_int($param)) { $types .= 'i';

php通过rmdir删除目录的简单用法_php技巧

本文实例讲述了php通过rmdir删除目录的简单用法.分享给大家供大家参考.具体分析如下: php可以通过rmdir()函数删除服务器上的目录,下面代码里用到了is_dir()函数,该函数用于判断指定的字符串是否是目录,删除成功返回True,否则返回False <?php if (!is_dir('exampledir')) { mkdir('exampledir'); } rmdir('exampledir'); ?> 希望本文所述对大家的php程序设计有所帮助. 以上是小编为您精心准备的的

PHP 内存缓存加速功能memcached安装与用法_php技巧

一.memcached 简介在很多场合,我们都会听到 memcached 这个名字,但很多同学只是听过,并没有用过或实际了解过,只知道它是一个很不错的东东.这里简单介绍一下,memcached 是高效.快速的分布式内存对象缓存系统,主要用于加速 WEB 动态应用程序.二.memcached 安装首先是下载 memcached 了,目前最新版本是 1.1.12,直接从官方网站即可下载到 memcached-1.1.12.tar.gz.除此之外,memcached 用到了 libevent,我下载的

PHP PDO函数库详解_php技巧

目前而言,实现"数据库抽象层"任重而道远,使用PDO这样的"数据库访问抽象层"是一个不错的选择. PDO中包含三个预定义的类 PDO中包含三个预定义的类,它们分别是 PDO.PDOStatement 和 PDOException. 一.PDO PDO->beginTransaction() - 标明回滚起始点PDO->commit() - 标明回滚结束点,并执行SQLPDO->__construct() - 建立一个PDO链接数据库的实例PDO-&

PHP面向对象程序设计之接口用法_php技巧

接口是PHP面向对象程序设计中非常重要的一个概念.本文以实例形式较为详细的讲述了PHP接口的用法.具体如下: 接口:interface 在PHP中,我们可以规定,一个对象应该具有哪些公共的外部操作,即可使用interface来规定. 公共的方法就是接口.用于规定一个对象应该用于哪些公共的操作方法(接口),这个也叫接口(公共操作方法的集合) 即:接口(interface结构,公共方法集合) 公共方法(接口方法) 定义:用于限定某个对象所必须拥有的公共操作方法的一种结构,称之为接口(interfac

以文件形式缓存php变量的方法_php技巧

本文实例讲述了以文件形式缓存php变量的方法.分享给大家供大家参考.具体实现方法如下: <?php /* $cache_set = array( //缓存路径 , 最后要加"/" 'cacheRoot'=>'./cache/', //缓存时间 'cacheTime'=>20, //cache type 'cacheType'=>1, //扩展名 'cacheExe'=>'.php' ); $cache = new Cache($cache_set); $a

php源码分析之DZX1.5字符串截断函数cutstr用法_php技巧

本文实例讲述了php源码分析之DZX1.5字符串截断函数cutstr用法.分享给大家供大家参考.具体分析如下: <?php /** * 函数来源DZX1.5,文件所在 /source/function/function_core.php */ define('CHARSET','UTF-8'); function cutstr($string, $length, $dot="...") { if(strlen($string)<=$length) { return $str