首頁  >  問答  >  主體

取得Laravel中快取中的所有Redis鍵的方法

Laravel 中的快取外觀似乎不允許您取得目前快取在 Redis 中的所有按鍵。

我想建立一個端點,以便我可以檢索此資訊並了解我的條目是否正常運作。

我嘗試使用 Redis 外觀,但使用以下命令及其各自的錯誤沒有成功

Redis::keys("*");

"Cannot use 'KEYS' with redis-cluster."


Redis::scan("cursor");

"Cannot use 'SCAN' with redis-cluster."

P粉986860950P粉986860950219 天前354

全部回覆(1)我來回復

  • P粉627027031

    P粉6270270312024-02-18 12:01:48

    在Redis、叢集中,如果你有很多key,建議掃描而不是key。 但是,您應該正確使用它。嘗試使用這種方式。

    use Illuminate\Support\Facades\Redis;
    
    $cursor = '0'; // Start with initial cursor
    
    do {
        // Scan for keys with current cursor
        list($cursor, $keys) = Redis::scan($cursor);
    
        foreach ($keys as $key) {
          echo "Key: $key\n";
       }
    } while ($cursor !== '0'); // Continue scanning until cursor is '0'

    參考: Laravel 和 redis 掃描

    #

    回覆
    0
  • 取消回覆