firehed / redis-psr16
Redis implementation of PSR-16
Installs: 1 498
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 3
Requires
- php: ^8.0
- ext-redis: ^5.3
- psr/simple-cache: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- phpstan/phpstan: ^1.8
- phpstan/phpstan-strict-rules: ^1.3
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
Provides
- psr/simple-cache-implementation: ^1.0 || ^2.0 || ^3.0
This package is auto-updated.
Last update: 2024-09-14 21:33:55 UTC
README
This is a Redis implementation for the PSR-16 SimpleCache interface.
This library actually uses the multi-key operators supported by Redis (mget
, etc), unlike most other implementations at the time of writing.
Installation and Usage
Install: composer require firehed/redis-psr16
Usage:
$redis = new \Redis(); $redis->connect('yourhost', 6379); $redis->auth(['user' => 'youruser', 'pass' => 'yourpass']); $cache = new \Firehed\Cache\RedisPsr16($redis); // Use like any other PSR-16 implementation
If Redis::OPT_SERIALIZER
is not set (or uses the default Redis::SERIALIZER_NONE
), this library will automatically set it to Redis::SERIALIZER_PHP
.
This will ensure that non-string values are stored and retreived correctly.
Be aware that this means if any object
s are cached, any magic methods related to serialization (__sleep()
, __wakeup()
, __serialize()
, __unserialize()
) will be called during caching operations.
Setting that option to any other value before providing Redis to this library will use the set serializer:
use Firehed\Cache\RedisPsr16; use Redis; // Automatically sets SERIALIZER_PHP: $redis = new Redis(); // connect/auth $cache = new RedisPsr16($redis); // Uses specified SERIALIZER_JSON $redis = new Redis(); // connect/auth $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_JSON); $cache = new RedisPsr16($redis);
Configuration
A runtime mode can be set via the $mode
constructor parameter:
use Firehed\Cache\RedisPsr16; $cache = new RedisPsr16($redis, RedisPsr16::MODE_THROW);
-
RedisPsr16::MODE_THROW
may throw exceptions on network issues (in the same way directly using theRedis
extension can). Exceptions thrown will implementPsr\SimpleCache\CacheException
, per PSR-16 requirements. This will help expose networking issues and may be beneficial for logging and error handling, but does require calling libraries to handle them. This is the default mode. -
RedisPsr16::MODE_FAIL
will prevent exceptions from being thrown. Any error, including networking errors (where theRedis
extension throws) will be treated as a failure. This could result in misleading behavior around cache misses; if it's important for your application to know the difference between "miss" and "Redis unavailable", do not use this mode.
The mode can be adjusted at runtime with RedisPsr16::setMode($mode)
.