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程序设计有所帮助。

标签:PHP,缓存,用法
0
投稿

猜你喜欢

  • 用XMLHTTPRequest对象进行客户端验证

    2008-03-03 12:38:00
  • Go 自定义error错误的处理方法

    2024-02-16 09:03:45
  • Python基本知识点总结

    2022-06-04 13:35:49
  • 使用IDLE的Python shell窗口实例详解

    2021-04-15 16:49:09
  • PHP 图片上传代码

    2024-05-22 10:05:49
  • Python利用Turtle绘画简单图形

    2021-12-02 04:59:21
  • 通过屏蔽IP来防止采集

    2007-08-19 15:28:00
  • SQL Server 安全检查列表全攻略

    2008-01-29 13:31:00
  • Python内置方法和属性应用:反射和单例(推荐)

    2022-08-04 03:23:48
  • python通过Seq2Seq实现闲聊机器人

    2021-09-02 13:39:15
  • Oracle数据加载和卸载的实现方法

    2024-01-27 20:19:08
  • 自动定时重启sql server回收内存

    2008-11-26 17:41:00
  • python面向对象编程设计原则之单一职责原则详解

    2022-02-13 20:51:25
  • python实现邮箱发送信息

    2023-02-25 15:11:51
  • python 日志 logging模块详细解析

    2022-06-12 15:11:51
  • Go单元测试工具gomonkey的使用

    2024-05-22 10:19:21
  • Python必备技巧之函数的使用详解

    2021-03-02 11:19:59
  • Numpy 将二维图像矩阵转换为一维向量的方法

    2022-09-21 03:11:01
  • pycharm最新免费激活码至2099年(21.3.18亲测可用)

    2023-10-02 09:53:20
  • Mysql如何巧妙的绕过未知字段名详解

    2024-01-27 16:35:32
  • asp之家 网络编程 m.aspxhome.com