Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Cơ Sở Dữ Liệu

Làm chủ Redis: Hướng dẫn Beginner cho Ultimate Data Structure Server

Tốc độ và flexibility của Redis khiến nó trở thành powerful tool cực kỳ cho developers và có thể dùng theo nhiều cách khác nhau. Dù Redis thường được gọi là key-value store, nhưng better described as Data Structure Server, vì cũng support 5 data structure types:

  • Strings
  • Hashes
  • Lists
  • Sets
  • Sorted sets

Mỗi structure type có shared commands và commands specific cho particular structure type.

Introduction này cover basics cách dùng Redis và overview các data structures. Chúng tôi cover một số basic commands, tuy nhiên nhớ Redis có hơn 160 commands tại thời điểm viết, tìm excellent documentation tại redis.io/commands.

Khởi động Redis

Dùng instance từ ObjectRocket for Redis hoặc cài Redis local.

Để cài local, download, extract và compile code:

$ wget https://download.redis.io/releases/redis-2.8.17.tar.gz
$ tar xzf redis-2.8.17.tar.gz
$ cd redis-2.8.17
$ make

Start local instance:

$ src/redis-server

Sử dụng Redis

Bây giờ ready interact với Redis dùng built-in client. Cho ObjectRocket for Redis instance, start redis-cli với hostname, port, password:

$ redis-cli -h my-host -p 1234 -a mypassword

Nếu dùng local instance, host là localhost, default port 6379, không password mặc định.

$ redis-cli

GET và SET

Ở simplest level, Redis mô tả như key-value store. Bằng command SET foo bar, set value của foo thành bar. Ví dụ, issue command sau từ redis-cli vừa start:

redis> SET foo bar
OK

Bây giờ read value của foo với GET command.

redis> GET foo
"bar"

EXPIRE và TTL

Có thể set keys expire trong given amount of time dùng command EXPIRE. TTL report time remaining trước khi key expire.

redis> SET foo bar
OK
redis> EXPIRE foo 120
(integer) 1
redis> TTL foo
(integer) 113

Lists

Đặc điểm phân biệt của Redis: values của key có thể là data-structures, không chỉ values.

Tạo list dùng LPUSH hoặc RPUSH. Nếu list đã tồn tại, LPUSH add value vào đầu, RPUSH add vào cuối.

redis> LPUSH cities "San Francisco"
(integer) 1
redis> RPUSH cities "Austin"
(integer) 2
redis> LRANGE cities 0 -1
1. "San Francisco"
2. "Austin"
redis> SORT cities alpha
1. "Austin"
2. "San Francisco"
redis> LPOP cities
"San Francisco"
redis> LRANGE cities 0 -1
1. "Austin"

SORT command sort list lexicographically ascending với ALPHA argument. Để sort descending, append DESC argument vào SORT command.

RPOP command pop element từ end của list. LPOP pop từ beginning.

Sets

Sets tương tự lists, trừ mỗi element chỉ occur once. Ví dụ dưới tạo small set US States. SADD command add item vào set, trừ khi item đã exist. Nếu không exist, add và return 1; ngược lại return 0. SMEMBERS return all items. SCARD return number elements. SREM remove item.

redis> SADD states "Vermont"
(integer) 1
redis> SMEMBERS states
1. "Vermont"
redis> SADD states "Texas"
(integer) 1
redis> SCARD states
(integer) 2
redis> SADD states "Vermont"
(integer) 0
redis> SMEMBERS states
1. "Vermont"
2. "Texas"
redis> SADD states "California"
(integer) 1
redis> SMEMBERS states
1. "Vermont"
2. "Texas"
3. "California"
redis> SREM states "California"
(integer) 1
redis> SMEMBERS states
1. "Vermont"
2. "Texas"

Hashes

Dùng hashes, assign và map string values đến fields trong mỗi key. Hash với vài fields stored cách tốn very little space, nên store millions objects trong small Redis instance. Ví dụ dưới, name của user:1 set thành "john racker" dùng HSET. HGET get name value của user. HGETALL return all keys và values related đến specified key.

redis> HSET user:1 name "john racker"
(integer) 1
redis> HGET user:1 name
"joe racker"
redis> HSET user:1 company "objectrocket"
(integer) 1
redis> HGET user:1 company
"ObjectRocket"
redis> HSET user:1 city "austin"
(integer) 1
redis> HGET user:1 city
"austin"
redis> HGETALL user:1
1. "name"
2. "john racker"
3. "company"
4. "objectrocket"
5. "city"
6. "austin"

SORTED SETS

Sorted Sets tương tự sets: non-repeating collections of strings, nhưng mỗi member associated với score. Sorted sets sorted by score ascending. Same element chỉ exist single time, no repeated elements, dù scores may be repeated.

Ví dụ này add points totals top 5 teams English Premier League per 10/23/2014:

redis> ZADD EPL 22 "chelsea"
(integer) 1
redis> ZADD EPL 17 "man city"
(integer) 1
redis> ZADD EPL 16 "southampton"
(integer) 1
redis> ZADD EPL 13 "liverpool"
(integer) 1
redis> ZADD EPL 13 "west ham"
(integer) 1
redis> ZRANK EPL "chelsea"
(integer) 3
redis> ZRANK EPL "liverpool"
(integer) 0
redis> ZRANK EPL "arsenal"
(nil)

Tiếp theo rank teams dựa trên points total dùng ZRANGE:

redis> ZRANGE EPL 0 -1
1) "liverpool"
2) "west ham"
3) "southampton"
4) "man city"
5) "chelsea"
redis> ZRANGE EPL 2 3
1) "southampton"
2) "man city"

Nhớ sets sorted ascending, nên để thấy rank teams dựa trên points total cần dùng ZREVRANGE

redis> ZREVRANGE EPL 0 -1
1) "chelsea"
2) "man city"
3) "southampton"
4) "west ham"
5) "liverpool"
redis> ZREVRANGE EPL 0 -1 WITHSCORES
1) "chelsea"
2) "22"
3) "man city"
4) "17"
5) "southampton"
6) "16"
7) "west ham"
8) "13"
9) "liverpool"
10) "13"

Tiếp theo, Southampton chơi game và thắng, gain 3 points. Dùng ZINCRBY increment score, và ZREVRANGE thấy Southampton move vào second place. Cũng dùng ZREVRANK thấy rank Southampton. Note: rank 0-based, nghĩa là member với highest score (Chelsea) có rank 0.

redis> ZINCRBY EPL 3 "southampton"
"19"
redis> ZREVRANGE EPL 0 -1 WITHSCORES
1) "chelsea"
2) "22"
3) "southampton"
4) "19"
5) "man city"
6) "17"
7) "west ham"
8) "13"
9) "liverpool"
10) "13"
redis> ZREVRANK EPL "southampton"
(integer) 1
redis> ZREVRANGEBYSCORE EPL 25 (15
1) "chelsea"
2) "southampton"
3) "man city"

Tuần tới chúng tôi sẽ dive deeper vào Redis và use cases cho versatile developer tool này. Drop us a line nếu có questions về running Redis hoặc muốn share cách bạn dùng Redis trong application stack.