zoukankan      html  css  js  c++  java
  • 缓存数据库(nosql)-redis 1

    ubuntu安装redis-server

    sudo apt update

    sudo apt install  redis-server

    python:pip install redis

    1、简介

      redis是一个k-v存储系统,与memcached类似,但支持五种数据类型:String字符串、Hash哈希表、List列表、Set集合、SortedSet有序集合

    五种数据类型均支持push/pop、add/remove,交集、并集、差集等操作,这些操作都是原子性操作。

      redis、memcached将数据缓存到内存中。但是redis会周期性将数据写入硬盘或把修改操作写入追加的记录文件,并在此基础上实现了master-slave同步

      redis有缓存功能,也能做消息队列

      redis支持数据持久化,电脑关机数据还在

      默认端口:6379,memcached自己指定

    2、redis的优势?

      a、因为数据存在内存,因此速度快,查找和操作时间复杂度是 O(1)

      b、支持的数据类型多

      c、支持事务

      d、功能多:缓存,设置过期时间,消息队列

      

      e、memcached值是简单的字符串,redis支持的数据类型多

      f、redis速度更快

      g、redis可支持数据的持久化

    3、使用

    linux操作:

    启动:redis-server

    连接:redis-cli

    String:help  set

    SET key value [EX seconds] [PX milliseconds] [NX|XX]
      summary: Set the string value of a key
      since: 1.0.0
      group: string
    127.0.0.1:6379> get name
    "lyb"
    127.0.0.1:6379> type name
    string
    127.0.0.1:6379> set nn 123
    OK
    127.0.0.1:6379> type nn
    string
    127.0.0.1:6379> keys *
    1) "name"
    2) "nn"
    127.0.0.1:6379> ttl nn  #查看过期时间,-1未设置过期时间
    (integer) -1
    127.0.0.1:6379> set qq www px 123123123
    OK
    127.0.0.1:6379> ttl qq    #秒
    (integer) 123119
    127.0.0.1:6379> pttl qq  #毫秒
    (integer) 123113045
    127.0.0.1:6379> set qq 123  #key不存在设置,存在就覆盖
    OK
    127.0.0.1:6379> get qq
    "123"
    127.0.0.1:6379> set qq 456 nx  #nx表示key存在就不设置,不存在就设置
    (nil)
    127.0.0.1:6379> get qq
    "123"
    127.0.0.1:6379> set qq 456 xx  #xx表示key存在设置,不存在不设置
    OK
    127.0.0.1:6379> get qq
    "456"

    mset 批量设置

    MSET key value [key value ...]
      summary: Set multiple keys to multiple values
      since: 1.0.1
      group: string
    
    127.0.0.1:6379> mset q1 qq q2 qw
    OK

    setex 设置过期时间

    SETEX key seconds value
      summary: Set the value and expiration of a key
      since: 2.0.0
      group: string

    setnx

    SETNX key value
      summary: Set the value of a key, only if the key does not exist
      since: 1.0.0
      group: string

    mget 批量获取

    MGET key [key ...]
      summary: Get the values of all the given keys
      since: 1.0.0
      group: string
    
    127.0.0.1:6379> mget q1 name ddd
    1) "qq"
    2) "lyb"
    3) (nil)

    getset 设置新值返回旧值

    GETSET key value
      summary: Set the string value of a key and return its old value
      since: 1.0.0
      group: string
    
    127.0.0.1:6379> getset name 333
    "lyb"

    getrange 获取字符串的子串

    GETRANGE key start end
      summary: Get a substring of the string stored at a key
      since: 2.4.0
      group: string
    
    127.0.0.1:6379> getrange name 2 
    (error) ERR wrong number of arguments for 'getrange' command
    127.0.0.1:6379> getrange name 2 10
    "3"
    127.0.0.1:6379> get qq
    "456"
    127.0.0.1:6379> getrange qq 0 -1
    "456"

    setrange

    SETRANGE key offset value
      summary: Overwrite part of a string at key starting at the specified offset
      since: 2.2.0
      group: string
    
    127.0.0.1:6379> setrange qq 1 ererer
    (integer) 7
    127.0.0.1:6379> get qq
    "4ererer"

    strlen 字节长度(汉字是三个)

    127.0.0.1:6379> strlen name
    (integer) 3

    incr 值是数字时一次 +1  #decr 是 -1

    incrby  increment  指定加多少  #decrby  decrment   减多少

    INCR key
      summary: Increment the integer value of a key by one
      since: 1.0.0
      group: string
    
    127.0.0.1:6379> incr name
    (integer) 334
    127.0.0.1:6379> incr name
    (integer) 335
    127.0.0.1:6379> incr name
    (integer) 336

    incrbyfloat 增加指定浮点数

    INCRBYFLOAT key increment
      summary: Increment the float value of a key by the given amount
      since: 2.6.0
      group: string
    
    127.0.0.1:6379> INCRBYFLOAT name 1.2
    "337.20000000000000001"
    127.0.0.1:6379> INCRBYFLOAT name 1.2
    "338.40000000000000002"
    127.0.0.1:6379> INCRBYFLOAT name 1.2
    "339.60000000000000003"

    setbit 和 bitcount:

      1、统计某天网站的上线的用户数量

      2、统计某用户访问网站的天数

    以1为例:以偏移量为用户的 id

    127.0.0.1:6379> set ww ''
    OK
    127.0.0.1:6379> bitcount ww
    (integer) 0

    来一个用户,将用户id处的bit设为1

    127.0.0.1:6379> setbit ww 5000 1
    (integer) 0
    127.0.0.1:6379> setbit ww 100 1
    (integer) 0
    127.0.0.1:6379> setbit ww 100145 1
    (integer) 0
    127.0.0.1:6379> bitcount ww    #今天有3位用户登录
    (integer) 3

    查看某些用户是否登录

    127.0.0.1:6379> getbit ww 355
    (integer) 0
    127.0.0.1:6379> getbit ww 5000
    (integer) 1

    性能:1 byte = 8 bit

        1亿 bit = 12500000 字节 = 12207 k = 12 M

    哈希

    hset 

    HSET key field value
      summary: Set the string value of a hash field
      since: 2.0.0
      group: hash
    
    127.0.0.1:6379> hset dd name lyb
    (integer) 1127.0.0.1:6379> hget dd name
    "lyb"

    hmset 批量设置,hmget 批量获取

    HMSET key field value [field value ...]
      summary: Set multiple hash fields to multiple values
      since: 2.0.0
      group: hash
    
    127.0.0.1:6379> hmset dd age 23 addr bj
    OK
    127.0.0.1:6379> hmget dd age addr name
    1) "23"
    2) "bj"
    3) "lyb"

    hgetall 获取所有键-值,hkeys 所有键,hlen 长度

    127.0.0.1:6379> hgetall dd
    1) "name"
    2) "lyb"
    3) "age"
    4) "23"
    5) "addr"
    6) "bj"
    127.0.0.1:6379> hkeys dd
    1) "name"
    2) "age"
    3) "addr"
    127.0.0.1:6379> hlen dd
    (integer) 3

    hvals 所有值,hexists 键是否存在

    127.0.0.1:6379> hvals dd
    1) "lyb"
    2) "23"
    3) "bj"127.0.0.1:6379> hexists dd name
    (integer) 1  #存在
    127.0.0.1:6379> hexists dd named
    (integer) 0  #不存在

    hdel 删除多个键

    HDEL key field [field ...]
      summary: Delete one or more hash fields
      since: 2.0.0
      group: hash

    增加

    HINCRBY key field increment
      summary: Increment the integer value of a hash field by the given number
      since: 2.0.0
      group: hash
    
    127.0.0.1:6379> help HINCRBYFLOAT
    
      HINCRBYFLOAT key field increment
      summary: Increment the float value of a hash field by the given amount
      since: 2.6.0
      group: hash

    hscan

    HSCAN key cursor [MATCH pattern] [COUNT count]
      summary: Incrementally iterate hash fields and associated values
      since: 2.8.0
      group: hash

    List列表

    lpush ,lrange,lpop

    127.0.0.1:6379> lpush mylist a b c  #等同于 lpush a,lpush b,lpush c,从左开始插入列表
    (integer) 3
    127.0.0.1:6379> lrange mylist 0 -1
    1) "c"
    2) "b"
    3) "a"
    127.0.0.1:6379> lpop mylist
    "c"
    127.0.0.1:6379> lpop mylist
    "b"
    127.0.0.1:6379> lpop mylist
    "a"
    127.0.0.1:6379> lpop mylist
    (nil)

    rpush

    127.0.0.1:6379> rpush mylist d e
    (integer) 5
    127.0.0.1:6379> lrange mylist 0 -1
    1) "c"
    2) "b"
    3) "a"
    4) "d"
    5) "e"

    lpushx 列表存在才添加

    LPUSHX key value
      summary: Prepend a value to a list, only if the list exists
      since: 2.2.0
      group: list

    llen

    LLEN key
      summary: Get the length of a list
      since: 1.0.0
      group: list

    linsert

    LINSERT key BEFORE|AFTER pivot value
      summary: Insert an element before or after another element in a list
      since: 2.2.0
      group: list
    
    127.0.0.1:6379> lrange mylist 0 -1
    1) "f"
    2) "c"
    3) "b"
    4) "a"
    5) "d"
    6) "e"
    127.0.0.1:6379> linsert mylist before 2 charu  #值不存在返回-1
    (integer) -1
    127.0.0.1:6379> lrange mylist 0 -1
    1) "f"
    2) "c"
    3) "b"
    4) "a"
    5) "d"
    6) "e"
    127.0.0.1:6379> linsert mylist after b charu
    (integer) 7  #成功返回最终长度
    127.0.0.1:6379> lrange mylist 0 -1
    1) "f"
    2) "c"
    3) "b"
    4) "charu"
    5) "a"
    6) "d"
    7) "e"

    lset

    LSET key index value
      summary: Set the value of an element in a list by its index
      since: 1.0.0
      group: list
    
    127.0.0.1:6379> lset mylist 10 3
    (error) ERR index out of range
    127.0.0.1:6379> lset mylist 5 3
    OK
    127.0.0.1:6379> lrange mylist 0 -1
    1) "f"
    2) "c"
    3) "b"
    4) "charu"
    5) "a"
    6) "3"
    7) "e"

    lrem

    LREM key count value
      summary: Remove elements from a list
      since: 1.0.0
      group: list
    
    127.0.0.1:6379> lrange mylist 0 -1
    1) "a"
    2) "f"
    3) "f"
    4) "c"
    5) "b"
    6) "charu"
    7) "a"
    8) "3"
    9) "e"
    127.0.0.1:6379> lrem mylist  1 a
    (integer) 1
    127.0.0.1:6379> lrange mylist 0 -1
    1) "f"
    2) "f"
    3) "c"
    4) "b"
    5) "charu"
    6) "a"
    7) "3"
    8) "e"
    127.0.0.1:6379> lrem mylist  2 f
    (integer) 2
    127.0.0.1:6379> lrange mylist 0 -1
    1) "c"
    2) "b"
    3) "charu"
    4) "a"
    5) "3"
    6) "e"

    lpop

    LPOP key
      summary: Remove and get the first element in a list
      since: 1.0.0
      group: list

    lindex

    LINDEX key index
      summary: Get an element from a list by its index
      since: 1.0.0
      group: list

    ltrim

    LTRIM key start stop
      summary: Trim a list to the specified range
      since: 1.0.0
      group: list
    127.0.0.1:6379> lrange mylist 0 -1
    1) "c"
    2) "b"
    3) "charu"
    4) "a"
    5) "3"
    6) "e"
    127.0.0.1:6379> ltrim mylist 2 5
    OK
    127.0.0.1:6379> lrange mylist 0 -1
    1) "charu"
    2) "a"
    3) "3"
    4) "e"

    rpoplpush

    RPOPLPUSH source destination
      summary: Remove the last element in a list, prepend it to another list and return it
      since: 1.2.0
      group: list
    
    127.0.0.1:6379> lrange mylist 0 -1
    1) "charu"
    2) "a"
    3) "3"
    4) "e"
    127.0.0.1:6379> lpush my 123 445
    (integer) 2
    127.0.0.1:6379> RPOPLPUSH mylist my
    "e"
    127.0.0.1:6379> lrange mylist 0 -1
    1) "charu"
    2) "a"
    3) "3"
    127.0.0.1:6379> lrange my 0 -1
    1) "e"
    2) "445"
    3) "123"

    blpop

    BLPOP key [key ...] timeout
      summary: Remove and get the first element in a list, or block until one is available
      since: 2.0.0
      group: list
    
    127.0.0.1:6379> blpop my 10
    1) "my"
    2) "e"
    127.0.0.1:6379> blpop my 10
    1) "my"
    2) "445"
    127.0.0.1:6379> blpop my 10
    1) "my"
    2) "123"
    127.0.0.1:6379> blpop my 10
    (nil)
    (10.07s)
    渐变 --> 突变
  • 相关阅读:

    logging 模块和日志
    re 模块和正则表达式
    collections 模块
    hashlib 模块
    序列化 json 和 pickle
    C++之以分隔符的形式获取字符串
    读取配置文件所在路径时的一种常见错误
    char和achar互转
    C++之ARX,Acstring,ACahr转char
  • 原文地址:https://www.cnblogs.com/lybpy/p/8684949.html
Copyright © 2011-2022 走看看