PHP Cache integration Library phpFastCache usage _ php skills

Source: Internet
Author: User
Tags apc
This article mainly introduces the PHP Cache integration Library phpFastCache usage, including basic usage analysis and Operation examples. it is of great practical value in PHP project development, for more information about PHP Cache integration Library phpFastCache, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

PhpFastCache is an open-source PHP Cache library that provides only one simple PHP file and can be easily integrated into existing projects. it supports multiple caching methods, including apc, memcache, memcached, wincache, files, pdo and mpdo. You can use a simple API to define the cache validity period.

The code is 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 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


Improve cURL and API call performance

The code is 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 cils next time
$ Cache-> set ("identity_keyword", $ results, 24x3600 );
}

Foreach ($ results as $ video ){
// Output Your Contents HERE
}

Full page cache

The code is as follows:

<? 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;

Pendant cache

The code is as follows:

<? 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;

Multiple caches at the same time

The code is as follows:

<? 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 ("127.0.0.1", 11211,100), array ("128.5.1.3 ));
$ 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 ("also 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

I hope this article will help you with PHP programming.

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.