php – phpfastcache 好用的製作快取類別

官網:http://www.phpfastcache.com/

<?php
// 官網的範例
// 引用類別
require_once("../phpfastcache/phpfastcache.php");

// 設定的類型預設可以是 "Auto", "files","sqlite","wincache" ,"apc","memcache","memcached", "xcache"
phpFastCache::setup("storage","auto");

//存放的路徑
phpFastCache::setup("path", "你的路徑"); 


$cache = phpFastCache();

//取得叫做 product_page 的快取
$products = $cache->get("product_page");

//若快取不存在
if($products == null) {
    $products = "DB QUERIES | FUNCTION_GET_PRODUCTS | ARRAY | STRING | OBJECTS";
    
    //寫入相同的關鍵字名稱作為快取, 存活10分鐘
    $cache->set("product_page",$products , 600);
}

// 測試看看
echo $products;

 

<?
//官網提供的所有方法範例
/*
     * List of functions and examples
     */
    require_once("../phpfastcache/phpfastcache.php");
    $cache = phpFastCache();

    // Write into cache
    $cache->set("keyword", "data | array | object", 300);

    // Read from Cache | return null or data
    $data = $cache->get("keyword");
    echo $data;

    // Read object information | value | time from cache
    $object = $cache->getInfo("keyword");
    print_r($object);

    // Delete from cache
    $cache->delete("keyword");

    // Clean up all cache
    $cache->clean();

    // Stats
    $array = $cache->stats();
    print_r($array);

    // Increase and Decrease Cache value - Return  true | false
    $cache->increment("keyword", 1);
    $cache->decrement("keyword", 1);

    // Extend expiring time - Return true | false;
    $cache->touch("keyword", 1000);

    // Check Existing or not - Return true | false;
    $cache->isExisting("keyword");

    // Get & Set Multiple Items
    // Same as above, but input is array();

    $list = $cache->getMulti(array("key1","key2","key3"));

    $list = $cache->getInfoMulti(array("key1","key2","key3"));

    $cache->setMulti(array("key1","value1", 300),
        array("key2","value2", 600),
        array("key3","value3", 1800));

    $cache->deleteMulti(array("key1","key2","key3"));

    $cache->isExistingMulti(array("key1","key2","key3"));

    $cache->touchMulti(array(
                        array("key", 300),
                        array("key2", 400),
                       ));

    $cache->incrementMulti(array(
                            array("key", 1),
                            array("key2", 2),
                        ));

    $cache->decrementMulti(array(
                            array("key", 1),
                            array("key2", 2),
                        ));

在 phpfastcache  地2版提供了快速的寫法

<?php

/*
 * 快捷的寫法更方便
 * 有兩種寫法
 * __c() 與 phpFastCache() 可以在任何地方使用沒有限制
 *
 * 範例 1
 */

require_once("../phpfastcache/phpfastcache.php");

// 取得快取
$products = __c()->my_products;

if($products == null) {
    $products = "ARRAY | OBJECT | FUNCTION GET PRODUCTS";
    // 寫入快取
    __c()->my_products = array($products, 300);
}

echo $products;

/*
 * 範例2 - Short Cut
 */

$products = __c("files")->get("keyword");

// 寫入快取並存活24小時
__c("files")->set("keyword","data | something | array | object", 3600*24);


 

發表迴響