You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel中通过正则或Like匹配获取Redis缓存键?

Troubleshooting Redis Key Lookup in Laravel

Hey there! Let's work through why your Redis key fetching code isn't working. I've dealt with this exact issue a few times, so here are the most likely fixes and things to check:

1. Don't forget Laravel's cache key prefix

Laravel automatically adds a prefix to all your cache keys (you can find this in config/cache.php under the prefix setting—something like laravel_cache: by default). When you use $redis->keys("*$key_name*"), you're searching for keys without this prefix, so it's no wonder you're not getting results!

Here's how to adjust your code to include the prefix:

$redis = Cache::getRedis();
$prefix = Cache::getPrefix(); // Grab Laravel's configured cache prefix
$keys = $redis->keys("{$prefix}*{$key_name}*"); // Match keys with the prefix included

$result = [];
foreach ($keys as $key) {
    // Optional: Remove the prefix from the returned key names for cleaner output
    $cleanKey = str_replace($prefix, '', $key);
    $result[$cleanKey] = $redis->get($key);
}

return $result;

2. Use scan instead of keys (especially for clusters/large datasets)

The keys command has two big issues:

  • It blocks your Redis server while running, which can cause performance problems if you have a lot of keys.
  • It doesn't work properly in Redis cluster environments (it only searches the current node, not all nodes).

The scan command is the safer, more reliable alternative. Here's how to implement it in Laravel:

$redis = Cache::getRedis();
$prefix = Cache::getPrefix();
$pattern = "{$prefix}*{$key_name}*";

$result = [];
$iterator = null;

do {
    // Scan for matching keys, adjust the 'count' value based on your needs
    $response = $redis->scan($iterator, ['match' => $pattern, 'count' => 100]);
    $iterator = $response[0];
    $keys = $response[1] ?? [];

    foreach ($keys as $key) {
        $cleanKey = str_replace($prefix, '', $key);
        $result[$cleanKey] = $redis->get($key);
    }
} while ($iterator != 0); // Keep scanning until the iterator loops back to 0

return $result;

3. Double-check your $key_name variable

It sounds silly, but sometimes the issue is just a typo or an empty $key_name variable. Add a dd($key_name); before your keys/scan call to make sure you're searching for the right string.

4. Verify you're connected to the correct Redis database

Laravel lets you specify a Redis database index in config/cache.php (the database option, default is 0). If your keys were stored in a different database, your lookup will fail. You can confirm the database with:

echo $redis->getDbNum(); // Check which database your Redis instance is using

If it's wrong, switch to the correct one with $redis->select(1); (replace 1 with your target database ID) before running your key lookup.


内容的提问来源于stack exchange,提问作者Aditi Parikh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:00:43