Phpapc cache and comparison with redis

Source: Internet
Author: User
Tags apc apc configuration
: This article mainly introduces the phpapc cache and the comparison with redis. if you are interested in the PHP Tutorial, please refer to it. About APC

APC is called Alternative PHP Cache. it is an open-source php plug-in used to Cache php code or user data.

APC cache of php code

Apc can remove php dynamic parsing and compilation time, and php scripts can be executed faster. The following figure shows why apc can make php scripts run faster.


PHP is a dynamic scripting language. to allow users to view the page content in a browser, the server must parse the php code to generate the desired html code. As shown in the figure above, when there is no apc, the server needs to parse, compile, and execute php for every request to the php page. However, many times, as long as the php code has not changed, the php execution logic will not change. that is to say, the parsing and compilation processes can be omitted, and the results can be directly executed and returned. This is the work of apc. apc caches php executable code, removes the parsing and compilation processes, and speeds up the execution of php scripts.

APC caches user data and

Besides caching php code, apc can also cache user data. However, because apc has a small capacity, it is best to cache data that remains unchanged for a long time, the unchanged unit should be in the unit of day. Therefore, if the data changes frequently and the data volume is large, do not use apc. you can use memcache or redis.

The following is a comparison test experiment between apc and redis. let's take a look at the efficiency comparison between apc and redis.

function getTimeStamp() {  $timestr = microtime();  $timestrary = explode(' ', $timestr);  $result = intval($timestrary[1])*1000 + intval(floatval($timestrary[0])*1000);  return $result;} $redis = new Redis();$redis->connect('127.0.0.1', 6379);$key = 'key';$value ='value';$redis->set($key, $value);  apc_store($key, $value, 1);   $begin = getTimeStamp();for($i = 0 ; $i < 100000 ; $i = $i +1) {  $result = apc_fetch($key);} $cost = getTimeStamp() - $begin;var_dump($cost);$begin = getTimeStamp();for($i = 0 ; $i < 100000 ; $i = $i +1) {  $result = $redis->get($key);} $cost = getTimeStamp() - $begin;var_dump($cost);$redis->close();  

They are all simple data fetch operations, which are performed 0.1 million times respectively, in milliseconds. The final running result is redis10 million reads within 4-5 seconds, while apc gets 0.1 million reads only about 50 milliseconds. We can see that if some data is simply cached, and the data will not change and hit, apc can still improve the efficiency.

When apc is installed, an apc. php page is displayed to view apc hits. For example


APC configuration

The configuration example is as follows:

Extension = apc. so apc. enabled = 1apc. shm_segments = 1 apc. shm_size = 64 M; size of apc memory. The final size must be multiplied by the number of segments. Therefore, 64 M apc is allocated for apc. ttl = 7200 apc. user_ttl = 7200apc. enable_cli = 1; if this parameter is not enabled, you can only perform apc operations on the webpage, but not through cli.

Reference

Http://www.inmotionhosting.com/support/website/what-is/speed-up-php-with-apc

Http://bbs.phpchina.com/thread-202432-1-1.html


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above describes the php apc cache and the comparison with redis, including the content, and hope to be helpful to friends who are interested in PHP tutorials.

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.