Redis Data Types

Most Basic Data Types in Redis are String, Hashes, Lists, Sets, and Sorted sets.

  • String
    • The max size of a string is a max 512 Megabytes in length.
redis:6379> set user1 Tim
OK
redis:6379> get user1
"Tim"
  • Hashes
    • This is essentially a map of keys and values tied to a key.
    • This is probably the closest thing to a real-world of data that you can get in Redis
redis:6379> hmset user2 fristName Tim lastName williams
OK
redis:6379> hgetall user2 
1) "fristName"
2) "Tim"
3) "lastName"
4) "williams"
  • Lists
    • The lists in Redis are fastest when accessed in order by index.
    • The lists can have duplicate items
redis:6379> lpush users Mike
(integer) 1
redis:6379> lpush users Tim
(integer) 2
redis:6379> lrange users 0 -1
1) "Tim"
2) "Mike"
redis:6379> lrange users 0 2
1) "Tim"
2) "Mike"
  • Sets
    • Sets are unordered. This means the items can be returned in any order.
    • Sets have unique members
    • Sets are similar to lists but they are unique and this is key.
redis:6379> sadd post1 firstpost secondpost
(integer) 2
redis:6379> sadd post1 firstpost
(integer) 0
redis:6379> smembers post1
1) "secondpost"
2) "firstpost"
redis:6379> sadd post1 thirdpost
(integer) 1
redis:6379> smembers post1
1) "secondpost"
2) "firstpost"
3) "thirdpost"
  • Sorted sets
    • Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.
redis:6379> zadd logins 500 1
(integer) 1
redis:6379> zadd logins 600 10
(integer) 1
redis:6379> zadd logins 300 15
(integer) 1
redis:6379> zrange logins 0 -1
1) "15"
2) "1"
3) "10"
redis:6379> zrevrange logins 0 -1
1) "10"
2) "1"
3) "15"

Leave a Reply

Your email address will not be published.

ANOTE.DEV