Download the PHP package groupbuy/hyperf-memory-cache without Composer

On this page you can find all versions of the php package groupbuy/hyperf-memory-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package hyperf-memory-cache

groupbuy/hyperf-memory-cache

基于 Swoole\Table 的 Hyperf 本地一级缓存(L1),通过注解驱动 AOP 实现透明缓存读写,内置单飞防击穿、TTL 抖动防雪崩、空值缓存防穿透、多表隔离。

特性

安装

快速开始

1. 启用缓存

在 .env 中添加:

2. 发布配置文件(可选)

这会将默认配置发布到 config/autoload/memory_cache.php。

3. 使用注解

读缓存

写失效

重要:MemoryCacheEvict 的 key 模板必须与对应 MemoryCache 完全一致,否则失效不生效。

多表(Channel)

不同业务场景对单值序列化后字节数上限的需求不同——例如商品详情缓存通常比配置缓存大得多。通过 channel 参数将缓存路由到不同的 Swoole\Table 实例,每个表独立配置 max_value_bytes。

配置

注解指定 channel

编程式指定 channel

注解参数

#[MemoryCache]

参数 类型 默认值 说明
key ?string null Key 模板,使用 {argName} 占位符从方法参数按名替换;null 时自动生成 类名::方法名:md5(参数)
ttl ?int null TTL(秒);null 使用配置 default_ttl
cacheNull bool false 是否缓存 null/[]/''/false 等空值(防穿透时打开)
singleFlight bool true 是否启用单飞防击穿;与全局 singleflight.enabled 取 AND
jitter bool true 是否对 TTL 加随机抖动防雪崩
channel string 'default' 缓存表 channel,对应 tables 配置中的 key

#[MemoryCacheEvict]

参数 类型 默认值 说明
key string 必填 要失效的 key 模板,必须与读方法 #[MemoryCache(key: ...)] 一致
alwaysEvict bool false 即便方法抛异常也强制清缓存;默认 false(事务性写优先)
channel string 'default' 缓存表 channel,必须与读方法 #[MemoryCache(channel: ...)] 一致

Key 模板规则

配置项

配置文件:config/autoload/memory_cache.php

全局配置

配置项 默认值 说明
enabled false 总开关(.env: MEMORY_CACHE_ENABLE);实时读取,支持一键回滚
max_key_length 60 Key 超过此长度走 md5
default_ttl 60 注解未声明 TTL 时的默认值(秒)
ttl_jitter 5 TTL 抖动上限(秒)
singleflight.enabled true 单飞防击穿开关
singleflight.wait_timeout 3.0 等待 leader 的协程超时(秒)
serializer auto 序列化器:auto/php/igbinary(.env: MEMORY_CACHE_SERIALIZER)
eviction_policy lru 淘汰策略(.env: MEMORY_CACHE_EVICTION_POLICY);见下方说明
eviction_batch_size 8 淘汰时一次删除的行数
log_value false 是否在 debug 日志中输出 value(默认关闭防敏感数据泄露)

多表配置(tables)

每个 channel 独立定义一个 Swoole\Table 实例:

配置项 默认值 说明
table_name memory_cache Swoole\Table 名称,必须全局唯一
table_size 16384 Table 行数
max_value_bytes 3500 单值序列化后字节数上限;超过不缓存
conflict_proportion 0.2 Table 哈希冲突比例
eviction_policy 全局值 表级淘汰策略,不设置则使用全局 eviction_policy
eviction_batch_size 全局值 表级淘汰批量大小,不设置则使用全局 eviction_batch_size

淘汰策略

策略 eviction_policy 值 get 时写操作 淘汰排序依据 适用场景
真 LRU lru 每次命中更新 last_access_at 列(4 字节) last_access_at 需要 LRU 精度,写开销可接受
概率近似 LRU lru_sampled 约 10% 概率更新 last_access_at 列(4 字节) last_access_at 通用场景,近似 LRU,写开销低
惰性淘汰 lru_lazy 无(纯读) created_at(FIFO 语义) 读密集、表容量充裕、可接受 FIFO

容量估算

调整 table_size 和 max_value_bytes 时注意:

适用场景

✅ 适用

高频读 + 低频写 + 可容忍秒级跨机不一致:

❌ 严禁(事故域)

显式调用(不走 AOP)

MemoryCacheManager 可在不走 AOP 的场景中直接使用:

MemoryCacheManager 方法签名

方法 说明
get(string $key, string $channel = 'default'): array 读取缓存,返回 ['hit' => bool, 'value' => mixed]
set(string $key, mixed $value, int $ttl, bool $jitter = true, string $channel = 'default'): void 写入缓存
delete(string $key, string $channel = 'default'): void 删除缓存
evict(string $key, string $channel = 'default'): void 驱逐缓存(计入 evicts 指标)
remember(string $key, Closure $callback, ?int $ttl = null, bool $cacheNull = false, bool $singleFlight = true, bool $jitter = true, string $channel = 'default'): mixed 缓存回源
clear(string $channel = 'default'): int 清空指定 channel 的所有缓存
clearAll(): int 清空所有 channel 的缓存
snapshot(): array 获取运行时指标快照

CLI 命令

查看当前 L1 缓存配置与指标模板,包含所有 channel 的表状态。

注意:CLI 与 Server 是不同进程,CLI 看不到 Server 的 Swoole\Table 运行时数据。真正的运行时指标请走 Prometheus 接入。

架构说明

Swoole\Table 自动注册

包通过 MemoryCacheTableInitializer 监听 BeforeMainServerStart 事件,在 Server 启动前遍历 tables 配置,自动创建所有 Swoole\Table 实例。无需手动在 swoole_table.php 中添加表定义。

缓存失效语义

升级指南

v1.1.x → v1.2.0

v1.2.0 新增多表(channel)支持,完全向后兼容:

测试

License

MIT


All versions of hyperf-memory-cache with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
hyperf/di Version ~3.1.0
hyperf/memory Version ~3.1.0
hyperf/logger Version ~3.1.0
hyperf/contract Version ~3.1.0
hyperf/engine Version ^2.10
hyperf/framework Version ~3.1.0
ext-swoole Version >=5.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package groupbuy/hyperf-memory-cache contains the following files

Loading the files please wait ...