rnr1721/le7-cache-memcache

PSR-16 Memcache适配器,适用于Le7框架或任何PHP项目

1.0.6 2023-05-20 15:57 UTC

This package is auto-updated.

Last update: 2024-09-20 18:49:09 UTC


README

使le7-framework或任何PHP项目能够使用memcache或memcached

要求

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

它能做什么?

  • PSR-16 Simple Cache的标准实现
  • 默认支持使用Filesystem、memory和session适配器
  • 使用memcache和memcached进行缓存

安装

composer require rnr1721/le7-cache-memcache

测试

composer test

它是如何工作的?

use Core\Cache\SCFactoryMemcacheGeneric;

$cacheFactory = new SCFactoryMemcacheGeneric();

// For memcache
$cache = $cacheFactory->getMemcache("127.0.0.1", 11211);
// For memcached
$cache = $cacheFactory->getMemcached("127.0.0.1", 11211);

$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\Cache\Interfaces\SCFactoryMemcache;

    /**
     * Get memcache instance of SimpleCache
     * @param string $host Memcache host
     * @param int $port Memcache port
     * @return CacheInterface
     */
    public function getMemcache(string $host = "127.0.0.1", int $port = 11211): CacheInterface;

    /**
     * Get memcached instance of SimpleCache
     * @param string $host Memcached host
     * @param int $port Memcached port
     * @return CacheInterface
     */
    public function getMemcached(string $host = "127.0.0.1", int $port = 11211): CacheInterface;