germania-kg/cachecallable

PSR-6 Cache Item Pools 的可调用便捷包装器:无缝创建、返回和存储您的数据。

2.2.0 2023-01-04 14:41 UTC

README

Packagist PHP version Tests

PSR-6 Cache Item Pools 的可调用便捷包装器:无缝创建、返回和存储您的数据。

缓存业务通常非常相似,可以概括如下

  1. 是否启用缓存? 如果没有,首先删除任何相应的旧条目。
    无论如何创建和返回新内容,最终在这里结束。
  2. 给定项目是否存在? 如果存在,则返回项目内容;
    否则,创建、存储并返回内容。

CacheCallable 类将这些步骤简化为方便且可定制的可调用对象。

使用 Composer 安装

$ composer require germania-kg/cachecallable

示例

尽管这个示例使用了 phpfastcache,但您应该能够传递任何 Cache Item Pool。使用您最喜欢的 PSR-3 Logger;此示例将使用知名的 Monolog

<?php
use phpFastCache\CacheManager;
use Monolog\Logger;
use Germania\Cache\CacheCallable;

// Setup dependencies
$cacheItemPool = CacheManager::getInstance('files', [ options ]);
$lifetime      = 3600;
$monolog       = new Logger( "My App" );
$content_creator = function( $keyword ) {
	return "Cache keyword: " . $keyword;
};


//
// Setup Cache wrapper
// 
$wrapped_cache = new CacheCallable(
	$cacheItemPool,
	$lifetime,
	$creator,
	$monolog // optional
);

// Identifying key. Example for a web page:
$keyword = sha1($_SERVER['REQUEST_URI']);
echo $wrapped_cache( $keyword );

关于缓存键的一些建议

根据 PSR-6 规范,缓存键应限制为 A-Z, a-z, 0-9, _, and . 以确保最大兼容性。因此,如果您从 Symfony Cache component 传递 PSR-6 适配器,则类 CacheCallable 内部将给定的键转换为 MD5 表示形式。

如果您想提供自定义的缓存键创建,可以使用 setCacheKeyCreator 方法,该方法接受任何可调用对象。

$wrapped_cache->setCacheKeyCreator( function($raw) { return sha1($raw); } );

PHP-FIG: PSR-6: 缓存接口

实现库必须支持由 A-Z、a-z、0-9、_ 和 . 组成的字符,以任何顺序在 UTF-8 编码中,长度最长为 64 个字符。实现库可以支持额外的字符和编码或更长的长度,但必须至少支持那个最小值。

Symfony 文档: “缓存项目键和值”

缓存项的键[...]应仅包含字母(A-Z、a-z)、数字(0-9)和 _ 和 . 符号。

缓存寿命

想象一下,一个网页在脚本运行时没有缓存 - 我们设置缓存包装器之后。因此,缓存包装器构造函数还接受一个具有 getValue 方法的 LifeTimeInterface 实现者。

<?php
namespace Germania\Cache;

interface LifeTimeInterface {
	// Return seconds to expiration
	public function getValue();
}

LifeTime 类是实现此接口的简单实现。它还允许您在运行时更改寿命值。由于我们通过引用将其传递给我们的缓存构造函数,因此缓存包装器可以在内容创建后决定是否缓存。

创建寿命对象

<?php
use Germania\Cache\CacheCallable;
use Germania\Cache\LifeTime;

// Setup LifeTime object
$lifetime_object = new LifeTime( 3600 );

// Use Factory method:
$lifetime_object = LifeTime::create( 3600 );

// Create from Lifetime instance
$another_lifetime = new LifeTime( $lifetime_object );
$another_lifetime = LifeTime::create( $lifetime_object );

使用 CacheCallable

<?php
use Germania\Cache\CacheCallable;
use Germania\Cache\LifeTime;

// Taken from example above
$wrapped_cache = new CacheCallable(
	$cacheItemPool,
	$lifetime_object,
	$creator
);

您的日志记录器现在将输出类似以下内容

MyLogger DEBUG Lifetime after content creation: 0
MyLogger NOTICE DO NOT store in cache

如何在脚本运行时更改寿命

实例化后,您可以使用 setValue 方法

<?php
namespace Germania\Cache;

interface LifeTimeInterface {
	// Return seconds to expiration
	public function getValue();
}

LifeTime 类是实现此接口的简单实现。它还允许您在运行时更改寿命值。由于我们通过引用将其传递给我们的缓存构造函数,因此缓存包装器可以在内容创建后决定是否缓存。

创建寿命对象

<?php
use Germania\Cache\CacheCallable;
use Germania\Cache\LifeTime;

// Setup LifeTime object
$lifetime_object = new LifeTime( 3600 );

// Use Factory method:
$lifetime_object = LifeTime::create( 3600 );

// Create from Lifetime instance
$another_lifetime = new LifeTime( $lifetime_object );
$another_lifetime = LifeTime::create( $lifetime_object );

实例化后设置时间值

// Change LifeTime value during runtime, 
// e.g. in router or controller
$lifetime_object->setValue( 0 );

使用 CacheCallable

<?php
use Germania\Cache\CacheCallable;
use Germania\Cache\LifeTime;

// Taken from example above
$wrapped_cache = new CacheCallable(
	$cacheItemPool,
	$lifetime_object,
	$creator
);

您的日志记录器现在将输出类似以下内容

MyLogger DEBUG Lifetime after content creation: 0
MyLogger NOTICE DO NOT store in cache

如何覆盖内容创建

如果您更喜欢单例服务,可以使用带有自定义内容创建参数的 invoke 来调用 CacheCallable 以覆盖默认值

// Default content creator
$default_creator = function($file) {
	return json_decode( file_get_contents($file) );
};

// Setup Service
$wrapped_cache = new CacheCallable(
    $cacheItemPool,
    $lifetime_object,
    $default_creator
);

// Override content creation 
$config = $wrapped_cache("config.json", function( $file ) {
	return array('foo' => 'bar');
};

问题

CacheCallableTest 中的 PSR-6 缓存接口模拟可能需要彻底检查。在 #issue 3 上进行讨论。

开发

$ git clone https://github.com/GermaniaKG/CacheCallable.git
$ cd CacheCallable
$ composer install

单元测试

可以选择将 phpunit.xml.dist 复制到 phpunit.xml 并根据您的需要进行修改,或者保持原样。运行 PhpUnit 测试或 composer 脚本,如下所示

$ composer test
# or
$ vendor/bin/phpunit

有用链接