behance / nbd.php-cache
Behance Cache Access Layer
Installs: 20 914
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 6
Forks: 7
Open Issues: 2
Requires
- php: >=7.1
- symfony/event-dispatcher: ~2.6 || ~3.0
Requires (Dev)
- phpunit/phpunit: ~6.0
- squizlabs/php_codesniffer: ^2.5.0
README
behance/nbd.php-cache
Provides basis for communicating with memcache servers, abstracts away interface differences between Memcache, Memcached, and Redis PECL extensions
Goals
- Have minimal dependencies, to be used in very diverse environments.
- Migration tool: flexibly switch between
Memcache
,Memcached
, andRedis
PECL extensions using a single interface
- Automatically detect PECL extensions and leverage them in priority order (Memcached over Memcache over Redis)
- 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
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:
composer install
./vendor/bin/phpunit
(preferred) Integration testing: leverages docker / docker-compose, using actual service containers for memcache and redis)
- (on PHP 7.1)
docker-compose build seven && docker-compose run sevenone
- (on PHP 7.2)
docker-compose build seven && docker-compose run seventwo
Operations
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 |
getBoundEvents() | Gets a list of the events that are bound |
close() | Disconnects from active connections |