PHP缓存集成库phpFastCache用法

 更新时间:2014年12月15日 12:34:05   投稿:shichen2014  
这篇文章主要介绍了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程序设计有所帮助。

相关文章

  • php中简单的对称加密算法实现

    php中简单的对称加密算法实现

    最近突发奇想要往数据库里保存一些机密的东西,然后就想着怎么让别人即使进入到了数据库也看不懂存储的是什么,那么只有加密了;可是我们自己还要看呢,那只能找一些对称加密的算法了,我们想看的时候再解密回来。下面就介绍了php中简单的对称加密算法实现。
    2017-01-01
  • PHP随机数 C扩展随机数

    PHP随机数 C扩展随机数

    这篇文章主要介绍了PHP随机数,C扩展随机数的相关资料,还为大家介绍了PHP唯一ID生成扩展ukey,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • PHP eval函数使用介绍

    PHP eval函数使用介绍

    eval()函数中的eval是evaluate的简称,这个函数的作用就是把一段字符串当作PHP语句来执行,一般情况下不建议使用容易被黑客利用
    2013-12-12
  • PHP读取TXT文本内容的五种实用方法小结

    PHP读取TXT文本内容的五种实用方法小结

    PHP作为一种流行的服务器端脚本语言,提供了多种方法来读取TXT文本内容,本文主要为大家详细介绍五种不同的PHP方法,希望对大家有所帮助
    2024-01-01
  • php类常量用法实例分析

    php类常量用法实例分析

    这篇文章主要介绍了php类常量用法,实例分析了php中类常量的概念、特性与相关使用技巧,需要的朋友可以参考下
    2015-07-07
  • 几个优化WordPress中JavaScript加载体验的插件介绍

    几个优化WordPress中JavaScript加载体验的插件介绍

    这篇文章主要介绍了几个优化WordPress中JavaScript加载体验的插件,一般来说在WordPress中加载JavaScript最好使用wp_enqueue_script()函数以减少问题提高效率,需要的朋友可以参考下
    2015-12-12
  • php操作redis的常见用法详解

    php操作redis的常见用法详解

    这篇文章主要为大家详细介绍了php操作redis的常见用法的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-11-11
  • PHP 将dataurl转成图片image方法总结

    PHP 将dataurl转成图片image方法总结

    这篇文章主要介绍了PHP 将dataurl转成图片image方法的相关资料,这里提供了两种方法及实现方式,需要的朋友可以参考下
    2016-10-10
  • PHP封装的数据库模型Model类完整示例【基于PDO】

    PHP封装的数据库模型Model类完整示例【基于PDO】

    这篇文章主要介绍了PHP封装的数据库模型Model类,结合实例形式分析了php基于PDO针对mysql数据库常见增删改查、统计、判断等相关操作封装与使用技巧,需要的朋友可以参考下
    2019-03-03
  • PHP验证类的封装与使用方法详解

    PHP验证类的封装与使用方法详解

    这篇文章主要介绍了PHP验证类的封装与使用方法,涉及php针对邮箱、手机号、字符串相关验证操作封装与使用技巧,需要的朋友可以参考下
    2019-01-01

最新评论