DEV Community

Roby Cigar
Roby Cigar

Posted on • Updated on

Catatan Belajar Redis - Data Types

Data types

Redis mempunyai 7 tipe data, yaitu
String, Lists, Sets, Sorted Sets, Hashes & Bitmaps, and Hyperlogs.

Basic Commands

String

note: user adalah namespace lalu 1 adalah unique keynya

set user:1:name Cigar
get user:1:name
// Cigar
Enter fullscreen mode Exit fullscreen mode

Lists

  • Lists of strings.
  • Items can either append to start or end(from left or right).
lpush blog:1:tags laravel php redis
lrange blog:1:tags 0 -1
// laravel php redis

rpush blog:1:tags nodejs
lrange blog:1:tags 0 -1
// laravel php redis nodejs
lpush blog:1:tags python
// python laravel php redis nodejs
Enter fullscreen mode Exit fullscreen mode

Sets

  • Unordered collection of strings
  • It is possible to add, remove, and test.
  • They do not allow repeated items and when retrieving items they are not returned in the same order they are entered
sadd user:1:anime naruto onepiece jujutsu kimi_no_nawa
smembers user:1:anime
Enter fullscreen mode Exit fullscreen mode

Hashes

  • Berisi list key dan value
HMSET myhash field1 "Hello" field2 "World"
HGET myhash field1
HGET myhash field2
Enter fullscreen mode Exit fullscreen mode

Sorted Sets

  • Adds all the specified members with the specified scores to the sorted set stored at key.
redis>  ZADD myzset 1 "one"

(integer) 1

redis>  ZADD myzset 1 "uno"

(integer) 1

redis>  ZADD myzset 2 "two" 3 "three"

(integer) 2

redis>  ZRANGE myzset 0 -1 WITHSCORES

1) "one"
2) "1"
3) "uno"
4) "1"
5) "two"
6) "2"
7) "three"
8) "3"

redis>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)