RedisDB學(xué)習(xí)

簡介:

Redis是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API


特性:
特性
1 支持?jǐn)?shù)據(jù)的持久化,可將內(nèi)存中的數(shù)據(jù)保存在磁盤中,重啟的時候可以再次加載進(jìn)行使用
2 不僅支持簡單的key-value類型的數(shù)據(jù),同時將value分為list,set,zset,hash等數(shù)據(jù)結(jié)構(gòu)存儲
3 因redis交換數(shù)據(jù)快,所以在服務(wù)器中常采用來存儲一些需要頻繁調(diào)取的數(shù)據(jù)以便提高效率

數(shù)據(jù)結(jié)構(gòu):

redis是key-value的數(shù)據(jù)結(jié)構(gòu)
每條數(shù)據(jù)都是一個鍵值對
鍵的類型是字符串
要注意的是:鍵不能重復(fù)
值的數(shù)據(jù)類型分為五種:

形式 類型
1 String 字符串
2 Hash 哈希
3 List 列表
4 Set 集合
5 Zset 有序集合

基本概念:

1.數(shù)據(jù)庫沒有名字,默認(rèn)有16個庫,通過0-15來標(biāo)識連接redis數(shù)據(jù)庫時
2.默認(rèn)選擇第一個數(shù)據(jù)庫,即0
3.redis里執(zhí)行g(shù)et或hget不存在的key或field時返回值在終端顯式的是”(nil)”


基本操作:

1.連接redis數(shù)據(jù)庫:redis-cli
2.退出:exit
3.操作服務(wù)器:service redis start/stop/restart
4.切換數(shù)據(jù)庫:select \color{red}{n}


全局鍵(key)的操作

#查看所有的鍵(key):keys *  (注意:s與*之間有空格)

127.0.0.1:6379> keys *
1) "name"
2) "age"

#---------------------
#查看鍵(key)是否存在:exists key (返回1則表示存在,返回0則表示不存在)

127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists sex
(integer) 0

#---------------------
#刪除鍵值對:del key

#---------------------
#改名:rename key new_key

127.0.0.1:6379> get age
"20"
127.0.0.1:6379> rename age num
OK
127.0.0.1:6379> get num
"20"

#---------------------
#設(shè)置過期時間:expire key seconds  (注意:seconds是整數(shù)類型)
(查看時間:ttl,刪除過期時間:persist key)

127.0.0.1:6379> expire age 20    #給名為age的鍵設(shè)置過期時間為20s
(integer) 1
127.0.0.1:6379> ttl age          #查看age的過期剩余時間(已過期,即已經(jīng)刪除)
(integer) -2
127.0.0.1:6379> persist age      #刪除過期時間
(integer) 0



String類型

string是redis最基本的數(shù)據(jù)類型
一個key對應(yīng)一個value

基本操作:

#設(shè)置數(shù)據(jù):set key value

127.0.0.1:6379> set name jayss          #創(chuàng)建一個鍵(key)為name,值(value)為jayss的數(shù)據(jù)
OK

#---------------------
#查看數(shù)據(jù):get key

127.0.0.1:6379> get name                #查看鍵(key)為name的值
"jayss"

#---------------------
#追加數(shù)據(jù):append key value     
         
127.0.0.1:6379> append age 20           #添加鍵(key)為age,值(value)為20的數(shù)據(jù)
(integer) 
127.0.0.1:6379> get age
"20"

#---------------------
#刪除數(shù)據(jù):del key

127.0.0.1:6379> get age                  #刪除鍵(key)為age的數(shù)據(jù)
"20"
127.0.0.1:6379> del age
(integer) 1
127.0.0.1:6379> get age
(nil)



Hash類型

hash又叫哈希,是一個鍵值對(key-value)的類型
是string類型的field和value的映射表

例如:
information {name:jayss,age:20}

information name,age jayss,20

基本操作:

#添加數(shù)據(jù):hset key field value

127.0.0.1:6379> hset information name jayss      #添加鍵為information,域?yàn)閚ame,值為jayss的數(shù)據(jù)
(integer) 1
127.0.0.1:6379> hset information age 20          #添加鍵為information,域?yàn)閍ge,值為20的數(shù)據(jù)
(integer) 1

#---------------------
#查看域值:hget hvals key

127.0.0.1:6379> hget information name
"jayss"
127.0.0.1:6379> hget information age
"20"

#---------------------
#查看所有的field:hkeys key

127.0.0.1:6379> hkeys information
1) "name"
2) "age"

#---------------------
#查看所有的value:hvals key

127.0.0.1:6379> hvals information
1) "jayss"
2) "20"


List類型

1.list類型是一個字符串列表
2.可在表頭或尾部添加/刪除數(shù)據(jù)
3.在插入數(shù)據(jù)時,若該鍵(key)不存在,則redis會創(chuàng)建該鍵

基本操作:

#添加數(shù)據(jù):rpush key value  [value1,value2...](尾部添加數(shù)據(jù))
lpush key value [value1,value2...](頭部添加數(shù)據(jù))

127.0.0.1:6379> rpush name jayss
(integer) 1
127.0.0.1:6379> rpush num [1,2,3,4,5]
(integer) 1
127.0.0.1:6379> keys *
1) "num"
2) "name"

#---------------------
#查看數(shù)據(jù):lrange key start stop
#查看某個數(shù)據(jù):lindex key index

127.0.0.1:6379> lrange num 0 4
1) "[1,2,3,4,5]"

#---------------------
#刪除數(shù)據(jù):rpop key
lpop key (頭部刪除數(shù)據(jù))

127.0.0.1:6379> keys *
1) "num"
2) "name"
127.0.0.1:6379> rpop num
"[1,2,3,4,5]"
127.0.0.1:6379> keys *
1) "name"


Set類型

set類型為無序的字符集合
元素具有唯一性,不重復(fù)

基本操作:
#添加數(shù)據(jù):sadd key member [member1,member2...]

127.0.0.1:6379> sadd name jayss
(integer) 1
127.0.0.1:6379> sadd num [1,2,3,4]
(integer) 1
127.0.0.1:6379> keys *
1) "name"
2) "num"

#---------------------
#查看數(shù)據(jù):smembers key

127.0.0.1:6379> smembers name
1) "jayss"
127.0.0.1:6379> smembers num
1) "[1,2,3,4]"

#---------------------
#隨機(jī)刪除:spop key

127.0.0.1:6379> keys *
1) "name"
2) "num"
127.0.0.1:6379> spop name
"jayss"
127.0.0.1:6379> keys *
1) "num"

#---------------------
#指定刪除:srem key member [member1,member2...]



歡迎技術(shù)交流

WeChat......

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容