Redis系列之掃盲篇(一)

作者:z小趙

★ 

一枚用心堅持寫原創的“無趣”程序猿,在自身受益的同時也讓朋友們在技術上有所提升。

目錄

  1. Redis 是什麼?

  2. Redis 安裝。

  3. 基礎命令掃盲。

Redis 是什麼?

Redis 是一款由 C 語言編寫的、分佈式的高性能的非關係型數據庫,其擁有超高的吞吐量(每秒 10w,我司實際使用場景中單端口讀請求最高 8w,寫請求 5w,具體得看實際使用場景和機器性能),但由於其基於內存操作的,內存相對比較昂貴的,所以一般只有在併發相對比較高且存儲要求相對較小的場景中被廣泛使用(如果有錢一般可以不用考慮內存的事情,哈哈)。

想說一句多餘的話,爲什麼要學習 Redis 呢?在我看來就兩點:

  • 爲了當下實際業務使用而學習

  • 爲了進大廠

爲了這兩個目標,想學習的朋友們抓緊上車,一起開啓愉快的學習旅程。

Redis 安裝

  1. 下載地址

  • http://download.redis.io/releases/

  1. 安裝

$ ls
redis-3.0.0.tar.gz

# 減壓
$ tar -zxvf redis-3.0.0.tar.gz

# 編譯測試
$ sudo make test

# 編譯安裝
$ sudo make install

# 以後臺的方式啓動Redis
$ ./redis-server ~/software/redis-4.0.10/redis.conf &

# 查看Redis進程
$ ps -ef | grep redis

# 登錄Redis客戶端,Redis默認配置啓動端口是6379,可以通過修改配置替換端口
$ ./redis-cli -h localhost -p 6379

基礎命令掃盲

key 常用操作

# 查詢指定key是否存在
$ localhost:6379> EXISTS hash1
(integer) 1
$ localhost:6379> EXISTS jjj
(integer) 0

# 刪除指定key
$ localhost:6379> DEL hash1
(integer) 1
$ localhost:6379> EXISTS hash1
(integer) 0

# 對指定key設置過期時間
$ localhost:6379> set t1 value ex 5
OK

# 查詢指定key的過期時間
$ localhost:6379> TTL t1
(integer) 7

# 查詢指定key的類型
$ localhost:6379> type t1
string

kv 結構

# 設置key 對應的value
$ localhost:6379> set key1 value1
OK

# 獲取key對應的value
$ localhost:6379> get key1
"value1"

# 設置帶有過期時間的key,EX標識5秒後過期,單位秒
$ localhost:6379> set key2 value2 EX 5
OK

# 設置帶有過期時間的key,PX表示5秒後過期,單位毫秒

# 設置key,如果其不存在的話,NX
$ localhost:6379> set key3 vlaue3 NX
OK

# 如果key存在的話,則直接返回nil
$ localhost:6379> set key3 value3 NX
(nil)

list 結構

# 從左側插入元素
$ localhost:6379> LPUSH list1 1
(integer) 1

# 從右側插入元素
$ localhost:6379> RPUSH list1 2
(integer) 2

# 查看集合內的元素,-1 表示查看所有元素
$ localhost:6379> LRANGE list1 0 -1
1) "1"
2) "2"

# 查看list的元素個數
$ localhost:6379> LLEN list1
(integer) 2

# 根據索引查詢對應的元素,如果指定的索引不存在,則返回'nil'
$ localhost:6379> LINDEX list1 0
"2"

# 從列表左側移除一個元素
$ 127.0.0.1:6379> LPOP list1
"5"

# 從列表右側移除一個元素
$ 127.0.0.1:6379> RPOP list1
"1"

# 從列表右側移除一個元素添加到左側
$ localhost:6379> LRANGE list1 0 -1
1) "three"
2) "two"
3) "one"
$ localhost:6379> RPOPLPUSH list1 list2
"one"
$ localhost:6379> LRANGE list2 0 -1
1) "one"

set 結構

# 向set中添加一個元素
$ localhost:6379> SADD set1 'one' 'two' 'three'
(integer) 3

# 獲取set集合中的元素
$ localhost:6379> SMEMBERS set1
1) "one"
2) "three"
3) "two

# 從set集合中移除一個或多個元素
$ localhost:6379> SREM set1 'one'
(integer) 1

# 從set集合中移除一個或多個元素並返回被刪除元素
$ localhost:6379> SPOP set1 1
1) "three"

# 獲取當前set集合元素個數
$ localhost:6379> SCARD set1
(integer) 3

# 從set集合隨機獲取元素但不刪除
$ localhost:6379> SRANDMEMBER set1 1
1) "one"
$ localhost:6379> SMEMBERS set1
1) "three"
2) "two"
3) "one"

# 判斷set集合中是否存在指定元素,如果存在則返回1,不存在返回0
$ localhost:6379> SISMEMBER set1 'one'
(integer) 1
$ localhost:6379> SISMEMBER set1 '4'
(integer) 0

sorted set 結構

# 向有序集合中添加元素
$ localhost:6379> ZADD zset1 1 'one'
(integer) 1

# 獲取有序集合中指定分數範圍的元素
$ localhost:6379> ZRANGE zset1 0 -1
1) "one"
2) "two"
3) "three"

# 刪除有序集合中的元素
$ localhost:6379> ZREM zset1 'one'
(integer) 1

# 獲取有序集合元素個數
$ localhost:6379> ZCARD zset1
(integer) 2

# 爲有序集合中指定成員增加指定個數
$ localhost:6379> ZINCRBY zset1 2 "one"
"4"
$ localhost:6379> ZRANGE zset1 0 -1 WITHSCORES
1) "two"
2) "2"
3) "three"
4) "3"
5) "one"
6) "4"

# 獲取有序集合指定分數範圍內的元素數量
$ localhost:6379> ZCOUNT zset1 0 2
(integer) 1

# 獲取元素在有序集合中的排名,分數越大,排名值越大
$ localhost:6379> ZRANK zset1 'one'
(integer) 2

# 獲取元素在有序集合中的排名。分數越大,排名值越小
$ localhost:6379> ZREVRANK zset1 'one'
(integer) 0

# 獲取指定元素的分值
$ localhost:6379> ZSCORE zset1 'one'
"4"

hash 結構

# 向hash集合中添加一個元素
$ localhost:6379> HSET hash1 field1 1
(integer) 1

# 向hash集合中添加多個元素
$ localhost:6379> HMSET hash1 field2 2 field3 3
OK

# 獲取指定field對應的value
$ localhost:6379> HGET hash1 field1
"1"

# 批量獲取指定field下的value
$ localhost:6379> HMGET hash1 field1 field2 field3
1) "1"
2) "2"
3) "3"

# 獲取hash結合裏面所有元素
$ localhost:6379> HGETALL hash1
1) "filed1"
2) "1"
3) "filed2"
4) "2"
5) "filed3"
6) "3"
7) "field3"
8) "3"

# 判斷指定filed是否在Hash結構中存在
$ localhost:6379> HEXISTS hash1 field1
(integer) 1
$ localhost:6379> HEXISTS hash1 field5
(integer) 0

# 從hash結構中刪除一個或多個field
$ localhost:6379> HDEL hash1 field1 field2
(integer) 2

# 對hash集合中指定field的value增加值
$ localhost:6379> HINCRBY hash1 field1 2
(integer) 3
$ localhost:6379> HGET hash1 field1
"3"

# 獲取hash結構中所有的key
$ localhost:6379> HKEYS hash1
1) "filed1"
2) "filed2"
3) "filed3"
4) "field3"
5) "field1"

$ 獲取hash集合中所有的value
$ localhost:6379> HVALS hash1
1) "1"
2) "2"
3) "3"
4) "3"
5) "3"

# 獲取hash集合中的元素個數
$ localhost:6379> HLEN hash1
(integer) 5

總結

本文介紹了實際生產中常見常用的命令,更多詳細命令使用及介紹可以查看官網,如果覺得看的費勁的話,也可訪問中文網站。

  • 官網:https://redis.io/commands

  • 中文網站:http://www.redis.cn/commands.html#

今天文章主要是 Redis 基礎使用掃盲,下面文章開始趴一下 Redis 的底層實現,看看它爲啥那麼快,敬請期待。

系統研發工程師、z小趙
高併發設計 | 大數據 | 架構設計

往期推薦

Kafka系列10:面試題是否有必要深入瞭解其背後的原理?我覺得應該刨根究底(下)

Kafka系列9:面試題是否有必要深入瞭解其背後的原理?我覺得應該刨根究底(上)

Kafka:你必須要知道集羣內部工作原理的一些事!

消息是如何在服務端存儲與讀取的,你真的知道嗎? 

一文讀懂消費者背後的那點"貓膩"

感謝您的【在看】【轉發】支持

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章