zoukankan      html  css  js  c++  java
  • Redis学习笔记——Redis的基本操作

    之前介绍过如何在ubuntu安装Redis服务器:https://www.cnblogs.com/zifeiy/p/9062738.html

    接下来,我们在Redis上进行一些基本的操作。
    所县使用命令:redis-cli登陆Redis命令行。
    然后执行下面的指令进行测试与学习:

    字符串类型的增删改查

    ### 增加一个值key为name,value为zifeiy
    127.0.0.1:6379> set name 'zifeiy'
    OK
    ### 查询name的值
    127.0.0.1:6379> get name
    "zifeiy"
    ### 更新name的值
    127.0.0.1:6379> set name 'yiefiz'
    OK
    ### 查询name的值
    127.0.0.1:6379> get name
    "yiefiz"
    ### 删除namd的值
    127.0.0.1:6379> del name
    (integer) 1
    ### 查询是沟存在name,0表示不存在
    127.0.0.1:6379> exists name
    (integer) 0
    

    List集合的增删改查

    ### 添加key为user_list,value为'zifeiy','hello'的list集合
    127.0.0.1:6379> lpush user_list 'zifeiy' 'hello'
    (integer) 2
    ### 查询key为user_list的集合
    127.0.0.1:6379> lrange user_list 0 -1
    1) "hello"
    2) "zifeiy"
    ### 往list底部添加love元素
    127.0.0.1:6379> rpush user_list 'hi'
    (integer) 3
    ### 查询key为user_list的集合
    127.0.0.1:6379> lrange user_list 0 -1
    1) "hello"
    2) "zifeiy"
    3) "hi"
    ### 更新index为0的值
    127.0.0.1:6379> lset user_list 0 'haha'
    OK
    ### 查询key为user_list的集合
    127.0.0.1:6379> lrange user_list 0 -1
    1) "haha"
    2) "zifeiy"
    3) "hi"
    ### 删除第1个为'wish'的值
    127.0.0.1:6379> lrem user_list 0 'haha'
    (integer) 1
    ### 查询key为user_list的集合
    127.0.0.1:6379> lrange user_list 0 -1
    1) "zifeiy"
    2) "hi"
    

    Hash集合的增删改查

    127.0.0.1:6379> flushdb
    OK
    127.0.0.1:6379> hset user_hset "user1" "zifeiy"
    (integer) 1
    127.0.0.1:6379> hset user_hset "user2" "apple"
    (integer) 1
    127.0.0.1:6379> hlen user_hset
    (integer) 2
    127.0.0.1:6379> hkeys user_hset
    1) "user1"
    2) "user2"
    127.0.0.1:6379> hget user_hset "user1"
    "zifeiy"
    127.0.0.1:6379> hgetall user_hset
    1) "user1"
    2) "zifeiy"
    3) "user2"
    4) "apple"
    127.0.0.1:6379> hset user_hset "user1" "new_zifeiy"
    (integer) 0
    127.0.0.1:6379> hset user_hset "user2" "banena"
    (integer) 0
    127.0.0.1:6379> hgetall user_hset
    1) "user1"
    2) "new_zifeiy"
    3) "user2"
    4) "banena"
    127.0.0.1:6379> hdel user_hset user1
    (integer) 1
    127.0.0.1:6379> hgetall user_hset
    1) "user2"
    2) "banena"
    

    SortedSet集合的增删改查

    127.0.0.1:6379> flushdb
    OK
    127.0.0.1:6379> zadd user_zset 1 "zifeiy"
    (integer) 1
    127.0.0.1:6379> zadd user_zset 2 "banana"
    (integer) 1
    127.0.0.1:6379> zadd user_zset 3 "hello"
    (integer) 1
    127.0.0.1:6379> zrange user_zset 0 -1
    1) "zifeiy"
    2) "banana"
    3) "hello"
    127.0.0.1:6379> zrevrange user_zset 0 -1
    1) "hello"
    2) "banana"
    3) "zifeiy"
    127.0.0.1:6379> zscore user_zset "zifeiy"
    "1"
    127.0.0.1:6379> zscore user_zset "hello"
    "3"
    
  • 相关阅读:
    【CF932E】Team Work
    【ZJOI2011】看电影
    【CQOI2011】放棋子
    【HAOI2010】计数
    【HNOI2009】有趣的数列
    【ZJOI2010】排列计数
    【FJOI2016】建筑师
    【USACO10 OPEN】三角形计数
    【HNOI2012】排队
    【HNOI2008】越狱
  • 原文地址:https://www.cnblogs.com/zifeiy/p/9917333.html
Copyright © 2011-2022 走看看