Import & redis common commands

  • redis Download (docker installation) and command

Pull redis Mirror (alpine classic version)

docker  pull  redis:4-alpine

This unit is mapped to a virtual 6379 6379

docker run -it -p 6379:6379 --name redis(名字) redis:4-alpine(下载的镜像名)

Enter redis

docker exec -it redis /bin/sh

Interact with redis

redis-cli

A. Redis properties

  1. high speed

    Memory-based, C language, single-threaded architecture, performance and elegance set in one of the codes

  2. Based on the data structure of key-value pairs of server
    REmote DIctionary Server. 值可以是字符串/哈希/列表/集合/有序集合, Bitmap等等
  1. Feature-rich

    The key feature releases expired - subscribe function (for message system), Lua script, simple transaction, pipeline (Pipeline)

  2. Simple and stable

    Less code, an earlier version of 20000 lines later. Version 3.0 features added to the cluster, 50,000 lines. Single-threaded model

  3. Multi-language client
  4. Endurance of

    (RDB and AOF embodiment) stored in the hard disk

  5. Master-slave replication
  6. High availability and distributed

Two. Redis usage scenarios

  1. Cache / session
  2. Ranking System
  3. Counter Application

    Video playback / Views Concurrent too big, big challenge for traditional database

  4. Social network

    Like up / down, fans, common friends / preferences and more complicated data

  5. Message queuing system

    Mainly for business decoupling

Three. Redis installed

Use docker install redis

四. Redis Shell

  1. redis-server start Redis

    The default port 6379 is used --port xxxto specify the port number
    specified configuration file

  2. redis-cli command line interface

Five. Redis use

1. Global Command

keys *        查看所有key

dbsize         key的数量

exists +key      判断存在几个

del  +key           删除几条

expire +key +seconds    设置过期时间

type +key                查看一个可以对应的value类型

set +key +value +ex      添加时可同时设置他的过期时间 ex填过期时间

get +key          获取一个key的值

2. The data structure and inner coding

2.1 Data Structure

  1. string string
    key 都是字符串类型, 而且其他几种数据结构都是在字符串类型基础上构建的.
    
    字符串类型的值实际上可以是简单或复杂的字符串(JSON/XML), 数字, 甚至是二进制(图片, 音频, 视频, 最大512M)

    mset, 批量设置值, 可以提高开发效率
    mget, 批量获取值
    
    计数, 
    incr key, ++      设置自增 (只有value是数字时)     例;  incer like:news:1001 有人喜欢就加一
    decr key, --      设置自减
    incrby key xx,    设置每次加几个
    decrby key xx     设置每次减几个
开发中合理的key名, 业务名:对象名:id:属性
  1. hash hash
    hset key field value    存入键值对(一个key 可存入多个key value分多次存)
    hget key field          取出键值对 

    hdel key field          删除摸个键值对属性
    hlen key                查看可以有多少属性
    hmget key field         批量获取key的多个属性
    hmset key field value   批量设置key的多个属性的键值对
    hexists key field       判断key的属性是否存在
    hkeys key               列出这个key 所有属性的名 
    hvals key               取出这个key 对应所有的属性的值
    hgetall key             列出这个key的所有属性和值
  1. list list

    Storing a plurality of ordered strings

     rpush key value ...     右边插入元素           
     lpush key value ...     左边插入元素
     lrange key start end    取出数据 +开始(0)和结束(?)       
     lpop key 左侧pop        从数组中把左侧pop弹出
     lrem key count value        删除 +key  +count数量的  +value
  2. set collection

    It does not permit duplicates.

    Simultaneously processing a plurality of sets of the intersection / union / difference set

     sadd key element ...   添加数据      
     exists key              是否出现  
     srem key element ... 删除元素
     scard key       元素个数
     sismember key element 判断是否在集合中
     smembers key  列出所有元素
  3. zset ordered set
    zadd key score member [score member...]   添加(可两个一起添加 
    zcard key           查看元素数量               
    zscore key member       查看分数
    zrem key member         移除
    zincrby key score member   指定key 元素 增长   
    zrange   scores  +count  +count  +witnscores 按key 排序后的数量 +witnscores后面会跟分数

Guess you like

Origin www.cnblogs.com/lxx-1843693653/p/11106638.html