Skip to content

Redis WRONGTYPE 操作

错误信息

                WRONGTYPE 操作针对键 持有错误 kind 的值
              

快速摘要

你 tried 到 use 列表 command (e.g., LPUSH) on 键那个持有字符串, or 字符串 command on 设置 — Redis enforces 每-键类型.

为什么发生

  • Using 列表 command on 键那个持有字符串

  • Using hash command on 键那个持有设置

  • Reusing 键 name for 不同数据类型跨越部分的 application

最小示例

✗ 错误代码
SET mykey "hello"
LPUSH mykey "world"
# → WRONGTYPE: mykey is a string, not a list
✓ 修复代码
SET mykey "hello"
LPUSH mylist "world"
# Use a different key for the list

键 mykey 持有字符串, 所以 LPUSH 失败. Using separate 键 for 列表 avoids 类型 conflict.

如何诊断

  • 检查类型的 键: TYPE 键

  • Identify 哪个 command 是 used and 什么类型它 expects

  • 检查如果不同部分的 application use 相同键 for 不同类型

如何修复

  • Use 正确 command for 键's 数据类型

  • 删除键 and recreate 它使用正确类型

  • Use 不同键 name for 新数据类型

如何预防

  • Use consistent 键 naming 约定那个 indicate 类型 (e.g., 用户:1:name for 字符串, 用户:1:会话 for 列表)

  • Document 哪个键 持有哪个类型

  • 检查键 类型之前操作 on 它如果 unsure

相关资源

相关术语

相关课程

← 返回语言