Provides basis for communicating with memcache servers, abstracts away interface differences between Memcache and Memcached PECL extensions
- Very minimal dependencies, to be used in very diverse environments
- 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)
- Make every attempt to shield connection and management logic from implementer
- 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.
- Provide deep introspection with events
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
Unit testing:
composer install
./vendor/bin/phpunit
Integration testing: leveraging Docker, using actual mysql container
docker-compose up -d
docker exec -it nbdphpcache_web_1 /bin/bash
cd /app
./vendor/bin/phpunit
Method | Explanation |
---|---|
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) |
beginBuffer | Simulates a transaction, provides consistent state for current connection |
rollbackBuffer | Ends transaction, without committing results |
commitBuffer | Ends 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 |