我正在尝试调试我遇到的一些 Redis 问题,但发现了一些关于 SET 命令的不确定文档。
在我的 Redis 配置中;我有以下几行(片段):
# Note: with all the kind of policies, Redis will return an error on write
# operations, when there are not suitable keys for eviction.
#
# At the date of writing this commands are: set setnx setex append
关于documentation page for the SET command我发现:
Status code reply: always OK since SET can't fail.
对最终行为有什么见解吗?
最佳答案
tl;dr:如果 redis 实例内存不足,SET 将返回错误响应。
据我从 redis.c 中的源代码可以看出,基本上当一个命令被处理时,流程是这样的(伪代码):
IF memory is needed
IF we can free keys
Free keys
Process the command
SET -> process and return OK response
ELSE return error response
ELSE
Process command
SET -> process and return OK response
虽然不完全是这样写的,但基本思路是:在执行命令之前会检查内存,所以即使命令不会失败,如果没有内存也会返回错误响应,不管实际是什么命令的响应。
关于Redis SET命令不能失败,但是可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12911248/