Redis中五種數據類型簡單操作
提出問題
Redis五種數據類型的簡單增刪改查命令???
解決問題
假設你已經安裝Redis服務器;
假設你已經打開Redis cli命令行工具;
假設你對Redis有所了解;
Redis簡單增刪改查例子
例一:字符串的增刪改查
#增加一個key為ay_key的值 127.0.0.1:6379> set ay_key "ay" OK #查詢ay_key的值 127.0.0.1:6379> get ay_key "ay" #修改ay_key的值 127.0.0.1:6379> set ay_key "new_ay" OK 127.0.0.1:6379> get ay_key "new_ay" #修改ay_key名稱 127.0.0.1:6379> rename ay_key new_ay_key OK 127.0.0.1:6379> keys * 1) "new_ay_key" #刪除ay_key 127.0.0.1:6379> del ay_key (integer) 0 #查詢是否存在ay_key 0 127.0.0.1:6379> exists ay_key (integer) 0
例二:Set集合的增刪改查
#刪除當前選擇數據庫中的所有key 127.0.0.1:6379> flushdb OK #生成set集合,添加4個數據 127.0.0.1:6379> sadd set_ay_key "ay" "al" "xy" "xl" (integer) 4 #查詢set裡面所有值 127.0.0.1:6379> smembers set_ay_key 1) "xy" 2) "al" 3) "ay" 4) "xl" #刪除value為"xl" , 返回 1 如果沒有返回 0 127.0.0.1:6379> srem set_ay_key "xl" (integer) 1 127.0.0.1:6379> smembers set_ay_key 1) "xy" 2) "al" 3) "ay" #添加value為"xl" 127.0.0.1:6379> sadd set_ay_key "xl" (integer) 1 127.0.0.1:6379> smembers set_ay_key 1) "xy" 2) "al" 3) "ay" 4) "xl" #添加value為"xl" 添加不進去,但也不報錯,set是不允許重復的 127.0.0.1:6379> sadd set_ay_key "xl" (integer) 0 #不多解釋 127.0.0.1:6379> sadd set_ay_key "xl" (integer) 0 #不多解釋 127.0.0.1:6379> sadd set_ay_key "xl" (integer) 0
例三:List集合的增刪改查
#添加key為list_ay_key的list集合 127.0.0.1:6379> lpush list_ay_key "ay" "al" "xy" "xl" (integer) 4 #查詢key為list_ay_key的集合 127.0.0.1:6379> lrange list_ay_key 0 -1 1) "xl" 2) "xy" 3) "al" 4) "ay" #往list尾部添加元素 127.0.0.1:6379> rpush list_ay_key "together" (integer) 5 #往list頭部添加元素 127.0.0.1:6379> lpush list_ay_key "first" (integer) 6 #查詢list集合 127.0.0.1:6379> lrange list_ay_key 0 -1 1) "first" 2) "xl" 3) "xy" 4) "al" 5) "ay" 6) "together" #更新index為0的值 127.0.0.1:6379> lset list_ay_key 0 "update_first" OK 127.0.0.1:6379> lrange list_ay_key 0 -1 1) "update_first" 2) "xl" 3) "xy" 4) "al" 5) "ay" 6) "together" #刪除index為1上的值 127.0.0.1:6379> lrem list_ay_key 1 "update_first" (integer) 1 127.0.0.1:6379> lrange list_ay_key 0 -1 1) "xl" 2) "xy" 3) "al" 4) "ay" 5) "together"
例四:Hash集合(類似Java)的增刪改查
127.0.0.1:6379> flushdb OK #生成hash集合,並添加key 為uuid_one value 為"12345" 127.0.0.1:6379> hset hash_ay_key "uuid_one" "12345" (integer) 1 127.0.0.1:6379> hlen hash_ay_key (integer) 1 #返回集合所有的key 127.0.0.1:6379> hkeys hash_ay_key 1) "uuid_one" #返回集合所有value 127.0.0.1:6379> hvals hash_ay_key 1) "12345" #集合添加值 127.0.0.1:6379> hset hash_ay_key "uuid_two" "22222" (integer) 1 #集合添加值 127.0.0.1:6379> hset hash_ay_key "uuid_three" "33333" (integer) 1 #獲得key為uuid_one的值 127.0.0.1:6379> hget hash_ay_key uuid_one "12345" #刪除key為uuid_three的值 127.0.0.1:6379> hdel hash_ay_key uuid_three (integer) 1 127.0.0.1:6379> hkeys hash_ay_key 1) "uuid_one" 2) "uuid_two" #獲得所有,包括key和value 127.0.0.1:6379> hgetall hash_ay_key 1) "uuid_one" 2) "12345" 3) "uuid_two" 4) "22222" #更新key為uuid_one的值 127.0.0.1:6379> hset hash_ay_key uuid_one "11111" (integer) 0 127.0.0.1:6379> hset hash_ay_key "uuid_one" "11111" (integer) 0 127.0.0.1:6379> hgetall hash_ay_key 1) "uuid_one" 2) "11111" 3) "uuid_two" 4) "22222"
例五:SortedSet集合的增刪改查
SortedSet是有序的set集合
#sorted set添加值ay 排序值為 1 127.0.0.1:6379> zadd zset_ay_key 1 "ay" (integer) 1 127.0.0.1:6379> zadd zset_ay_key 2 "al" (integer) 1 127.0.0.1:6379> zadd zset_ay_key 3 "xy" (integer) 1 127.0.0.1:6379> zadd zset_ay_key 4 "xl" (integer) 1 #查詢所有的值 127.0.0.1:6379> zrange zset_ay_key 0 -1 1) "ay" 2) "al" 3) "xy" 4) "xl" #刪除所有的值 127.0.0.1:6379> zrem zet_ay_key "xl" (integer) 0 127.0.0.1:6379> zrange zset_ay_key 0 -1 1) "ay" 2) "al" 3) "xy" 4) "xl"
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!