rnr1721/le7-cache

PSR-16 SimpleCache for Le7 框架或任何其他 PHP 项目

1.0.6 2023-05-20 15:53 UTC

This package is auto-updated.

Last update: 2024-09-20 19:04:29 UTC


README

需求

  • PHP 8.1 或更高版本。
  • Composer 2.0 或更高版本。

它能做什么?

  • PSR-16 Simple Cache 的标准实现
  • 开箱即用,可以使用文件系统、内存和会话适配器
  • 作为另一个包(rnr1721/le7-cache-memcache)提供 memcache 和 memcached

安装

composer require rnr1721/le7-cache

它是如何工作的?

use Core\Cache\SCFactoryGeneric;

$cacheFactory = new SCFactoryGeneric();

$cache = $cacheFactory->getFileCache('./cache');

$data = [
    'value1' => 'The 1 value',
    'value2' => 'The 2 value'
    ];

// Put data in cache
// Set cache key, value and time-to-live
$cache->set('mykey', $data, 5000);

// Get value from cache
$result = $cache->get('mykey');

print_r($result);

实现的方法

use Psr\SimpleCache\CacheInterface;

    public function get(string $key, mixed $default = null): mixed;

    public function set(string $key, mixed $value, null|int|DateInterval $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, null|int|\DateInterval $ttl = null): bool;

    public function deleteMultiple(iterable $keys): bool;

    public function has(string $key): bool;

工厂方法

use Core\Interfaces\SCFactory;

    /**
     * Get filesysyem cache
     * @param string $folder Folder for caache
     * @return CacheInterface
     */
    public function getFileCache(string $folder): CacheInterface;

    /**
     * Get memory cache
     * @return CacheInterface
     */
    Public function getMemory(): CacheInterface;

    /**
     * Get Session cache
     * @return CacheInterface
     */
    public function getSession(): CacheInterface;