Skip to content

Provides basis for communicating with cache servers

License

Notifications You must be signed in to change notification settings

mlebrun/nbd.php-cache

 
 

Repository files navigation

Build Status Dependency Status

behance/nbd.php-cache

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

Goals


  1. Very minimal dependencies, to be used in very diverse environments
  2. Flexibility to switch between Memcache vs. Memcached PECL extensions using a single interface
  • Automatically detect PECL extensions and leverage them in priority order (Memcached over Memcache)
  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

###Usage

use Behance\NBD\Cache;

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

// Creates an adapter based on the presence of memcache/memcached extensions
$cache = Cache\Factory::create( $config );

// Retrieve a single value
$cache->get( 'abcdefg' );

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

Testing


Unit testing:

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

Integration testing: leveraging Docker, using actual mysql container

  1. docker-compose up -d
  2. docker exec -it nbdphpcache_web_1 /bin/bash
  3. cd /app
  4. ./vendor/bin/phpunit

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
close()Disconnects from active connections

About

Provides basis for communicating with cache servers

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%