# 🔍 Redis 快速檢查指令清單

### 1. 查看 Redis 整體記憶體使用

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-redis-cli-info-memor"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`redis-cli info memory`</div></div>重點看：

- `used_memory_human` → Redis 目前用多少
- `maxmemory` → 上限
- `mem_fragmentation_ratio` → 記憶體碎片率

---

### 2. 查看資料筆數

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-redis-cli-dbsize"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`redis-cli dbsize`</div></div>顯示 key 數量（Drupal 正常應該幾千～幾萬，不會幾百萬）。

---

### 3. 找出最大 key

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-redis-cli---bigkeys"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`redis-cli --bigkeys`</div></div>會依 key type 顯示「最大 key」與統計資訊，幫助找出異常大的 cache。  
例如看到：

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-biggest-string-found"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`Biggest <span class="hljs-built_in">string</span> found <span class="hljs-string">'cache:default:tags'</span> has <span class="hljs-number">200</span> MB`</div></div>就代表 cache tag key 爆掉。

---

### 4. 檢查 key 記憶體大小

（需要 Redis 4.0+）

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-redis-cli-memory-usa"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`redis-cli memory usage <key>`</div></div>例如：

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-redis-cli-memory-usa-1"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`redis-cli memory usage cache:default:tags`</div></div>會輸出 bytes，用來判斷哪個 key 特別肥。

---

### 5. 抽樣列出 key

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-redis-cli-scan-0-mat"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`redis-cli scan 0 match <span class="hljs-string">"cache:*"</span> count 20`</div></div>可隨機抽樣看看 Drupal 快取 key 的型態。

---

### 6. 查看所有 key 大小排名（進階）

<div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary" id="bkmrk-redis-cli---raw-keys"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`redis-cli --raw keys <span class="hljs-string">"*"</span> | \xargs -L1 redis-cli memory usage | \<span class="hljs-built_in">sort</span> -n | <span class="hljs-built_in">tail</span> -20`</div></div>這會顯示 **最大 20 個 key** 的記憶體用量。

---

### 7. 清理快取

- 清全部 Redis：
    
    <div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
    </div></div></div><div class="overflow-y-auto p-4" dir="ltr">`redis-cli flushall`</div></div>
- 清 Drupal Cache：
    
    <div class="contain-inline-size rounded-2xl relative bg-token-sidebar-surface-primary"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
    </div></div></div><div class="overflow-y-auto p-4" dir="ltr">`drush cr`</div></div>

---

## 🔧 建議排查步驟

1. `redis-cli info memory` → 確認總用量
2. `redis-cli dbsize` → 確認 key 數量
3. `redis-cli --bigkeys` → 找最大 key
4. `redis-cli memory usage <key>` → 精準看佔用記憶體
5. 如果某個 cache tag 或 session 爆大 → 考慮調整 Drupal 的 Redis 設定或加上 `maxmemory` 控制