Redis store key without a value

牧云@^-^@ 提交于 2019-12-20 16:24:43

问题


When using the redis expire commands like SETEXP & TTL, there are scenarios in which there is no need for the key to hold a value at all, because the time to live acts as such.

However, redis requires any key to have a value.

What would be the most reasonable value to use - if you don't ever want to read it?


回答1:


Who said that you should actually store anything in redis key?

Empty string "" is a perfectly valid value for a redis key, and it's a shortest possible one:

> SET foo ""
OK
> GET foo
""
> BITCOUNT foo
(integer) 0



回答2:


I would store one byte of data that could also be broadly interpreted as "truthy", such as the ASCII character 1.




回答3:


Do you serialize everything coming to and from redis, yourself? If so, you may consider using a sentinel value (like a NONE constant, etc.) which is set to something like 'None'.




回答4:


I would avoid using "". How about simple 0 ?

127.0.0.1:6379> set akey 0
OK
127.0.0.1:6379> memory usage akey
(integer) 48
127.0.0.1:6379> set akey ""
OK
127.0.0.1:6379> memory usage akey
(integer) 50
127.0.0.1:6379>


来源:https://stackoverflow.com/questions/25557250/redis-store-key-without-a-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!