libertyphp/apcu-cache

使用APCu的PSR-6和PSR-16缓存实现。PHP中最快的缓存。

1.0 2022-10-30 08:12 UTC

This package is auto-updated.

Last update: 2024-09-29 05:57:15 UTC


README

使用PSR-6PSR-16缓存实现,基于APCu。APCu是PHP的内存键值存储。键是字符串类型,值可以是任何PHP变量。

为什么选择APCu缓存?

网络

APCu是非分布式缓存,只能在HTTP请求中使用(在PHP CLI中不可用)。如果你有一个运行在不同Web服务器上的Web应用程序,最好使用APCu缓存,因为它是最简单的缓存,不需要网络请求。

速度

APCu比Memcached和Redis更快。在Intel Core i5-8250U、DDR4 2400MHz上的10000个字符串值的设置和获取基准结果

APCu
set 0.01187s
get 0.00633s

Memcached
set 0.66142s
get 0.63786s

Redis
set 0.37687s
get 0.39227s

因此,如果你的Web应用程序对缓存进行大量请求,最好在每个工作进程上都有一个预热好的APCu缓存。

安装

composer require libertyphp/apcu-cache

Docker配置

如果你使用php-fpm Docker,请将以下代码添加到PHP中安装APCu

ARG APCU_VERSION=5.1.21

RUN pecl install apcu-${APCU_VERSION} && docker-php-ext-enable apcu
RUN echo "apc.shm_size=1024M" >> /usr/local/etc/php/php.ini
RUN echo "apc.ttl=0" >> /usr/local/etc/php/php.ini
RUN echo "apc.gc_ttl=0" >> /usr/local/etc/php/php.ini

使用PSR-6实现

$cachePool = new ApcuCacheItemPool();
$cacheItem = (new ApcuCacheItem('u1030_rating', 98))
    ->expiresAfter(3600);

$cachePool->save($cacheItem);

使用PSR-16实现

$cache = new ApcuCache();
$cache->set('u1030_rating', 98, 3600);

版权

版权(c)2022 Vladimir Lila。有关详细信息,请参阅LICENSE。