behance/nbd.php-cache 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

behance/nbd.php-cache

最新稳定版本:2.4.0

Composer 安装命令:

composer require behance/nbd.php-cache

包简介

Behance Cache Access Layer

README 文档

README

Build Status Dependency Status

behance/nbd.php-cache

Provides basis for communicating with memcache servers, abstracts away interface differences between Memcache, Memcached, and Redis PECL extensions

Goals

  1. Have minimal dependencies, to be used in very diverse environments.
  2. Migration tool: flexibly switch between Memcache, Memcached, and Redis PECL extensions using a single interface
  • Automatically detect PECL extensions and leverage them in priority order (Memcached over Memcache over Redis)
  1. Make every attempt to shield connection and management logic from implementer
  2. Support limited cache "transaction" functionality: Just like an ACID DB transaction, reads + writes only visible single process until committed. Helpful for embedded cache processes that follow actual DB transactions.
  3. Provide deep introspection with events

Implementation Note

  • Redis, at time of writing, connects at the moment of configuration. Until lazy instantiation is fully implemented in the released PECL extension (milestone 3.1.0), initial connection errors are sadly swallowed to work similar to memcache/memcached.

Usage

use Behance\NBD\Cache;

$config = [
  [
    'host' => 'cache1.com',
    'port' => 11211
  ],
  [
    'host' => 'cache2.com',
    'port' => 11212
  ],
  //[
  //  ... add as many servers as necessary
  //]
];

Create an adapter based on the presence of memcache/memcached/redis extensions

$cache = Cache\Factory::create($config);

Or, build a instance of a specific type:

$cache = Cache\Factory::create($config, Factory::TYPE_REDIS);
$cache = Cache\Factory::create($config, Factory::TYPE_MEMCACHE);
$cache = Cache\Factory::create($config, Factory::TYPE_MEMCACHED);

Retrieve a single value

$cache->get('abcdefg');

Retrieve multiple values

$cache->getMulti(['abcdefg', 'hijklmn']); // Result preserves order

Testing

Unit testing, requires memcache, memcached, and redis plugins:

  1. composer install
  2. ./vendor/bin/phpunit

(preferred) Integration testing: leverages docker / docker-compose, using actual service containers for memcache and redis)

  1. (on PHP 7.1) docker-compose build seven && docker-compose run sevenone
  2. (on PHP 7.2) docker-compose build seven && docker-compose run seventwo

Operations

MethodExplanation
get( $key )Retrieves value of $key
getMulti( array $keys )Will return ordered list with all keys defined, set to null if individual is missing
set( $key, $value, $ttl = AdapterInterface::EXPIRATION_DEFAULT )Saves $key to $value
add( $key, $value, $ttl = AdapterInterface::EXPIRATION_DEFAULT )Saves $key to $value, ONLY if $key does NOT exist already
replace( $key, $value, $ttl = AdapterInterface::EXPIRATION_DEFAULT )Saves $value to $key, ONLY if $key already exists
increment( $key, $value = 1 )Increments $key by $value
decrement( $key, $value = 1 )Decrements $key by $value
delete( $key )Removes a single key from server
deleteMulti( array $keys )Removes group of keys from server(s)
beginBufferSimulates a transaction, provides consistent state for current connection
rollbackBufferEnds transaction, without committing results
commitBufferEnds transaction, commits results
flush()Removes all keys from server(s)
getAllKeys()Retrieves the full keylist from server(s)
getStats()Retrieves usage stats from server(s)
bind( $event_name, callable $handler )Provide handlers for cache-specific events
getBoundEvents()Gets a list of the events that are bound
close()Disconnects from active connections

统计信息

  • 总下载量: 20.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 6
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-16