PHP Cache Integration Library Phpfastcache usage, Phpphpfastcache
This example describes the PHP cache integration Library Phpfastcache usage. Share to everyone for your reference. The specific analysis is as follows:
Phpfastcache is an open source PHP cache library that provides a simple PHP file that can be easily integrated into existing projects and supports multiple caching methods including: APC, Memcache, memcached, Wincache, files, PDO and Mpdo. A simple API can be used to define the effective time of the cache.
Copy the 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 do not need to the change your code, 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 on to cache in seconds = ten minutes
$cache->set ("Product_page", $products, 600);
}
Output Your Contents $products here
Improve Curl and API call performance
Copy the Code code as follows: <?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
}
Full-page Caching
Copy the 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 the 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 | | Widgets | | "Hello World";
Save to Cache minutes
$cache->widget_1 = Array ($html, 1800);
}
echo or return your $html;
Use multiple caches simultaneously
Copy the Code code as follows: <?php
In your config files
Include ("phpfastcache/phpfastcache.php");
Auto | Memcache | Files ... etc. Would be the 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 is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/928230.html www.bkjia.com true http://www.bkjia.com/PHPjc/928230.html techarticle PHP Cache Integration Library Phpfastcache usage, Phpphpfastcache This example describes the PHP cache integration Library Phpfastcache usage. Share to everyone for your reference. The specific analysis is as follows: PHPFASTCAC ...