Redis ฉบับเริ่มต้น ใน 1 วัน
Redis เป็น No-sql ตัวนึงที่นิยมในการทำ cache เนื่องจากเป็น in-memory database จึงมีความเร็วค่อนข้างสูง
ไม่ต้องอธิบายเยอะ ติดตั้งเลยละกัน
ใครใช้ mac ก็ลง brew (ใครไม่มี หรือ ใช้ os อื่นลองหาวิธีลงดูไม่ยาก)
brew install redis
start redis server เลย
redis-server
หลังจากนั้นให้เปิดอีก tab เพื่อใช้งาน redis-cli (ห้ามปิด redis servcer นะ)
redis-cli
หรือ จะใช้ Docker
docker run --name some-redis -d redis // สำหรับ redis serverdocker run -it --link some-redis:redis --rm redis redis-cli -h redis -p 6379 // สำหรับ redis-cli
ต่อไปก็ทำการไหว้ครูด้วย set key = hello และ value = world
127.0.0.1:6379> set hello world // คำสั่ง set <key> <value>
OK
ทีนี้ลอง get ข้อมูลด้วย key
127.0.0.1:6379> get hello // คำสั่ง get <key>
“world”
Redis ทำได้มากกว่าการเก็บ key-value ธรรมดา เพราะจริงๆแล้ว Redis คือ data structures server
Redis สามารถเก็บข้อมูลรวมได้หลายหลายรูปแบบ และมีคำสั่งในการกับข้อมูล นอกเหนือจากการ get set
- บวกเลข ทีละหนึ่งด้วยคำสั่ง incr
> set counter 1
OK
> incr counter
(integer) 2
- บวกเลขด้วยคำสั่ง INCRBY
> INCRBY counter 5
(integer) 7
- เก็บข้อมูลหลายๆ key-value พร้อมกันด้วยคำสั่ง mset , mget
> mset a 1 b 2 c 3
OK
> mget a b c //ดึงหลายๆ key พร้อมกัน
1) "1"
2) "2"
3) "3"หรือจะใช้ get ก็ได้เช่นกัน
> get a
"10"
- เช็คว่า key นี้มีหรือยังด้วยคำสั่ง exists
> exists b
(integer) 1 // 1 คือมี key
> exists n
(integer) 0 // 0 คือไม่มี
- กำหนดอายุของข้อมูลด้วยคำสั่ง ex
สมมติว่าผมอยากเก็บ session ของ user ที่ login ใน redis โดยจำกัดเวลาที่ 10 วิ
> set session test ex 10 // set <key> <value> ex <expires in seconds>> ttl session // เช็คเวลาที่เหลือของข้อมูล
(integer) 8> get session
(nil) // เมื่ออายุของข้อมูลหมด
List
- LPUSH คือ การสร้าง list (โดยถ้ามีอยู่แล้วจะเป็นการเพิ่มข้อมูล) เพิ่มข้อมูลที่ด้วแรก
- RPUSH เหมือนกับ LPUSH แต่จะเป็นการเพิ่มข้อมูลต่อหลังตัวสุดท้าย
- LRANGE คือการดึงข้อมูล 0 คือตัวแรก -1 คือตัวสุดท้าย
- LPOP , RPOP คือการดึงข้อมูลออกจาก list จากด้านหัว และ ท้ายตามลำดับ
- ยังมีคำสั่งอีกเยอะเลยเกี่ยวกับ list ลองอ่าน doc ดู
> LPUSH mylist 1
(integer) 1> LPUSH mylist 2 3 4 5
(integer) 5> RPUSH mylist 6
(integer) 6> LRANGE mylist 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
6) "6"> rpop mylist
"6"> lpop mylist
"5"> LRANGE mylist 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
SET
- คือชุดข้อมูลที่ไม่ซ้ำและไม่ได้เรียงลำดับ
- SADD คือสร้าง set ถ้ามีอยู่แล้วจะเป็นการ เพิ่ม
- SMEMBERS แสดงสมาชิกทั้งหมด
- SISMEMBER ตรวจสอบว่าข้อมูลเป็นสมาชิกของ set หรือไม่
> sadd myset 1
(integer) 1
> sadd myset 2
(integer) 1
> sadd myset 3
(integer) 1
> sadd myset 3
(integer) 0
> sadd myset 4 5
(integer) 2> smembers myset
1) “1”
2) “2”
3) “3”
4) “4”
5) “5”> sismember myset 1
(integer) 1
Hashes
- ถ้าจะอธิบายให้ง่ายก็คือ key: [ key1:value1 , key2:value2]
เช่น user:1 มี key = name , value = thehoi และ key = age , value = 20
- hmset คือสร้าง hash ขึ้นมาโดยมีสมาชิกได้หลายตัว
- hget คือ get value ด้วย key ของสมาชิก
- hmget คือ get value ด้วย key หลายๆตัวพร้อมกัน
- hgetall คือ get ข้อมูลทั้งหมด
> HMSET user:1 name thehoi age 25
OK> HMSET user:2 name eiei age 10
OK> HGET user:1 name
“thehoi”> HMGET user:1 name age
1) “thehoi”
2) “25”> HGETALL user:1
1) “name”
2) “thehoi”
3) “age”
4) “25”
PUB/SUB
- คือการที่มีช่องสำหรับการรับ-ส่งข้อมูล
- เหมาะกับการคุยกันระหว่าง server to server
- ถ้าคิดไม่ออก ก็ให้คิดว่ามันทำงานคล้ายๆกับ message queue broker เช่น kafka
เรียนนี้ผมเคยเขียนไว้สมัยลองเล่น redis แรกๆ ลองไปอ่านดู =>>> Redis Pub/Sub กับ Spring boot
Delete Data
- FLUSHDB ลบข้อมูลเฉพาะ Database ที่ต่ออยู่
- FLUSHALL ลบข้อมูลทุก Database
> FLUSHALL
OK
> get hello
(nil)
ปัญหาคือ ถ้าในการใช้งานจริงมีใครเผลอพิมพ์คำสั่งด้านบนไป ข้อมูลทุกอย่างจะหายหมด
เพราะฉะนั้นถ้าจะใช้บน production แนะนำว่าควรปิดคำสั่งนี้ หรือ เปลี่ยนมันซะ
ให้ไปที่ /etc/redis/redis.conf
จากนั้นเพิ่มคำสั่งดังนี้
สำหรับปิดคำสั่ง flushdb และ flushall
rename-command FLUSHDB ""
rename-command FLUSHALL ""
หรือ ถ้ายังจำเป็นต้องใช้ในบ้างครั้ง อาจจะเปลี่ยนเป็นคำสั่งอื่นแทน
rename-command FLUSHDB dAXgbqEeI8
rename-command FLUSHALL LT4zjiczxU
จากนั้น restart redis
service redis-server restart
ผลที่ได้
>FLUSHDB
(error) ERR unknown command ‘FLUSHDB’> LT4zjiczxU
OK
ใน redis ยังมีอะไรอีกมาก อยากให้ลองไปศึกษากันดู
ที่มา : redis.io