This project is build upon my first redis open PHP project Redis Adapter. Thanks to it you can use either Predis client or native PHP Redis client in a transparent way.
This implementation is quite safe and rely totally on RESP (REdis Serialization Protocol), implemented by Predis and the Redis PHP extension, through their standard API.
For now some of the reserved charachters ()/@: for the keys are supported entirely, it is an on purpose choice we made because PSR reserved those characters years ago and did nothing concrete with it, or nothing I have heard of.
Moreover there are some real life example where those characters are cool to have (emails, urls, paths, and even redis proposed key format which is considered a good practise, e.g user:123).
Finally, as there are no security constraints not to use those characters we made the choice not to follow PSR on this point and to support those chars ()/@: and we hope it will be well tolerated by the PHP developpers community.
A contrario we decided to not enable withespaces in keys to ease debugging and because it is not a redis standard (std_key:preferred:form) nor in URL RFC definition. And so we thought it as not a good practice, at least, for our use cases.
But thoses choices are not engraved in marble and are totally still on the table to discussion.
If PHP redis is installed
$ apt-get install php8.x-redisThese implementations will use it or fallback on Predis client otherwise.
You can simply use composer to install this library:
composer require llegaz/redis-cache
composer installPSR-6 implementation is my implementation of Caching Interface from the PHP FIG www.php-fig.org/psr/psr-6
It is far from perfect and as of now (first versions of this implementation) you should be aware that pool expiration are available but by pool's fields expiration are not really !
I will try to test and implement a pool key expiration for Valkey.io in a near future but my first draft is: if you expire a pool key it will expire your entire pool SO BE EXTRA CAUTIOUS WITH THAT !
if you expire a pool key it will expire your entire pool SO BE EXTRA CAUTIOUS WITH THAT !
Of course you should do cleaner, proper implementation, the below example is not production ready, it is simplified and given ONLY for the sake of example !
$cache = new LLegaz\Cache\RedisEnhancedCache();
// retrieve user_id as $id
$user = new \LLegaz\Cache\Pool\CacheEntryPool($cache, 'user_data' . $id);
$cart = new \LLegaz\Cache\Pool\CacheEntryPool($cache 'user_cart' . $id);
if ($this->bananaAdded()) {
$item = $cart->getItem('product:banana');
$item->set(['count' => 3, 'unit_price' => .33, 'kg_price' => 1.99, 'total_price' => 0]]); // yeah today bananas are free
$cart->save($item);
$cartItem = $user->getItem('cart');
// increment $cartItem here
$user->save($cartItem);
}foreach ($cart as $item) {
$cart->saveDeferred($item); // items are commited on pool object destruct
}My Simple Cache implementation PHP FIG PSR-16 www.php-fig.org/psr/psr-16
$cache = new LLegaz\Cache\RedisCache();
$cache->selectDatabase(3); // switch database
$cache->set('key', 'mixed value, could be an object or an array');
$cache->get('key'));
$cache->setMultiple(['key1' => [], 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4'], 90); // 1m30s expiration on each key
if ($cache->has('key1') && $cache->has('key4')) {
$array = $cache->getMultiple(['key1', 'key1', 'key4']));
var_dump(count($array)); // int(2)
var_dump($array); // array(2) { ["key1"]=> string(6) "value1" ["key4"]=> string(6) "value4" }
}$cache = new LLegaz\Cache\RedisCache();is equivalent to
$cache = new LLegaz\Cache\RedisCache('127.0.0.1');or
$cache = new LLegaz\Cache\RedisCache('localhost', 6379, null, 'tcp', 0, false); // the nulled field is the Redis password in clear text (here no pwd)$cache = new LLegaz\Cache\RedisCache('localhost', 6379, null, 'tcp', 0, true);We welcome contributions! This project follows Git Flow workflow.
# Create feature branch from develop
git checkout -b feature/my-feature develop
# Make changes and commit
git commit -m "feat: add new feature"
# Push and create Pull Request
git push origin feature/my-featureFor complete guidelines, see CONTRIBUTING.md which covers:
- Git Flow workflow in detail
- Development environment setup
- Testing requirements and commands
- Code quality standards (PSR-12, PHPStan)
- Pull Request process and review timeline
# Install dependencies
composer install
# Run tests
composer test:integration
# Code quality
composer cs:check # Check style
composer cs:fix # Fix style
composer stan # Static analysis
composer quality # Run all checksAutomated testing on:
- π PHP 8.4.x, 8.5.x (latest stable versions)
- π¦ Redis 7.2
- π Both Predis and phpredis adapters
All Pull Requests are automatically tested before merge.
Redis-native PSR-16/PSR-6 for mature developers
Stay tuned, by following me on github, for new features using predis and PHP Redis.
@See you space cowboy... π