behance/nbd.php-cache

This package is abandoned and no longer maintained. No replacement package was suggested.

Behance Cache Access Layer

2.4.0 2018-08-30 18:45 UTC

This package is not auto-updated.

Last update: 2018-09-30 19:01:22 UTC


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