nanoblocktech / psr-cache
Luminova 框架的 PSR 缓存。
1.3
2024-05-09 20:04 UTC
Requires
- php: ^8.0
- psr/cache: ^3.0
- psr/simple-cache: ^3.0
README
Luminova 框架的 PSR 缓存 Luminova Framework CachePool
、SimpleCache
。使用此库之前,您需要先安装 Luminova 框架。
安装
通过 Composer
composer require nanoblocktech/psr-cache
使用方法
use \Luminova\Psr\Cache\CachePool; use \Luminova\Psr\Cache\CacheItem; $pool = new CachePool('my_cache', 'my_cache_folder_name'); // Set a cache item $item = $pool->getItem('cache_key'); $item->set('This is my cache data'); $pool->save($item); // Get a cache item $data = $pool->getItem('cache_key')->get(); // Check if the cache item exists if (!$item->isHit()) { $data = databaseLoadData(); $item->expiresAfter(new DateInterval('PT1H')); $item->set($data); $pool->save($item); } else { // Data exists in the cache, use it directly $data = $item->get(); }
CachePool 方法
使用 storage
位置名称和 folder
子目录名称初始化类。
$pool = new CachePool(string $storage = 'psr_cache_storage', string $folder = 'psr');
// Retrieves an item from the cache. $pool->getItem('cache_key'): CacheItem; // Retrieves multiple cache items at once. $pool->getItems(array ['key1', 'key2']): iterable<key, CacheItem>; // Determines whether an item exists in the cache. $pool->hasItem(string 'cache_key'): bool; // Persists a cache item immediately. $pool->save(CacheItemInterface $item): bool; // Save a deferred cache item. $pool->saveDeferred(CacheItemInterface $item): bool; // Commits any deferred cache items. $pool->commit(): bool; // Rollback If any deferred commit failed to save, if you prefer not to recommit $pool->rollback(): bool; // Deletes an item from the cache. $pool->deleteItem(string 'cache_key'): bool; // Deletes multiple items from the cache. $pool->deleteItems(array ['key1', 'key2']): bool; // Clear all cached entries $pool->clear(): bool;
use \Luminova\Psr\Cache\SimpleCache; $simple = new SimpleCache(string 'my_cache', string 'my_cache_folder_name'); // Set a cache item $data = $simple->get('cache_key', 'NO_DATA'); if($item === 'NO_DATA'){ $data = 'This is my cache data'; $simple->set('cache_key', $data); }
SimpleCache 方法
使用 storage
位置名称和 folder
子目录名称初始化类。
$simple = new SimpleCache(string $storage = 'psr_cache_storage', string $folder = 'psr');
// Retrieves an item from the cache. $simple->get(string 'cache_key', mixed 'default value'): mixed; // Retrieves multiple cache items at once. $simple->getMultiple(array ['key1', 'key2'], 'default on empty key value'): iterable<key, mixed>; // Determines whether an item exists in the cache. $simple->has(string 'cache_key'): bool; // Persists a cache item immediately with an optional TTL. $simple->set(string 'cache_key', mixed 'data to save', null|int|DateInterval 60): bool; // Persists a set of key => value pairs in the cache, with an optional TTL. $simple->setMultiple(array ['key1' => 'data 1', 'key2' => 'data 2'], null|int|DateInterval 60): bool; // Deletes an item from the cache. $simple->delete(string 'cache_key'): bool; // Deletes multiple items from the cache. $simple->deleteMultiple(array ['key1', 'key2']): bool; // Clears all cache entries. $simple->clear(): bool;
CacheItem 方法
使用 key
、content
初始化类以保存内容,并可选地指定 hit
状态。
$item = new CacheItem(string $key, mixed $content = null, ?bool $isHit = null);
//Retrieves the key of the cache item. $item->getKey(): string; //Retrieves the value of the cache item. $item->get(): mixed; //Check if the cache item is a hit. $item->isHit(): bool; //Sets the value of the cache item. $item->set(mixed $value): static; //Sets the expiration time of the cache item. $item->expiresAt(?DateTimeInterface $expiration): static; //Sets the expiration time of the cache item relative to the current time. $item->expiresAfter(int|DateInterval|null $time): static;