<noframes id="bhrfl"><address id="bhrfl"></address>

    <address id="bhrfl"></address>

    <noframes id="bhrfl"><address id="bhrfl"><th id="bhrfl"></th></address>

    <form id="bhrfl"><th id="bhrfl"><progress id="bhrfl"></progress></th></form>

    <em id="bhrfl"><span id="bhrfl"></span></em>

    全部
    常見問題
    產品動態
    精選推薦

    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}} LV.{{ item.user_info.bbs_level }}

    作者 管理員 企業

    {{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
    {{item.is_suggest == 1? '取消推薦': '推薦'}}
    沙發 板凳 地板 {{item.floor}}#
    {{item.user_info.title || '暫無簡介'}}
    附件

    {{itemf.name}}

    {{item.created_at}}  {{item.ip_address}}
    打賞
    已打賞¥{{item.reward_price}}
    {{item.like_count}}
    {{item.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復

    {{itemc.user_info.nickname}}

    {{itemc.user_name}}

    回復 {{itemc.comment_user_info.nickname}}

    附件

    {{itemf.name}}

    {{itemc.created_at}}
    打賞
    已打賞¥{{itemc.reward_price}}
    {{itemc.like_count}}
    {{itemc.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復
    查看更多
    打賞
    已打賞¥{{reward_price}}
    2302
    {{like_count}}
    {{collect_count}}
    添加回復 ({{post_count}})

    相關推薦

    快速安全登錄

    使用微信掃碼登錄
    {{item.label}} 加精
    {{item.label}} {{item.label}} 板塊推薦 常見問題 產品動態 精選推薦 首頁頭條 首頁動態 首頁推薦
    取 消 確 定
    回復
    回復
    問題:
    問題自動獲取的帖子內容,不準確時需要手動修改. [獲取答案]
    答案:
    提交
    bug 需求 取 消 確 定
    打賞金額
    當前余額:¥{{rewardUserInfo.reward_price}}
    {{item.price}}元
    請輸入 0.1-{{reward_max_price}} 范圍內的數值
    打賞成功
    ¥{{price}}
    完成 確認打賞

    微信登錄/注冊

    切換手機號登錄

    {{ bind_phone ? '綁定手機' : '手機登錄'}}

    {{codeText}}
    切換微信登錄/注冊
    暫不綁定
    亚洲欧美字幕
    CRMEB客服

    CRMEB咨詢熱線 咨詢熱線

    400-8888-794

    微信掃碼咨詢

    CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
    返回頂部 返回頂部
    CRMEB客服