laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.38k stars 10.97k forks source link

Cache::many with small integer keys returns an unexpected result #48419

Closed AlexKarpan closed 1 year ago

AlexKarpan commented 1 year ago

Laravel Version

10

PHP Version

8.2

Database Driver & Version

No response

Description

\Cache::many([1,2,3]) for an empty cache returns:

array:3 [
  1 => 2
  2 => 3
  3 => null
]

String keys like this \Cache::many(['1','2','3']) make no difference.

The reason for this result is somewhat related to the implementation of PHP arrays, but the actual confusion happens in Illuminate\Cache\Repository at this place:

protected function handleManyResult($keys, $key, $value)
    {
        // ...
        if (is_null($value)) {
            $this->event(new CacheMissed($key));

            return isset($keys[$key]) ? value($keys[$key]) : null;    // <--- HERE
        }

        // ...
        $this->event(new CacheHit($key, $value));

        return $value;
    }

Steps To Reproduce

  1. On a fresh installation of Laravel, run php artisan tinker
  2. Type in \Cache::many([1,2,3]); and hit enter
  3. The result is:
    = [
    1 => 2,
    2 => 3,
    3 => null,
    ]

    The expected result is:

    = [
    1 => null,
    2 => null,
    3 => null,
    ]
driesvints commented 1 year ago

Hey there,

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.

Thanks!