Redis系列之掃盲篇(一)

作者:z小趙
★?一枚用心堅持寫原創(chuàng)的“無趣”程序猿,在自身受益的同時也讓朋友們在技術(shù)上有所提升。
目錄
Redis 是什么? Redis 安裝。 基礎(chǔ)命令掃盲。
Redis 是什么?
Redis 是一款由 C 語言編寫的、分布式的、高性能的、非關(guān)系型數(shù)據(jù)庫,其擁有超高的吞吐量(每秒 10w,我司實際使用場景中單端口讀請求最高 8w,寫請求 5w,具體得看實際使用場景和機器性能),但由于其基于內(nèi)存操作的,內(nèi)存相對比較昂貴的,所以一般只有在并發(fā)相對比較高且存儲要求相對較小的場景中被廣泛使用(如果有錢一般可以不用考慮內(nèi)存的事情,哈哈)。
想說一句多余的話,為什么要學(xué)習(xí) Redis 呢?在我看來就兩點:
為了當下實際業(yè)務(wù)使用而學(xué)習(xí) 為了進大廠
為了這兩個目標,想學(xué)習(xí)的朋友們抓緊上車,一起開啟愉快的學(xué)習(xí)旅程。
Redis 安裝
下載地址
http://download.redis.io/releases/
安裝
$ ls
redis-4.0.10.tar.gz
# 減壓
$ tar -zxvf redis-4.0.10.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
基礎(chǔ)命令掃盲
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設(shè)置過期時間
$ localhost:6379> set t1 value ex 5
OK
# 查詢指定key的過期時間
$ localhost:6379> TTL t1
(integer) 7
# 查詢指定key的類型
$ localhost:6379> type t1
string
kv 結(jié)構(gòu)
# 設(shè)置key 對應(yīng)的value
$ localhost:6379> set key1 value1
OK
# 獲取key對應(yīng)的value
$ localhost:6379> get key1
"value1"
# 設(shè)置帶有過期時間的key,EX標識5秒后過期,單位秒
$ localhost:6379> set key2 value2 EX 5
OK
# 設(shè)置帶有過期時間的key,PX表示5秒后過期,單位毫秒
# 設(shè)置key,如果其不存在的話,NX
$ localhost:6379> set key3 vlaue3 NX
OK
# 如果key存在的話,則直接返回nil
$ localhost:6379> set key3 value3 NX
(nil)
list 結(jié)構(gòu)
# 從左側(cè)插入元素
$ localhost:6379> LPUSH list1 1
(integer) 1
# 從右側(cè)插入元素
$ localhost:6379> RPUSH list1 2
(integer) 2
# 查看集合內(nèi)的元素,-1 表示查看所有元素
$ localhost:6379> LRANGE list1 0 -1
1) "1"
2) "2"
# 查看list的元素個數(shù)
$ localhost:6379> LLEN list1
(integer) 2
# 根據(jù)索引查詢對應(yīng)的元素,如果指定的索引不存在,則返回'nil'
$ localhost:6379> LINDEX list1 0
"2"
# 從列表左側(cè)移除一個元素
$ 127.0.0.1:6379> LPOP list1
"5"
# 從列表右側(cè)移除一個元素
$ 127.0.0.1:6379> RPOP list1
"1"
# 從列表右側(cè)移除一個元素添加到左側(cè)
$ 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 結(jié)構(gòu)
# 向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集合元素個數(shù)
$ 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 結(jié)構(gòu)
# 向有序集合中添加元素
$ localhost:6379> ZADD zset1 1 'one'
(integer) 1
# 獲取有序集合中指定分數(shù)范圍的元素
$ localhost:6379> ZRANGE zset1 0 -1
1) "one"
2) "two"
3) "three"
# 刪除有序集合中的元素
$ localhost:6379> ZREM zset1 'one'
(integer) 1
# 獲取有序集合元素個數(shù)
$ localhost:6379> ZCARD zset1
(integer) 2
# 為有序集合中指定成員增加指定個數(shù)
$ 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"
# 獲取有序集合指定分數(shù)范圍內(nèi)的元素數(shù)量
$ localhost:6379> ZCOUNT zset1 0 2
(integer) 1
# 獲取元素在有序集合中的排名,分數(shù)越大,排名值越大
$ localhost:6379> ZRANK zset1 'one'
(integer) 2
# 獲取元素在有序集合中的排名。分數(shù)越大,排名值越小
$ localhost:6379> ZREVRANK zset1 'one'
(integer) 0
# 獲取指定元素的分值
$ localhost:6379> ZSCORE zset1 'one'
"4"
hash 結(jié)構(gòu)
# 向hash集合中添加一個元素
$ localhost:6379> HSET hash1 field1 1
(integer) 1
# 向hash集合中添加多個元素
$ localhost:6379> HMSET hash1 field2 2 field3 3
OK
# 獲取指定field對應(yīng)的value
$ localhost:6379> HGET hash1 field1
"1"
# 批量獲取指定field下的value
$ localhost:6379> HMGET hash1 field1 field2 field3
1) "1"
2) "2"
3) "3"
# 獲取hash結(jié)合里面所有元素
$ localhost:6379> HGETALL hash1
1) "filed1"
2) "1"
3) "filed2"
4) "2"
5) "filed3"
6) "3"
7) "field3"
8) "3"
# 判斷指定filed是否在Hash結(jié)構(gòu)中存在
$ localhost:6379> HEXISTS hash1 field1
(integer) 1
$ localhost:6379> HEXISTS hash1 field5
(integer) 0
# 從hash結(jié)構(gòu)中刪除一個或多個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結(jié)構(gòu)中所有的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集合中的元素個數(shù)
$ localhost:6379> HLEN hash1
(integer) 5
總結(jié)
本文介紹了實際生產(chǎn)中常見常用的命令,更多詳細命令使用及介紹可以查看官網(wǎng),如果覺得看的費勁的話,也可訪問中文網(wǎng)站。
官網(wǎng):https://redis.io/commands 中文網(wǎng)站:http://www.redis.cn/commands.html#
今天文章主要是 Redis 基礎(chǔ)使用掃盲,下面文章開始趴一下 Redis 的底層實現(xiàn),看看它為啥那么快,敬請期待。
評論
圖片
表情
