php 緩存之 APC 和apcu


php opcode 緩存 apc.

其實,我自己的理解, php apc 緩存其實分兩部分,

  一部分是 緩存 類似於 java 編譯的中間的 字節碼, 不同於c 語言編譯之后的二進制的機器碼。 php apc 來緩存php解釋器解析

php產生的 opcode, 哈哈。純屬個人的瞎理解,如有錯誤,請大家指出, 不甚感激。

  還有一部分是 data cache, (key / value map ), 也就是數據緩存, 這點類似於 memerched 和 redis  緩存, 用來存儲數據, 將數據庫或者文件中的數據暫時緩存起來。以

提高訪問速度。

 

APC的介紹

The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

但是據我查的資料, apc 由於嚴重的bug ,php官方已經廢棄了。 出現了一個 apcu , apcu的接口和apc 是一樣的。

這里是一篇介紹的文章,非常不錯 https://www.devside.net/wamp-server/installing-apc-for-php-5-5

這篇文章也是相當的好,大家有時間可以看看:https://support.cloud.engineyard.com/hc/en-us/articles/205411888-PHP-Performance-I-Everything-You-Need-to-Know-About-OpCode-Caches 

 

下面是我自己寫的測試代碼: 注意, 用了一段時間后可能要清除一下緩存,否則會有效率問題, 方法:apc_clear_cache();

<?php
apc_store('name', 'ysr');
$name = apc_fetch('name');
var_dump($name);

  

瀏覽器訪問該頁面,是沒問題的,輸出為:

 

Installing APC for PHP 5.5 and 5.6

If you need to install and enable the php_apc extension for PHP 5.5 or 5.6, there is a way to accomplish this. But before you do this, there are a few things you have to be aware of:

  1. The last PHP version that had the php_apc extension included in was PHP 5.3. * Newer versions of PHP have replaced APC with php_opcache.
  2. The last APC release was php_apc 3.1.14, and while it worked with PHP 5.5, it was immediately removed due to some serious memory issues that could not be fixed. * php_apc 3.1.14 is not available anywhere, it was removed from all official sources.
  3. The only available release of APC is 3.1.13, and while it’s for both PHP 5.3 and 5.4, it’s only non-beta for 5.3 (i.e., not recommended for PHP 5.4).* php_apc 3.1.13 will not work with PHP 5.5+.

Having said that, APC has two parts to it…

  • The opcode cache part that compiles and caches script code.
  • And the data cache part that stores key/value pairs (e.g., just like memcached).

If your scripts require APC, more than likely they only do so because they use APC’s data cache part. And if you want to run those scripts under PHP 5.5 or 5.6, the APCu extension fully replaces APC, and is fully compatible with APC’s API.

APCu is the APC extension with the opcode cache part removed (which was the source of all APC issues) and the data cache part cleaned up. APCu has the same exact functions and configuration directives as APC – so it can be used as a drop-in replacement.

Also, from what I’ve gathered, APCu performs even better than memcache/memcached on single server setups (which is the case 95% of the time if you are using a WAMP such as WampDeveloper Pro).

Installing APCu For PHP 5.5

1. Download the latest build of APCu from –
http://windows.php.net/downloads/pecl/releases/apcu/

 

我下載的是 

php_apcu-4.0.8-5.4-nts-vc9-x86.zip

 

For PHP 5.5 Standard:

php_apcu-4.0.7-5.5-ts-vc11-x86.zip

For PHP 5.5 FCGI:

php_apcu-4.0.7-5.5-nts-vc11-x86.zip

2. Extract out files php_apcu.dll and php_apcu.pdb into the proper PHP version package –

For PHP 5.5 Standard:

C:\WampDeveloper\Versions\Php\php-5.5.24.0-r1-win32-vc11-standard\ext\

For PHP 5.5 FCGI:

C:\WampDeveloper\Versions\Php\php-5.5.24.0-r1-win32-vc11-standard-fcgi\ext\

3. Edit php.ini, near end add section:

[APCu]
extension=php_apcu.dll
apc.enabled=1
apc.shm_size=32M
apc.ttl=7200
apc.enable_cli=1
apc.serializer=php

3. Save file. Restart Apache.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM