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 Client Laravel Package

allprogrammic/redis-client

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require allprogrammic/redis-client
    

    Register the service provider in config/app.php:

    'providers' => [
        Allprogrammic\RedisClient\RedisServiceProvider::class,
    ],
    
  2. Basic Configuration Publish the config file:

    php artisan vendor:publish --provider="Allprogrammic\RedisClient\RedisServiceProvider" --tag="config"
    

    Edit config/redis-client.php to match your Redis server details (host, port, password, etc.).

  3. First Use Case: Simple Connection Inject the Redis client into a controller or service:

    use Allprogrammic\RedisClient\Facades\RedisClient;
    
    public function testRedis()
    {
        $redis = RedisClient::connection();
        $redis->set('key', 'value');
        $value = $redis->get('key');
        return $value; // Returns 'value'
    }
    

Implementation Patterns

Common Workflows

  1. Connection Management Use named connections for different environments (e.g., redis:cache, redis:session):

    $redis = RedisClient::connection('redis:cache');
    
  2. Pub/Sub Integration Publish messages:

    RedisClient::connection()->publish('channel', 'message');
    

    Subscribe in a separate process (e.g., queue worker):

    RedisClient::connection()->subscribe(['channel'], function ($message) {
        // Handle message
    });
    
  3. Pipeline Operations Batch commands for efficiency:

    $redis = RedisClient::connection();
    $redis->pipeline(function ($pipe) {
        $pipe->set('key1', 'value1');
        $pipe->set('key2', 'value2');
        $pipe->incr('counter');
    });
    
  4. Laravel Integration Use with Laravel’s cache system for Redis-backed storage:

    Cache::store('redis')->put('key', 'value', $seconds);
    
  5. Transactions Execute atomic operations:

    $redis = RedisClient::connection();
    $redis->multi()
          ->set('a', 1)
          ->incr('b')
          ->exec();
    

Gotchas and Tips

Pitfalls

  1. Connection Timeouts

    • Default timeout may be too short for slow networks. Adjust in config:
      'timeout' => 5.0, // seconds
      
    • Use retry() for transient failures:
      RedisClient::connection()->retry(3, function () {
          $redis->get('key');
      });
      
  2. Blocking Commands

    • Commands like BRPOPLPUSH or BLPOP block execution. Run in background processes (e.g., queues) or use non-blocking alternatives.
  3. Serialization Issues

    • Laravel’s default JSON serialization may not work for all Redis data types (e.g., hashes with nested structures). Use Redis::rawCommand() for complex cases:
      Redis::rawCommand('HSET user:1 name "John" age 30');
      
  4. Memory Leaks

    • Unbound subscriptions or large datasets can bloat memory. Clean up with:
      RedisClient::connection()->unsubscribe();
      RedisClient::connection()->flushDb(); // Use cautiously!
      

Debugging

  • Enable Logging Add to config/redis-client.php:

    'log' => [
        'enabled' => true,
        'channel' => 'single',
    ],
    

    Check logs for connection issues or command failures.

  • Check Redis CLI Verify keys/data directly:

    redis-cli KEYS *
    redis-cli GET my_key
    

Extension Points

  1. Custom Commands Extend the client with raw Redis commands:

    RedisClient::connection()->rawCommand('EVAL', $luaScript, 1, 'key');
    
  2. Middleware Add middleware for auth or rate limiting:

    RedisClient::connection()->middleware(function ($command) {
        if ($command === 'DEL') {
            // Custom logic
        }
    });
    
  3. Event Listeners Listen for Redis events (e.g., key expiry) via Redis::on() if the package supports it (check docs for custom events).

  4. Testing Use Redis::fake() (if supported) or mock the connection:

    $this->mock(RedisClient::class)->shouldReceive('get')->andReturn('mocked_value');
    
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky