Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Redis Safe Client Laravel Package

edsi-tech/redis-safe-client

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require edsi-tech/redis-safe-client
    

    Register the service provider in config/app.php:

    'providers' => [
        // ...
        EDSI\RedisSafeClient\RedisSafeClientServiceProvider::class,
    ],
    
  2. 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).

  3. 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
    }
    

Implementation Patterns

Workflow: Safe Redis Operations

  1. 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);
    
  2. Transaction Handling Wrap multiple operations in a transaction:

    RedisSafeClient::transaction(function ($redis) {
        $redis->set('user:1:online', 'true');
        $redis->expire('user:1:online', 300);
    });
    
  3. Pub/Sub with Fallbacks Subscribe with automatic reconnection:

    RedisSafeClient::subscribe('channel', function ($message) {
        // Handle message
    });
    

Integration Tips

  • 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
    }
    

Gotchas and Tips

Pitfalls

  1. Connection Resilience

    • The package auto-reconnects on failure, but long-running transactions may fail silently. Use try-catch:
      try {
          RedisSafeClient::transaction(...);
      } catch (\EDSI\RedisSafeClient\Exceptions\RedisSafeException $e) {
          Log::error('Redis transaction failed: '.$e->getMessage());
      }
      
  2. Expiry Handling

    • Methods like 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)
      
  3. Blocking Operations

    • brPop/blPop may block indefinitely. Use timeouts:
      $item = RedisSafeClient::brPop('list', 5); // Timeout after 5 seconds
      

Debugging

  • 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).

Extension Points

  1. Custom Fallbacks Override the default fallback behavior in a service provider:

    RedisSafeClient::extend('fallback', function ($connection) {
        return new CustomRedisClient($connection);
    });
    
  2. 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;
    }
    
  3. 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));
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle