mehr-it/php-cache

使用PHP文件作为后端的PSR-16简单缓存实现

1.0.0 2021-04-01 10:31 UTC

This package is auto-updated.

Last update: 2024-09-29 06:02:58 UTC


README

使用PHP文件作为后端实现PSR-16的缓存库。

简介

感谢opcache,使用PHP文件作为缓存后端提高了性能,因为它可以利用PHP的字节码缓存。

此库了解opcache,并且每当文件更改时,也会使opcache无效。

注意:由于opcache在不同PHP SAPI之间不共享,因此缓存数据和锁对于每个SAPI都是独立的。即使清除缓存也只会使当前SAPI的缓存无效。

此库使用flock()进行锁定。

安装

使用composer

composer require mehr-it/php-cache

用法

PhpCache构造函数有以下参数

new PhpCache(

    // The directory to store all cache data and locks
    // in. Will be created if not existing.
    $baseDir,

    // Ensures that all created files have the given
    // permission, eg. 0660 (optional)
    $filePermission,

    // Ensures that all created directories have the 
    // given permission, eg. 0770 (optional)
    $directoryPermission,

    // If set to false, keys are not checked to special 
    // chars according to PSR-16. This improves 
    // performance.
    $strictKeyValidation
)

除了PSR-16接口之外,缓存还实现了创建命名锁的能力

$cache = new PhpCache('/tmp/cache');

// create a lock
$lock = $cache->lock('my_lock', LOCK_SH);

// change locking mode, if needed
$lock->setMode(LOCK_EX);

// release lock
$lock->release();