Installation
composer require edsi-tech/redis-safe-client
Register the service provider in config/app.php:
'providers' => [
// ...
EDSI\RedisSafeClient\RedisSafeClientServiceProvider::class,
],
Basic Configuration Publish the config file:
php artisan vendor:publish --provider="EDSI\RedisSafeClient\RedisSafeClientServiceProvider"
Update config/redis-safe-client.php with your Redis connection details (e.g., redis from Laravel's default config).
First Use Case: Safe Redis Connection Inject the client into a controller/service:
use EDSI\RedisSafeClient\Facades\RedisSafeClient;
public function cacheData()
{
RedisSafeClient::set('key', 'value', 60); // Auto-expiry in seconds
$value = RedisSafeClient::get('key');
return $value; // Returns 'value' or null
}
Atomic Operations Use built-in methods for thread-safe operations:
// Atomic increment with fallback
$count = RedisSafeClient::incr('counter', 1, 10); // Max value: 10
// Conditional set (only if key doesn't exist)
RedisSafeClient::setIfNotExists('lock', 'locked', 30);
Transaction Handling Wrap multiple operations in a transaction:
RedisSafeClient::transaction(function ($redis) {
$redis->set('user:1:online', 'true');
$redis->expire('user:1:online', 300);
});
Pub/Sub with Fallbacks Subscribe with automatic reconnection:
RedisSafeClient::subscribe('channel', function ($message) {
// Handle message
});
Laravel Cache Integration Extend Laravel’s cache with RedisSafeClient for critical operations:
Cache::put('critical:key', $value, now()->addMinutes(10));
// Fallback to RedisSafeClient for atomicity:
RedisSafeClient::set('critical:key', $value, 600);
Queue Job Fallbacks Use RedisSafeClient in queue jobs to ensure data integrity:
public function handle()
{
RedisSafeClient::set('job:'.$this->jobId.':status', 'processing');
// Job logic
}
Rate Limiting Implement rate limiting with atomic checks:
if (RedisSafeClient::incr('rate_limit:'.$ip, 1, 100) === 1) {
// First request in window
}
Connection Resilience
try-catch:
try {
RedisSafeClient::transaction(...);
} catch (\EDSI\RedisSafeClient\Exceptions\RedisSafeException $e) {
Log::error('Redis transaction failed: '.$e->getMessage());
}
Expiry Handling
set() accept expiry in seconds, not minutes/hours. Convert manually:
RedisSafeClient::set('key', 'value', now()->addHours(1)->second); // ❌ Avoid
RedisSafeClient::set('key', 'value', 3600); // ✅ Correct (1 hour in seconds)
Blocking Operations
brPop/blPop may block indefinitely. Use timeouts:
$item = RedisSafeClient::brPop('list', 5); // Timeout after 5 seconds
Enable Logging
Set debug to true in config/redis-safe-client.php to log connection issues:
'debug' => env('REDIS_SAFE_DEBUG', false),
Check Connection Pool
If commands hang, verify the Redis server isn’t overloaded or misconfigured (e.g., maxmemory limits).
Custom Fallbacks Override the default fallback behavior in a service provider:
RedisSafeClient::extend('fallback', function ($connection) {
return new CustomRedisClient($connection);
});
Middleware for Safe Operations Create middleware to wrap Redis calls:
public function handle($next, $request)
{
$response = $next($request);
RedisSafeClient::set('request:'.$request->id.':status', 'completed');
return $response;
}
Monitoring Track Redis operations with Laravel’s monitoring tools:
RedisSafeClient::after(function ($result, $method, $args) {
Log::debug("Redis {$method} called with args: ".json_encode($args));
});
How can I help you explore Laravel packages today?