anik/cache

v2.0 2024-03-30 00:23 UTC

This package is auto-updated.

Last update: 2024-08-30 01:32:49 UTC


README

anik/cache codecov PHP Version Require Latest Stable Version

anik/cache 包含了 PSR-6 - 缓存接口PSR-16 - 缓存库的通用接口 的实现,具有以下特点:

  • 空缓存
  • 内存/运行时/数组缓存

用途

  • 当使用需要 psr 缓存的库,但出于某些原因不希望使用缓存时。
  • 当开发一个库,开发者可能不会提供缓存,并且你不想进行 if...else 检查时。
if (!is_null($this->cache)){
    $this->cache->set($key,$value,$ttl);
}

if (!is_null($this->cache)){
    $this->cache->get($key)
}

文档

安装

要安装此包,请运行

composer require anik/cache

使用方法

InMemoryPool & NullPool

所有 *Pool 类都实现了在 PSR-6 中定义的 CacheItemPoolInterface 接口。因此,以下方法可用:

use Psr\Cache\CacheItemInterface;

public function getItem(string $key): CacheItemInterface;
public function getItems(array $keys): CacheItemInterface[];
public function hasItem(string $key): bool;
public function clear(): bool;
public function deleteItem(string $key): bool;
public function deleteItems(array $keys): bool;
public function save(CacheItemInterface $item): bool;
public function saveDeferred(CacheItemInterface $item): bool;
public function commit(): bool;

项目

项目类实现了 \Anik\Cache\Contracts\CacheItemInterface,它是 \Psr\Cache\CacheItemInterface 的扩展。因此,以下方法可用。

public function getKey(): string;
public function get(): mixed;
public function isHit(): bool;
public function set(mixed $value): static;
public function expiresAt(\DateTimeInterface|null $expiration): static;
public function expiresAfter(\DateInterval|int|null$time): static;

public function getExpiration(): ?int;
public function getValue(): mixed;

注意事项

当使用 savesaveDeferred 方法将数据保存到缓存中时,应将 \Anik\Cache\Contracts\CacheItemInterface 传递给这些方法。

PoolAdapter

PoolAdapter 类实现了 CacheItemPoolInterface(PSR-6)和 CacheInterface(PSR-16)接口。因此,以下方法可用。

use Psr\Cache\CacheItemInterface;

public function getItem(string $key): CacheItemInterface;
public function getItems(array $keys): CacheItemInterface[];
public function hasItem(string $key): bool;
public function clear(): bool;
public function deleteItem(string $key): bool;
public function deleteItems(array $keys): bool;
public function save(CacheItemInterface $item): bool;
public function saveDeferred(CacheItemInterface $item): bool;
public function commit(): bool;

public function get(string $key, mixed $default = null): mixed;
public function set(string $key, mixed $value, \DateInterval|int|null $ttl = null): bool;
public function delete(string $key): bool;
public function clear(): bool;
public function getMultiple(iterable $keys, mixed $default = null): iterable;
public function setMultiple(iterable $values, \DateInterval|int|null $ttl = null): bool;
public function deleteMultiple(iterable $keys): bool;
public function has(string $key): bool;

示例

NullPool/InMemoryPool

use Anik\Cache\Item;
use Anik\Cache\Pool\NullPool;

$pool = new NullPool();
// $pool = new NullPool($defaultReturnValue);
// $pool = new InMemoryPool();

// Item to store
$item = new Item('key-1', 'value-1');
// $item = new Item('key-2', 'value-2');

// Item expiration
// $item->expiresAfter(10);
// $item->expiresAt(($now = new DateTimeImmutable())->modify('+100 seconds'))

// save item
$pool->save($item);

// save deferred
$pool->saveDeferred($item);
$pool->commit();

// get item
$item = $pool->getItem('key-1');
$item->isHit();
$item->get();

// get multiple items
$pool->getItems(['key-1', 'key-2']);

// has item
$pool->hasItem('key-1');

// delete item
$pool->deleteItem('key-1');

// delete multiple items
$pool->deleteItems(['key-1', 'key-2']);

// clear pool
$pool->clear();

PoolAdapter

use Anik\Cache\Item;
use Anik\Cache\Pool\NullPool;
use Anik\Cache\PoolAdapter;

// pass the type of pool you want to use
$adapter = new PoolAdapter(new NullPool());
// $adapter = new PoolAdapter(new NullPool($defaultReturnValue));
// $adapter = new PoolAdapter(new InMemoryPool());

// can achieve the same using the helper methods
// $adapter = null_cache();
// $adapter = null_cache($defaultReturnValue);
// $adapter = in_memory_cache();

// Item to store
$item = new Item('key-1', 'value-1');
// $item = new Item('key-2', 'value-2');

// Item expiration
// $item->expiresAfter(10);
// $item->expiresAt(($now = new DateTimeImmutable())->modify('+100 seconds'))

// save/save deferred item
$adapter->save($item);
$adapter->saveDeferred($item);
$adapter->commit();
// Otherwise,
$adapter->set('key-3', 'value-3');

// get item
$item = $adapter->getItem('key-1');
$item->isHit();
$item->get();
// Otherwise
$adapter->get('key-3');

// get multiple items
$adapter->getItems(['key-1', 'key-2']);

// Otherwise
$adapter->getMultiple(['key-1', 'key-4'], 'default-value');

// has item
$adapter->hasItem('key-1');
// otherwise
$adapter->has('key-1');

// delete item
$adapter->deleteItem('key-1');

// delete multiple items
$adapter->deleteItems(['key-1', 'key-2']);
// Otherwise
$adapter->deleteMultiple(['key-1', 'key-2']);

// clear pool
$adapter->clear();