PHP Cache Integration Library Phpfastcache usage _php tips

Source: Internet
Author: User
Tags apc curl memcached

The example in this article describes the Phpfastcache usage of the PHP cache integration Library. Share to everyone for your reference. The specific analysis is as follows:

Phpfastcache is an open source PHP cache that provides a simple PHP file for easy integration into existing projects and supports a variety of caching methods, including: APC, Memcache, memcached, Wincache, files, PDO and Mpdo. A simple API can be used to define the effective time for caching.

Copy Code code as follows:
<?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 the code when your change your caching system. Or Simple Keep it auto
$cache = Phpfastcache ();

In your Class, functions, PHP Pages
Try to get from Cache a. 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 seconds = minutes
$cache->set ("Product_page", $products, 600);
}

Output Your Contents $products here


improve Curl and API invocation performance
Copy Code code as follows:
<?php
Include ("phpfastcache/phpfastcache.php");

$cache = Phpfastcache ("memcached");

Try to get from Cache a.
$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
}

Full Page Caching

Copy Code code as follows:
<?php
Use the 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
RENDER YOUR PAGE, DB QUERY, WHATEVER
*/

Get HTML webpage
$html = Ob_get_contents ();
Save to Cache minutes
__c ("Files")->set ($keyword _webpage, $html, 1800);
}

Echo $html;

Pendant cache

Copy Code code as follows:
<?php
Use the 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 minutes
$cache->widget_1 = Array ($html, 1800);
}

echo or return your $html;

Use multiple caches simultaneously

Copy Code code as follows:
<?php
In your config files
Include ("phpfastcache/phpfastcache.php");
Auto | Memcache | Files ... etc. 'll is default for $cache = __c ();
Phpfastcache:: $storage = "Auto";

$cache 1 = Phpfastcache ();

$cache 2 = __c ("Memcache");
$server = Array (Array ("127.0.0.1", 11211,100), Array ("128.5.1.3", 11215,80));
$cache 2->option ("Server", $server);

$cache 3 = new Phpfastcache ("APC");

How to Write?
$cache 1->set ("Keyword1", "String|number|array|object", 300);
$cache 2->keyword2 = Array ("Something here", 600);
__c ()->keyword3 = Array ("Array|object", 3600*24);

How to Read?
$data = $cache 1->get ("Keyword1");
$data = $cache 2->keyword2;
$data = __c ()->keyword3;
$data = __c ()->get ("Keyword4");

Free to Travel between any caching methods

$cache 1 = Phpfastcache ("Files");
$cache 1->set ("Keyword1", $value, $time);
$cache 1->memcache->set ("Keyword1", $value, $time);
$cache 1->apc->set ("Whatever", $value, 300);

$cache 2 = __c ("APC");
$cache 2->keyword1 = Array ("So cool", 300);
$cache 2->files->keyword1 = Array ("Oh yeah!", 600);

$data = __c ("Memcache")->get ("Keyword1");
$data = __c ("Files")->get ("Keyword2");
$data = __c ()->keyword3;

Multiple? No Problem

$list = $cache 1->getmulti (Array ("Key1", "Key2", "Key3"));
$cache 2->setmulti (Array ("Key1", "value1", 300),
Array ("Key2", "value2", 600),
Array ("Key3", "Value3", 1800),
);

$list = $cache 1->apc->getmulti (Array ("Key1", "Key2", "Key3"));
__c ()->memcache->getmulti (Array ("A", "B", "C"));

Want more? Check out document in source code

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.