zoukankan      html  css  js  c++  java
  • Centos7 安装部署redis及其入门使用

    #!/bin/bash
    wget
    -c http://download.redis.io/releases/redis-3.2.9.tar.gz #下载源码 tar -xvf redis-3.2.9.tar.gz #解压 cd redis-3.2.9/ make #编译,如果报zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录之类的错误,执行make MALLOC=libc make install #安装 ./utils/install_server.sh #安装并启动服务,直接回车默认即可
    /etc/init.d/redis_6379 start               #启动redis

    进入redis,我们该如何使用呢

    #字符串类型
    [root@localhost src]# redis-cli -h 127.0.0.1
    127.0.0.1:6379> set bp 123                      #设置字符串类型,变量bp的值为123
    OK
    127.0.0.1:6379> get bp                      #获取bp的值
    "123"
    127.0.0.1:6379> type bp                     #查看bp的类型
    string
    127.0.0.1:6379> append bp linux                 #在bp末尾添加linux
    (integer) 8
    127.0.0.1:6379> get bp                      #添加成功
    "123linux"
    127.0.0.1:6379> strlen bp                   #字符串长度
    (integer) 8
    127.0.0.1:6379> del bp                      #删除bp变量
    (integer) 1
    127.0.0.1:6379> get bp                      #删除成功
    (nil)
    127.0.0.1:6379> mset linux a kali b centos c            #同时设置多个值,mset后面是两个两个一对
    OK
    127.0.0.1:6379> mget linux kali centos              #同时获取多个值
    1) "a"
    2) "b"
    3) "c"
    
    #散列类型
    127.0.0.1:6379> hset id name passwd                 #散列名称为id,后面可以跟多对值
    (integer) 1
    127.0.0.1:6379> hset id sex color
    (integer) 1
    127.0.0.1:6379> hset id city good
    (integer) 1
    127.0.0.1:6379> hget id name                        #获取散列名称为id里的name对应的值
    "passwd"
    127.0.0.1:6379> hget id sex
    "color"
    127.0.0.1:6379> hgetall id                      #获取散列id的全部值
    1) "name"
    2) "passwd"
    3) "sex"
    4) "color"
    5) "city"
    6) "good"
    127.0.0.1:6379> hmset ood name linux kali contos debian 30      #一次性设置ood散列的值
    OK
    127.0.0.1:6379> hgetall ood
    1) "name"
    2) "linux"
    3) "kali"
    4) "contos"
    5) "debian"
    6) "30"
    127.0.0.1:6379> hget ood name
    "linux"
    127.0.0.1:6379> hmget ood name
    1) "linux"
    127.0.0.1:6379> hdel ood name                   #删除ood里的name
    (integer) 1
    127.0.0.1:6379> hgetall ood
    1) "kali"
    2) "contos"
    3) "debian"
    4) "30"
    127.0.0.1:6379> hexists ood kali                #散列ood是否存在kali,存在
    (integer) 1
    127.0.0.1:6379> hexists ood no                  #散列ood是否存在no,不存在
    (integer) 0
    
    #列表类型
    127.0.0.1:6379> lpush test 1                #列表名为test,从左边加入1
    (integer) 1
    127.0.0.1:6379> lpush test 2
    (integer) 2
    127.0.0.1:6379> rpush test -1               #列表名为test,从右边加入-1
    (integer) 3
    127.0.0.1:6379> llen test               #列表长度
    (integer) 3
    127.0.0.1:6379> lpop test               #左边出去一个数
    "2"
    127.0.0.1:6379> llen test
    (integer) 2
    127.0.0.1:6379> rpop test               #右边出去一个数
    "-1"
    127.0.0.1:6379> lpop test
    "1"
    127.0.0.1:6379> llen test
    (integer) 0 
    127.0.0.1:6379> lpush test 1
    (integer) 1
    127.0.0.1:6379> lpush test 2
    (integer) 2
    127.0.0.1:6379> lpush test 3
    (integer) 3
    127.0.0.1:6379> rpush test 8
    (integer) 4
    127.0.0.1:6379> rpush test 9
    (integer) 5
    127.0.0.1:6379> lrange test 2 3             #列表下标从0开始计算,显示第三个数和第四个数
    1) "1"
    2) "8"
    127.0.0.1:6379> lrange test 0 3
    1) "3"
    2) "2"
    3) "1"
    4) "8"
    127.0.0.1:6379> lrem test 1 3               #左数删除1个3
    (integer) 1
    127.0.0.1:6379> llen test
    (integer) 4
    127.0.0.1:6379> lrange test 0 3
    1) "2"
    2) "1"
    3) "8"
    4) "9"
    127.0.0.1:6379> lindex test 2               #获取2的下标
    "8"
    127.0.0.1:6379> ltrim test 0 2              #test取截取出来的下标0到2对应的值
    OK
    127.0.0.1:6379> llen test
    (integer) 3
    127.0.0.1:6379> lrange test 0 2
    1) "2"
    2) "1"
    3) "8"
    
    #集合类型
    127.0.0.1:6379> sadd linux a b c d e a b            #增加linux集合,集合内容为a b c d e a b
    (integer) 5
    127.0.0.1:6379> sadd kali A F I S
    (integer) 4
    127.0.0.1:6379> srem linux d e                  #删除linux集合中的d e元素
    (integer) 2
    127.0.0.1:6379> smembers linux                  #查看linux的元素
    1) "b"
    2) "c"
    3) "a"
    127.0.0.1:6379> sismember linux d               #查看d是否是集合linux的元素,否
    (integer) 0
    127.0.0.1:6379> sismember linux a               #查看a是否是集合linux的元素,是
    (integer) 1
    127.0.0.1:6379> sadd centos a d e c b
    (integer) 5
    127.0.0.1:6379> smembers linux
    1) "b"
    2) "c"
    3) "a"
    127.0.0.1:6379> smembers centos
    1) "e"
    2) "b"
    3) "c"
    4) "a"
    5) "d"
    127.0.0.1:6379> sdiff linux centos              #取差集,linux集合被包含在centos里面,所以没有(linux-centos)
    (empty list or set)
    127.0.0.1:6379> sinter linux centos             #取交集
    1) "b"
    2) "c"
    3) "a"
    127.0.0.1:6379> sdiff centos linux              #centos-linux
    1) "e"
    2) "d"
    127.0.0.1:6379> sunion linux centos             #取并集
    1) "e"
    2) "b"
    3) "c"
    4) "d"
    5) "a"
    
    #有序集合
    127.0.0.1:6379> zadd test1 10 a                     #增加test1有序集合,分数为10 等级为a
    (integer) 1
    127.0.0.1:6379> zadd test2 20 b
    (integer) 1
    127.0.0.1:6379> zadd test1 5 c
    (integer) 1
    127.0.0.1:6379> zrem test1 b                        #移除test1的等级b的值
    (integer) 1
    127.0.0.1:6379> zscore test1 a                      #查看test1的等级a的值
    "10"
    127.0.0.1:6379> zrange test1 0 1                    #查看test1第一个和第二个的值
    1) "c"
    2) "a"
    127.0.0.1:6379> zrangebyscore test1 5 10                #根据分数查看对应的的等级
    1) "c"
    2) "a"
  • 相关阅读:
    Codeforces Beta Round #18 (Div. 2 Only) C. Stripe 前缀和
    Codeforces Round #309 (Div. 1) C. Love Triangles dfs
    Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 构造
    Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合
    Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题
    Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks 字符串水题
    Codeforces Beta Round #13 E. Holes 分块暴力
    Codeforces Beta Round #12 (Div 2 Only) D. Ball sort/map
    Codeforces Beta Round #6 (Div. 2 Only) E. Exposition multiset
    Codeforces Beta Round #5 E. Bindian Signalizing 并查集
  • 原文地址:https://www.cnblogs.com/biaopei/p/7730533.html
Copyright © 2011-2022 走看看