Download the PHP package kode/cache without Composer

On this page you can find all versions of the php package kode/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 cache

Kode Cache

高性能 PHP 缓存组件,支持文件、内存、Redis、Memcached、APCu、SQLite 等多种驱动,可独立使用、框架集成使用或结合 Kode 其他包使用。支持分布式锁、原子计数器、限流器等高级功能。

目录


特性


安装

环境要求

安装命令

推荐安装


快速开始

基本使用

使用 Facade


驱动配置

文件驱动 (默认)

适用于低流量、简单场景、开发测试环境。

特点:

内存驱动 (Array)

适用于单机进程、测试环境、请求内共享数据。不支持持久化。

特点:

Redis 驱动

适用于分布式环境、高性能场景、生产环境。

特点:

Memcached 驱动

适用于高并发、分布式缓存场景,比 Redis 更轻量。

特点:

APCu 驱动

适用于单机环境,无需额外进程,比 Redis 更轻量。

特点:

SQLite 驱动

适用于文件型持久化缓存,无需配置数据库服务器。

特点:

多驱动配置


自定义驱动扩展

可以通过 CacheManager::extend() 方法注册自定义驱动。

创建自定义驱动

注册自定义驱动

注意: 自定义驱动类必须实现 StoreInterface 接口,建议继承 AbstractStore 抽象类。


缓存操作

基础操作

批量操作

自增/自减

适用于计数器场景。

自动获取/设置

不存在则添加


缓存标签

标签用于对缓存进行分组管理,适合批量清除相关缓存。

注意: 标签功能需要在存储中额外维护标签与键的映射关系。


缓存项

用于更精细化地控制缓存项。


序列化器

支持多种序列化方式,可根据需求选择。

序列化器对比:

类型 优点 缺点
PHP 无依赖 性能一般
JSON 可跨语言 不支持资源类型
igbinary 最高效 需要扩展

分布式锁

Redis 分布式锁

适用于多进程、跨机器的分布式场景。

本地锁

适用于单机单进程的同步场景。

协程锁

适用于 Swoole/Fiber/Swow 等协程环境的同步锁,支持协程间协调。

注意: 协程锁需要 kode/context 包支持,如未安装则使用本地静态存储。


原子计数器

基于 Redis 的原子操作,适用于高并发计数。


Redis 连接池管理器

协程安全的 Redis 连接池,支持 Swoole/Fiber/Swow 等协程环境。

注意: 连接池需要 kode/context 包支持协程间连接隔离。


限流器

基于 Redis 的请求限流,使用 kode/limiting 组件实现令牌桶算法。


配置管理


异常处理

使用 kode/exception 组件,遵循统一的异常规范。

异常类说明:

异常类 使用场景
CacheException 缓存操作失败,如文件写入失败、Redis 连接失败
InvalidArgumentException 参数无效或驱动未配置

可选集成: 如果安装了 kode/exception,将使用其作为异常基类。


框架集成

ThinkPHP 8

Laravel

原生 PHP


API 参考

CacheManager

方法 说明 返回值
store($name) 获取指定驱动的缓存实例 StoreInterface
extend($name, $class) 注册自定义驱动 void
get($key, $default) 获取缓存 mixed
set($key, $value, $ttl) 设置缓存 bool
put($key, $value, $ttl) 设置缓存 (显式TTL) bool
has($key) 检查缓存是否存在 bool
delete($key) 删除缓存 bool
pull($key, $default) 获取并删除 mixed
clear() 清空所有缓存 bool
many($keys, $default) 批量获取 iterable
putMany($values, $ttl) 批量设置 bool
remember($key, $callback, $ttl) 不存在时设置 mixed
rememberForever($key, $callback) 永久缓存 mixed
increment($key, $step) 自增 int|false
decrement($key, $step) 自减 int|false
forever($key, $value) 永久缓存 bool
forget($key) 删除缓存 bool
flush() 清空所有缓存 bool
tag($name) 获取标签 Tag

StoreInterface (驱动接口)

方法 说明 返回值
get($key, $default) 获取缓存 mixed
set($key, $value, $ttl) 设置缓存 bool
put($key, $value, $ttl) 设置缓存 bool
delete($key) 删除缓存 bool
has($key) 检查存在 bool
clear() 清空 bool
getMultiple($keys, $default) 批量获取 iterable
setMultiple($values, $ttl) 批量设置 bool
deleteMultiple($keys) 批量删除 bool
pull($key, $default) 获取并删除 mixed
add($key, $value, $ttl) 不存在时添加 bool
increment($key, $step) 自增 int|false
decrement($key, $step) 自减 int|false
forever($key, $value) 永久缓存 bool

AbstractStore (抽象基类)

自定义驱动建议继承此类,只需实现 4 个抽象方法:

抽象方法 说明
getItem($key) 获取存储项
setItem($key, $value, $expire) 设置存储项
deleteItem($key) 删除存储项
clearAll() 清空所有

目录结构


测试

运行所有测试

运行指定测试

显示详细输出

生成覆盖率报告


性能建议

驱动 适用场景 性能 持久化
File 低流量、简单场景、开发测试
Memory 请求内共享、测试 最高
Redis 生产环境、分布式
Memcached 高并发、分布式
APCu 单机高性能缓存 极高
SQLite 轻量持久化、无需数据库

优化建议

  1. 文件驱动: 适用于低流量场景,注意定期清理过期文件
  2. 内存驱动: 适用于请求内共享数据,不支持持久化
  3. Redis 驱动: 适用于生产环境,推荐使用,支持分布式
  4. Memcached 驱动: 适用于高并发分布式缓存,比 Redis 更轻量
  5. APCu 驱动: 适用于单机高性能缓存,无需额外进程
  6. SQLite 驱动: 适用于轻量持久化,无需配置数据库服务器

All versions of cache with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
kode/context Version ^2.3
kode/exception Version ^2.0
kode/limiting Version ^1.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 kode/cache contains the following files

Loading the files please wait ...