概述
PHP APCu(Advanced and Performance Caching User Cache)是一個(gè)用于共享內(nèi)存的緩存系統(tǒng),它提供了一個(gè)用戶緩存機(jī)制,可以被PHP應(yīng)用程序用來緩存數(shù)據(jù)。APCu是APC(Alternative PHP Cache)的一個(gè)分支,專為PHP 5.5及以上版本設(shè)計(jì),并且不包含APC的OPcache功能。
特性
- 共享內(nèi)存緩存:APCu使用共享內(nèi)存來存儲緩存數(shù)據(jù),這意味著多個(gè)PHP進(jìn)程可以訪問相同的緩存數(shù)據(jù),從而提高性能。
- 用戶緩存:與APC的系統(tǒng)緩存不同,APCu專注于用戶緩存。這意味著它主要用于存儲用戶會話數(shù)據(jù)和應(yīng)用程序級別的緩存,而不是編譯后的PHP代碼。
- 易于使用:APCu提供了一組簡單的函數(shù)來存儲和檢索緩存數(shù)據(jù)。例如:apcu_store()、apcu_fetch()、apcu_delete()等。
- 性能提升:通過緩存經(jīng)常訪問的數(shù)據(jù),APCu可以顯著減少數(shù)據(jù)庫查詢和文件I/O操作,從而提高應(yīng)用程序的性能。
- 內(nèi)存管理:APCu會自動管理緩存的內(nèi)存使用,當(dāng)內(nèi)存不足時(shí),它會根據(jù)需要自動清理舊的緩存數(shù)據(jù)。
- 安全性:APCu的緩存數(shù)據(jù)是進(jìn)程隔離的,這意味著不同的PHP進(jìn)程不能訪問彼此的緩存數(shù)據(jù),從而提高了安全性。
- 配置:可以通過php.ini文件配置APCu的相關(guān)參數(shù),例如緩存大小、清理策略等。
安裝
下載源碼包并解壓
wget https://pecl.php.net/get/apcu-5.1.23.tgztar -zxvf apcu-5.1.23.tgz
編譯
cd apcu-5.1.23/usr/local/php-7.4/bin/phpize
執(zhí)行以下命令
./configure --with-php-config=/usr/local/php-7.4/bin/php-config
可能會報(bào)錯(cuò)
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for cc... ccchecking whether the C compiler works... no
configure: error: in `/home/www/build/apcu-5.1.23':
configure: error: C compiler cannot create executables
See `config.log' for more details
查看錯(cuò)誤日志config.log
compilation terminated.
configure:2894: $? = 1
configure:2914: checking whether the C compiler works
configure:2936: cc conftest.c >&5
cc1: error: /usr/local/include/x86_64-linux-gnu: Permission denied
configure:2940: $? = 1
configure:2978: result: no
configure: failed program was:
| /* confdefs.h */|
| #define PACKAGE_NAME ""
| #define PACKAGE_TARNAME ""
| #define PACKAGE_VERSION ""
| #define PACKAGE_STRING ""
| #define PACKAGE_BUGREPORT ""
| #define PACKAGE_URL ""
| /* end confdefs.h. */
|
| int
| main ()
| {
|
| ;
| return 0;
| }
configure:2983: error: in `/home/www/build/apcu-5.1.23':
configure:2985: error: C compiler cannot create executables
See `config.log' for more details
可以看出error: /usr/local/include/x86_64-linux-gnu: Permission denied
這個(gè)提示語表示沒有權(quán)限操作
解決方案使用sudo
操作解決問題
sudo ./configure --with-php-config=/usr/local/php-7.4/bin/php-config
編譯安裝
sudo make -j4
sudo make install
如果沒有報(bào)錯(cuò),查看擴(kuò)展是否安裝成功
ls -l /usr/local/php-7.4/lib/php/extensions/no-debug-non-zts-20190902/
total 183804
-rwxr-xr-x 1 root root 650472 Jul 24 09:34 apcu.so
-rwxr-xr-x 1 root root 1033840 Mar 17 2021 event.so
-rwxr-xr-x 1 root root 275008 Jul 2 11:01 gmssl.so
-rw-r--r-- 1 root root 131697456 Feb 25 2022 grpc.so
-rwxr-xr-x 1 root root 6252494 Mar 17 2021 opcache.a
-rwxr-xr-x 1 root root 2894784 Mar 17 2021 opcache.so
-rw-r--r-- 1 root root 1274552 Feb 25 2022 protobuf.so
-rwxr-xr-x 1 root root 2215880 Jun 14 19:12 rar.so
-rwxr-xr-x 1 root root 697352 Feb 22 2022 rdkafka.so
-rwxr-xr-x 1 root root 2850040 Mar 17 2021 redis.so
-rwxr-xr-x 1 root root 37484536 May 23 09:58 swoole.so
-rwxr-xr-x 1 root root 24176 May 2 11:38 utils.so
-rwxr-xr-x 1 root root 154120 Apr 21 2023 xhprof.so
-rwxr-xr-x 1 root root 684928 May 2 09:25 zephir_parser.so
配置APCu擴(kuò)展
sudo vim /usr/local/php-7.4/etc/php.ini
增加以下配置
[apcu]
extension = apcu.so
apc.shm_size = 1024M
校驗(yàn)配置是否有效
php -i |grep apcu
apcu
OLDPWD => /home/www/build/apcu-5.1.23/build
PWD => /home/www/build/apcu-5.1.23
$_SERVER['OLDPWD'] => /home/www/build/apcu-5.1.23/build
$_SERVER['PWD'] => /home/www/build/apcu-5.1.23
簡單使用
進(jìn)行讀寫
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$key = 'apcu' . $i;
apcu_add($key, $i);
apcu_fetch($key);
}
echo microtime(true) - $start . PHP_EOL;
apcu_add(key, val, ttl) 設(shè)置值,注意,緩存有值的情況下無法設(shè)置值,類比Redis的setnx,類型支持標(biāo)量、數(shù)組、與對象,這一點(diǎn)非常好。
apcu_fetch(key) 取緩存,獲取不到返回false,并發(fā)情況下容易返回false 執(zhí)行
php apcu.php 0.0011260509490967
Redis壓測對比連接性能
方式 | 輪次 | APCu耗時(shí)(秒) | Redis耗時(shí)(秒) |
---|---|---|---|
只讀 | 10000 | 0.011 | 1.162 |
只寫 | 10000 | 0.012 | 1.062 |
讀寫,一次new Redis | 10000 | 0.011 | 2.117 |
讀寫,多次new Redis | 10000 | 0.011 | 3.646 |
總結(jié):以上為
快易數(shù)據(jù)中心
實(shí)踐結(jié)果,具體性能效果與測試機(jī)器也有一定關(guān)系~