thegallagher/wordpress-psr-cache

dev-master 2017-05-27 07:40 UTC

This package is not auto-updated.

Last update: 2024-09-21 00:19:23 UTC


README

这个库应被视为alpha版本,可能会随时更改。建议在composer中使用时指定commit。

安装

composer require thegallagher/wordpress-psr-cache:dev-master@dev

使用示例

<?php
// Include composer autoloader
require 'vendor/autoload.php';

// PSR-6
use TheGallagher\WordPressPsrCache\CacheItemPool;

$cacheKey = 'the_cache_item_key';
$cacheTtl = 600;
$cacheItemPool = new CacheItemPool();
$cacheItem = $cacheItemPool->getItem($cacheKey);
if (!$cacheItem->isHit()) {
    $value = SomeClass::someLongRunningMethod();
    $cacheItem->set($value);
    $cacheItem->expiresAfter($cacheTtl);
}
echo $cacheItem->get();

// PSR-16
use TheGallagher\WordPressPsrCache\SimpleCache;

$cacheKey = 'the_cache_item_key';
$cacheTtl = 600;
$cache = new SimpleCache();
$value = $cache->get($cacheKey);
if ($value === null) {
    $value = SomeClass::someLongRunningMethod();
    $cache->set($cacheKey, $value, $cacheTtl);
}
echo $value;
?>

注意

  • 存储的值实际上被序列化两次,因为Transient API没有方法来区分存储false和缓存未命中。
  • CacheItemPool::clear()始终失败,因为Transient API不允许清除transients中的所有值。这也适用于默认的SimpleCache::clear(),因为它代理了CacheItemPool

许可证

该库是开源软件,使用MIT许可证授权。