Skip to content

Cache::get() returns null for cached false values using phpredis + igbinary serializer #58721

@vitalymalashevsky-pixieset

Description

Laravel Version

11.x - 12.x

PHP Version

8.x

Database Driver & Version

Redis Support => enabled
Redis Version => 6.3.0
Redis Sentinel Version => 1.0
Available serializers => php, json, igbinary
Available compression => lzf, zstd, lz4

Description

When using phpredis with serializers (SERIALIZER_PHP, SERIALIZER_IGBINARY, SERIALIZER_JSON, etc) in Laravel's Cache layer, storing boolean false values results in null being returned when retrieved. This only affects phpredis with certain serializers, not Predis.

Root Cause (potential)

The bug is in Illuminate\Redis\Connections\PhpRedisConnection::get() method. The original code:

public function get($key)
{
    $result = $this->command('get', [$key]);
    return $result !== false ? $result : null;  // ← BUG: Can't distinguish false value from missing key
}

The problem: When phpredis with a serializer retrieves a stored false boolean value, it correctly deserializes and returns the boolean false. However, the code treats false (whether it means "key not found" or "value is false") the same way - both return null.

Steps To Reproduce

Prerequisites

// config/database.php
'redis' => [
    'client' => 'phpredis',
    'options' => [
        'prefix' => 'igb:', // optional
        'serializer' => Redis::SERIALIZER_IGBINARY, // any except Redis::SERIALIZER_NONE
        'compression' => Redis::COMPRESSION_LZ4, // optional
    ],
],

Reproduction Steps

  1. Store false value: Redis::connection()->setex('key_false', 300, false) or Cache::put('key_false', false, 300)
  2. Retrieve via Cache: Cache::get('key_false')
  3. Result: null (incorrect - should be false)
  4. Direct Redis retrieval: Redis::get('key_false') returns false (correct)

Expected Behavior

Cache::get('key') should return the cached false value, not null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions