aviat / banker
一个实现了 PSR-6 (psr/cache) 和 PSR-16 (psr/simple-cache) 的缓存库
v4.1.2
2023-03-17 02:11 UTC
Requires
- php: >= 8
- ext-json: *
- predis/predis: ^v2.1.2
- psr/cache: ^3.0.0
- psr/log: ^1.1.0 || ^3.0.0
- psr/simple-cache: ^3.0.0
Requires (Dev)
- ext-apcu: *
- ext-memcached: *
- monolog/monolog: ^3.3.1
- phpunit/phpunit: ^10.0.16
Suggests
- ext-apcu: Required for apcu driver
- ext-memcached: Required for Memcached backend
- ext-phpiredis: Improves speed of Redis driver
- monolog/monolog: A good standard logging library
Provides
- psr/cache-implementation: ^3.0.0
- psr/simple-cache-implementation: ^3.0.0
README
实现 PSR-6 和 PSR-16 接口的缓存库,用于多个常见的缓存后端
缓存后端
- Apcu
- Memcached
- Redis
- Null - 无持久化
基本用法(SimpleCache/PSR-16)
<?php
// $config is the configuration array
// $logger is an optional psr/log compatible logger
$cache = new Aviat\Banker\Teller($config, $logger);
// Get a value from the cache, returning $defaultValue
// if the value doesn't exist in the cache
$value = $cache->get($key, $defaultValue);
// Save a new value at the specified key
$saved = $cache->set($key, 'newValue');
基本用法(Pool/PSR-6)
<?php
// Create the pool
// $config is the configuration array
// $logger is an optional psr/log compatible logger
$pool = new Aviat\Banker\Pool($config, $logger);
// Grab an item from the cache
$item = $pool->getItem('foo');
// Was there a cache hit?
if ( ! $item->isHit())
{
// ... Generation of value to cache
$item->set($value);
$item->save();
}
else
{
$value = $item->get();
}
配置 / 连接数组
传递给 Pool 类构造函数的配置数组决定了要连接到哪个服务器。无论后端是什么,基本结构如下
<?php
$config = [
'driver' => 'null', // null, apcu, redis, memcached
'connection' => [
// Optional (For some drivers):
// driver setup, see below for the structure for each
// driver
],
'options' => [
// Optional:
// Set additional driver-specific options, like persistence for
// Memcached, or a prefix for Redis keys
]
];
以下是每个后端的连接数组
Memcached
<?php
$config['connection'] = [
'host' => 'localhost', // hostname or socket
'port' => 11211, // Port needs to be 0 if socket
'persistent' => false, // Use persistent connection
];
Redis:请参阅 Predis 文档。空数组将连接到本地主机上的端口 6379。
Null, Apcu:无连接参数