全部
常见问题
产品动态
精选推荐

PHP源码编译安装APCu扩展,利用APCu实现数据缓存

管理 管理 编辑 删除

概述

PHP APCu(Advanced and Performance Caching User Cache)是一个用于共享内存的缓存系统,它提供了一个用户缓存机制,可以被PHP应用程序用来缓存数据。APCu是APC(Alternative PHP Cache)的一个分支,专为PHP 5.5及以上版本设计,并且不包含APC的OPcache功能。

特性

  1. 共享内存缓存:APCu使用共享内存来存储缓存数据,这意味着多个PHP进程可以访问相同的缓存数据,从而提高性能。
  2. 用户缓存:与APC的系统缓存不同,APCu专注于用户缓存。这意味着它主要用于存储用户会话数据和应用程序级别的缓存,而不是编译后的PHP代码。
  3. 易于使用:APCu提供了一组简单的函数来存储和检索缓存数据。例如:apcu_store()、apcu_fetch()、apcu_delete()等。
  4. 性能提升:通过缓存经常访问的数据,APCu可以显著减少数据库查询和文件I/O操作,从而提高应用程序的性能。
  5. 内存管理:APCu会自动管理缓存的内存使用,当内存不足时,它会根据需要自动清理旧的缓存数据。
  6. 安全性:APCu的缓存数据是进程隔离的,这意味着不同的PHP进程不能访问彼此的缓存数据,从而提高了安全性。
  7. 配置:可以通过php.ini文件配置APCu的相关参数,例如缓存大小、清理策略等。

安装

下载源码包并解压

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

执行以下命令

./configure --with-php-config=/usr/local/php-7.4/bin/php-config

可能会报错

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

查看错误日志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 这个提示语表示没有权限操作

解决方案使用sudo操作解决问题

sudo ./configure --with-php-config=/usr/local/php-7.4/bin/php-config

编译安装

sudo make -j4
sudo make install

如果没有报错,查看扩展是否安装成功

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扩展

sudo vim /usr/local/php-7.4/etc/php.ini

增加以下配置

[apcu]
extension = apcu.so
apc.shm_size = 1024M

校验配置是否有效

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

简单使用

进行读写

$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) 设置值,注意,缓存有值的情况下无法设置值,类比Redis的setnx,类型支持标量、数组、与对象,这一点非常好。
  • apcu_fetch(key) 取缓存,获取不到返回false,并发情况下容易返回false 执行
php apcu.php 0.0011260509490967

Redis压测对比连接性能

方式轮次APCu耗时(秒)Redis耗时(秒)
只读100000.0111.162
只写100000.0121.062
读写,一次new Redis100000.0112.117
读写,多次new Redis100000.0113.646

总结:以上为快易数据中心实践结果,具体性能效果与测试机器也有一定关系~


请登录后查看

快易数据中心 最后编辑于2024-07-27 11:07:12

快捷回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
回复从新到旧

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest==1? '取消推荐': '推荐'}}
{{item.floor}}#
{{item.user_info.title}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

作者 管理员 企业

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}   {{itemc.ip_address}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
回复
回复
501
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

快易数据中心 作者
快易数据中心平台。提供各类数据API接口免费调用服务,通知短信、视频去水印、网安备案查询、ICP备案查询、快递物流查询数据接口应有尽有,所有接口高效稳定。同时提供国内外物理服务器、云服务及SSL单域名和SSL通配符证书。支持在线测试,7×24小时全天候服务,专业的API接口服务商。

回答

2

发布

3

经验

1484

快速安全登录

使用微信扫码登录
{{item.label}} {{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
加精
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 开源下载 CRMEB官方论坛 帮助文档
返回顶部 返回顶部
CRMEB客服