定制 phpdot/cache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phpdot/cache

最新稳定版本:v1.1.0

Composer 安装命令:

composer require phpdot/cache

包简介

PSR-16 cache with pluggable drivers. Redis, File, Array, APCu, Null. remember() pattern. Standalone.

README 文档

README

PSR-16 cache with pluggable drivers. Redis, File, Array, APCu, Null. remember() pattern. Standalone.

Install

composer require phpdot/cache

Architecture

graph TD
    S[Store] -->|PSR-16| DI[DriverInterface]
    S -->|remember / rememberForever| DI

    DI --> RD[RedisDriver]
    DI --> FD[FileDriver]
    DI --> AD[ArrayDriver]
    DI --> APD[ApcuDriver]
    DI --> ND[NullDriver]
    DI --> CD[Custom Driver]

    RD -->|serialize| SER[Serializer]
    FD -->|serialize| SER

    style S fill:#2d3748,color:#fff
    style DI fill:#4a5568,color:#fff
    style RD fill:#718096,color:#fff
    style FD fill:#718096,color:#fff
    style AD fill:#718096,color:#fff
    style APD fill:#718096,color:#fff
    style ND fill:#718096,color:#fff
    style CD fill:#718096,color:#fff
    style SER fill:#718096,color:#fff
Loading

Usage

Basic

use PHPdot\Cache\Store;
use PHPdot\Cache\Driver\RedisDriver;

$cache = new Store(new RedisDriver($redis, prefix: 'app:'));

$cache->set('user:1', $userData, 3600);
$user = $cache->get('user:1');
$cache->delete('user:1');
$cache->has('user:1'); // false

Remember pattern

$user = $cache->remember('user:1', 3600, function () use ($db) {
    return $db->table('users')->find(1);
});
// First call: queries DB, caches result
// Subsequent calls: returns from cache

$config = $cache->rememberForever('app:config', fn() => loadConfig());

Swap backends

$cache = new Store(new RedisDriver($redis, prefix: 'app:'));
$cache = new Store(new FileDriver('/var/cache/app'));
$cache = new Store(new ArrayDriver());
$cache = new Store(new ApcuDriver(prefix: 'app:'));
$cache = new Store(new NullDriver());
// Same API, different backend

Batch operations

$cache->setMultiple([
    'user:1' => $user1,
    'user:2' => $user2,
], ttl: 3600);

$users = $cache->getMultiple(['user:1', 'user:2', 'user:3'], default: null);
$cache->deleteMultiple(['user:1', 'user:2']);

LRU eviction (ArrayDriver)

$cache = new Store(new ArrayDriver(maxItems: 1000));
// Evicts least recently used entry when full

Custom driver

use PHPdot\Cache\DriverInterface;

final class MongoDriver implements DriverInterface
{
    // Implement 8 methods: get, set, delete, clear, has,
    // getMultiple, setMultiple, deleteMultiple
}

$cache = new Store(new MongoDriver($collection));

Drivers

Driver Backend Serialization Shared Use case
RedisDriver ext-redis igbinary/serialize Yes Production, distributed
FileDriver Filesystem igbinary/serialize Yes (disk) Single server, no Redis
ArrayDriver PHP array None No (per-worker) Testing, short-lived
ApcuDriver ext-apcu None (SHM) Yes (per-server) Single server, fast reads
NullDriver None None N/A Testing, disabled cache

PSR-16 Compliance

Store implements Psr\SimpleCache\CacheInterface:

  • Key validation: rejects {}()/\@: characters and empty strings
  • TTL normalization: accepts int, DateInterval, or null
  • Negative TTL treated as expired
  • Throws PHPdot\Cache\Exception\InvalidArgumentException for invalid keys

Notes

remember() and null values: PSR-16 cannot distinguish between "key not found" and "key stores null". If a remember() callback returns null, the callback will run on every call. Use false or a sentinel value instead of null for cacheable "empty" results.

Requirements

  • PHP >= 8.3
  • psr/simple-cache ^3.0
  • ext-redis (for RedisDriver)
  • ext-apcu (for ApcuDriver)
  • ext-igbinary (optional, faster serialization)

License

MIT

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固