Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing ints using HSET in redis

Tags:

redis

I am using redis to store some numeric values using HSET. Here is an example command I am using

HSET "abc" "field" 123

I'm expecting this to store an integer, but when I do HGETALL, it turns out to be a string:

1) "field"
2) "123"

My two questions are:

  1. Is there any way to store an integer in redis using HSET?
  2. Is there any space saving benefit of doing so?
like image 277
Sam Lee Avatar asked Nov 08 '22 18:11

Sam Lee


1 Answers

From http://redis.io/topics/protocol#integer-reply:

The following commands will reply with an integer reply: SETNX, DEL, EXISTS, INCR, INCRBY, DECR, DECRBY, DBSIZE, LASTSAVE, RENAMENX, MOVE, LLEN, SADD, SREM, SISMEMBER, SCARD.

The reply for HSET is a string, but it doesn't mean that Redis stored your info as such.

From http://redis.io/topics/memory-optimization

Since Redis 2.2 many data types are optimized to use less space up to a certain size. Hashes, Lists, Sets composed of just integers, and Sorted Sets, when smaller than a given number of elements, and up to a maximum element size, are encoded in a very memory efficient way that uses up to 10 times less memory (with 5 time less memory used being the average saving).

Also in this page there are commands to configure Redis to set threshold for that optimization.

like image 194
Niloct Avatar answered Nov 27 '22 16:11

Niloct