• redis的数据类型


    String
    Hash
    List
    Set
    Sorted set
     
    自己去扩展
    Bit arrays
    HyperLogLogs
    Streams(redis5新加的类型)
     

    String字符串

     
    string是redis最基本的类型,一个key对应一个value。一个键最大能存储512MB。
    String类型有如下基本操作:
    set
    get
    incr
    incrby
    decr
    decrby
    mset
    mget
    append
    getset
    setex
    setnx
    del
    setrange
    strlen
    getrange
     
    举例:定义一个或者多个键值对
    127.0.0.1:6379> set name linuxyn
    OK
    127.0.0.1:6379> get name "linuxyn"
    127.0.0.1:6379>
    127.0.0.1:6379> mset id 1 name tom age 20 gender male
    OK
    127.0.0.1:6379> mget id name age gender
    1) "1"
    2) "tom"
    3) "20"
    4) "male"
    127.0.0.1:6379>
    

      

     
    计数器的例子:粉丝数量
    127.0.0.1:6379> INCR fans
    (integer) 1
    127.0.0.1:6379> INCR fans
    (integer) 2
    127.0.0.1:6379> INCR fans
    (integer) 3
    127.0.0.1:6379> INCR fans
    (integer) 4
    127.0.0.1:6379> INCR fans
    (integer) 5
    127.0.0.1:6379>
    127.0.0.1:6379> get fans
    "5"
    127.0.0.1:6379> DECR fans
    (integer) 4
    127.0.0.1:6379> DECR fans
    (integer) 3
    127.0.0.1:6379> get fans
    "3"
    127.0.0.1:6379> INCRBY fans 100
    (integer) 103
    127.0.0.1:6379> DECRBY fans 100
    (integer) 3
    127.0.0.1:6379> get fans
    "3"
    

      

     
    应用场景:
    • 非常基本的键值对存储
    • 计数器,如互联网当中,点击量,访问量,关注量;微博数、粉丝数;网页游戏应用中的血量、蓝量等
     

    Hash字典

    我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器。
      所以该类型非常适合于存储值对象的信息。如Username、Password和Age等。如果Hash中包含很少的字段,那么该类型的数据也将仅占用很少的磁盘空间。每一个Hash可以存储4294967295个键值对。
    hset
    hsetnx
    hmset
    hdel
    del
    hincrby
    hget
    hmget
    hlen
    hexists
    hgetall
    hkeys
    hvals
     
    举例:
    127.0.0.1:6379> HSET stu1 name tom age 17 gender maile
    (integer) 3
    127.0.0.1:6379> HSET stu2 name linuxyn age 32 gender female
    (integer) 3
    127.0.0.1:6379> HGETALL stu1
    1) "name"
    2) "tom"
    3) "age"
    4) "17"
    5) "gender"
    6) "maile"
    127.0.0.1:6379> HGETALL stu2
    1) "name"
    2) "linuxyn"
    3) "age"
    4) "32"
    5) "gender"
    6) "female"
    127.0.0.1:6379> HDEL stu1 name age gender
    (integer) 3
    

      

     
     
    应用场景:
    最接近于MySQL表结构的数据类型,可以提前把mysql的数据提前灌入到redis当中,进行数据预热。
    存储部分变更的数据,如用户的信息。至于如何将MySQL数据迁移到Redis呢? 参考将MySQL数据迁移到Redis
     
    思路:
     
    mysql> select * from stu;
    +------+---------+------+--------+
    | id   | name    | age  | gender |
    +------+---------+------+--------+
    |    1 | tom     |   17 | male   |
    |    2 | linuxyn |   32 | female |
    +------+---------+------+--------+
    2 rows in set (0.00 sec)
    
    mysql>
    

      

    如何把他们转换为下面的,由于key值不能重复。这里我们把key的结尾都加上id
    HSET stu:1 id 1 name tom age 17 gender maile
    HSET stu:2 id 2 name linuxyn age 32 gender female
     
    我们利用concat进行拼接
    mysql> select concat('hset stu:',id, ' id ',id, ' name ', name,' age ',age,' gender ', gender) from stu;;
    +----------------------------------------------------------------------------------+
    | concat('hset stu:',id, ' id ',id, ' name ', name,' age ',age,' gender ', gender) |
    +----------------------------------------------------------------------------------+
    | hset stu:1 id 1 name tom age 17 gender male                                      |
    | hset stu:2 id 2 name linuxyn age 32 gender female                                |
    +----------------------------------------------------------------------------------+
    2 rows in set (0.00 sec) 
    

      

     然后再把这些结果,放到redis里面批量执行就ok了。这样就实现了redis作为mysql缓存的功能。
     
     

    LIST列表

     
    List类型是按照插入顺序排序的字符串链表。和数据结构中的普通链表一样,我们可以在其头部(left)和尾部(right)添加新的元素。在插入时,如果该键并不存在,Redis将为该键创建一个新的链表。与此相反,如果链表中所有的元素均被移除,那么该键也将会被从数据库中删除。List中可以包含的最大元素数量是4294967295。
    lpush
    lpushx
    linsert
    rpush
    rpushx
    rpoplpush
    del
     
    lrem
    ltrim
    lset
    rpoplpush
    lrange
    lpop
    lindex
     
    127.0.0.1:6379> LPUSH wechat 'today is mon'
    (integer) 1
    127.0.0.1:6379> LPUSH wechat 'today is tu'
    (integer) 2
    127.0.0.1:6379> LPUSH wechat 'today is we'
    (integer) 3
    127.0.0.1:6379> LPUSH wechat 'today is th'
    (integer) 4
    127.0.0.1:6379> LPUSH wechat 'today is fr'
    (integer) 5
    127.0.0.1:6379> LPUSH wechat 'today is sa'
    (integer) 6
    127.0.0.1:6379> LPUSH wechat 'today is su'
    (integer) 7
    127.0.0.1:6379> LRANGE wechat 0 -1
    1) "today is su"
    2) "today is sa"
    3) "today is fr"
    4) "today is th"
    5) "today is we"
    6) "today is tu"
    7) "today is mon"
    

      

    通过下标来获取列表中的所有的元素,可以看出顺序为:后进先出
     
     
    应用场景: 朋友圈应用
    消息队列系统
    比如sina微博: 在Redis中我们的最新微博ID使用了常驻缓存,这是一直更新的。但是做了限制不能超过5000个ID,因此获取ID的函数会一直询问Redis。只有在start/count参数超出了这个范围的时候,才需要去访问数据库。 
    系统不会像传统方式那样“刷新”缓存,Redis实例中的信息永远是一致的。SQL数据库(或是硬盘上的其他类型数据库)只是在用户需要获取“很远”的数据时才会被触发,而主页或第一个评论页是不会麻烦到硬盘上的数据库了。
     
    例子:微信朋友圈应用
    后进先出,最新发的朋友圈最先被看到。
     
     

    set(集合)

    Set类型看作为没有排序的字符集合。Set可包含的最大元素数量是4294967295。如果多次添加相同元素,Set中将仅保留该元素的一份拷贝。
    sadd
    spop
    srem
    smove
    sismember
    smembers
    scard
    srandmember
    sdiff
    sdiffstore
    sinter
    sinterstore
    sunion
    sunionstore
     
    应用场景:
    案例: 
    在微博应用中,可以将一个用户所有的关注人存在一个集合中,将其所有粉丝存在一个集合。Redis还为集合提供了求交集、并集、差集等操作,可以非常方便的实现如共同关注、共同喜好、二度好友等功能,对上面的所有集合操作,你还可以使用不同的命令选择将结果返回给客户端还是存集到一个新的集合中。
     
    定义两个用户:Linuxyn和alex,后面为他们的朋友。可以快速判断所有的好友,共同的好友,以及只属于linuxyn的好友,以及只属于alex的好友
    127.0.0.1:6379> SADD linuxyn tom1 tom2 tom3
    (integer) 3
    127.0.0.1:6379> SADD alex tom2 tom3 lily
    (integer) 3
    127.0.0.1:6379>
    127.0.0.1:6379> SUNION linuxyn alex
    1) "tom3"
    2) "tom2"
    3) "tom1"
    4) "lily"
    127.0.0.1:6379>
    127.0.0.1:6379> SINTER linuxyn alex
    1) "tom3"
    2) "tom2"
    127.0.0.1:6379>
    127.0.0.1:6379> SDIFF linuxyn alex
    1) "tom1"
    127.0.0.1:6379>
    127.0.0.1:6379> SDIFF alex linuxyn
    1) "lily"
    

      

     

    SortedSet(有序集合)

    Sorted-Sets中的每一个成员都会有一个分数(score)与之关联,Redis正是通过分数来为集合中的成员进行从小到大的排序。成员是唯一的,但是分数(score)却是可以重复的。
    zadd
    zrem
    zincrby
    zrange
    zrank
    zcard
    zcount
    zscore
    zrangebyscore
    zremrangebyscore
    zremrangebyrank
    zrevrange
    zrevrangebyscore
    zrevrangebyscore
    应用场景:
    排行榜应用,取TOP N操作 
    这个需求与上面需求的不同之处在于,前面操作以时间为权重,这个是以某个条件为权重,比如按顶的次数排序,这时候就需要我们的sorted set出马了,将你要排序的值设置成sorted set的score,将具体的数据设置成相应的value,每次只需要执行一条ZADD命令即可。
     
    127.0.0.1:6379> zadd music 0  '成都'  0  '燃烧我的卡路里'  0  '探清水河'  0 '我要你'  0 '猎户星座'
    (integer) 5
    127.0.0.1:6379>
    127.0.0.1:6379>
    127.0.0.1:6379>
    127.0.0.1:6379>
    127.0.0.1:6379> ZINCRBY music 10000 '成都'
    "10000"
    127.0.0.1:6379> ZINCRBY music 200 '燃烧我的卡路里'
    "200"
    127.0.0.1:6379> ZINCRBY music 100 '探清水河'
    "100"
    127.0.0.1:6379> ZINCRBY music 39888 '我要你'
    "39888"
    127.0.0.1:6379> ZINCRBY music 9999 '猎户星座'
    "9999"
    127.0.0.1:6379>
    127.0.0.1:6379> ZREVRANGE music 0 4 withscores
    我要你
    39888
    成都
    10000
    猎户星座
    9999
    燃烧我的卡路里
    200
    探清水河
    100
    

      

     
    这里模拟用户每点击播放一次加1,这里直接加10000,39888等。结果可以看出这几首歌曲的一个热度以及以及播放次数。
     
    注意:这里的结果可能为乱码,解决方案客户端连接redis用下面这种方式:redis-cli --raw

  • 相关阅读:
    pgspider 一些ppt 截图
    postgres cassandra_fdw 扩展试用
    使用 postgres s3 fdw + cube.js 分析 csv 数据
    cube.js 集成s3 的一种方法
    postgres s3 fdw 试用
    cube.js 集成 elasticsearch 的一种变通方法
    使用postgres_fdw 串接elasticsearch fdw
    postgres elasticsearch fdw 学习
    使用vcpkg 管理c&&c++ 包
    postgres pg_cron 扩展连接远程pg server
  • 原文地址:https://www.cnblogs.com/yang-ning/p/11642145.html
Copyright © 2020-2023  润新知