当前位置:网站首页>Redis knowledge points

Redis knowledge points

2022-06-09 20:20:00 It is small new

Redis
    NoSQL
        NoSQL, A general term for a non-relational database . With the Internet web2.0 The rise of websites , Traditional relational databases
        Processing web2.0 Website , Especially in large-scale and highly concurrent communities , A lot of difficult problems have been exposed ,NoSQL
        Redis It's the fastest growing , And it is what we must master at present
         A technical
         Relational database : form , That's ok , Column
         Non relational database : Key value pair Map<String,Object>
        
    NoSQL characteristic
        1 decoupling
        2 Easy to expand ( There's no relationship between the data , Very easy to expand )
        3 Large data volume and high performance (Redis One second can write 8 All the data , Read 11 All the data )
        4 Data types are diverse ,5 Common use ,3 Species characteristics ( There's no need to design the database in advance , Take it and use it )
        5 Tradition RDBMS and NoSQL The difference between
             That is, the difference between relational and non relational databases
    NoSQL Four categories
        KV Key value pair
         Document database (bson Format , and json equally )
         Column store database
         Graphical relational database
        
         Solve application scenarios
             Create objects by reflection
             Get and set properties through reflection
             Call methods through reflection
     Classic application scenarios
         Do caching
         Publish subscribe mode
         Distributed lock , share session
        
Redis Installation and use
  ○ install
    Before reference linux Documentation for installation
Use
    Modify the configuration
      bind Set up redis It can be connected remotely , By default, only localhost Connect
      port Specify port number , Just use the default ( When the port conflicts, it needs to be modified )
      daemonize: Set to yes Delegates can start in the background
      requirepass: Specified password  redis The default is protected mode , You can connect remotely only if you have configured a password
      
    stay redis Start in the installation directory redis-server redis.conf
    Connect redis
        redis-cli Connect
        auth password   Verify authority
        keys * View all key
Basic operation
    select Select Library
    set key value Set the value
    get key   Get value
    del key   Delete key
    flushdb Empty the current library
    flushall Empty all libraries
    expire key seconds Set up key The expiration time of
    ttl key     Check the time remaining
  ○ Redis Five basic data types
    String
        set
        get
        del
        setex key expire value Set up kv And specify the expiration time
        setnx key value   Set if key Add if not
        getrange key start stop   Get the specified returned content
        strlen key To obtain the length of the
    List
        lpush key elements Left side insertion means head insertion
        rpush key elements Right side insertion means tail insertion
        lrange key start stop View the contents of the specified index range
        lpop / rpop Out of the stack
        lrem key count element Delete the specified number of element Elements
        ltrim key start stop Intercept the contents of the specified range of the array
        llen key Viewing the length of an array
        rpoplpush source target     source Tail out of stack target Head in stack
        lset key index value Set the value of the specified index
        linsert key before/after element value Add content before and after the specified element
    Set
        sadd key value
        smembers key See all members
        sismemeber key element Determine whether you are a member
        srem Delete
        srandmemeber Get a member randomly
        spop Stack randomly
        sdiff Subtraction
        sinter Find the intersection
        sunion Union
    Hash
        hset key field value Set the element
        hget key field Get elements
        hgetall key Get all
        hdel key field Remove elements
        hlen key field To obtain the length of the
        hkeys Get all key
        hvals Get all val
        hsetnx If no settings exist , To exist is to ignore
        hincrby  += Integer type
        hincrbyfloat += floating-point
    ZSet( The corresponding ones are sorted set)
        In addition to the basic elements, a score Field of , The default in accordance with the score Sort
        zadd key score element Additive elements
        zrange key min max View the data of the specified ranking range .( The default value is from small to large )
        zrevrange key max min View the data of the specified ranking range .( According to the score from big to small )
        zrem Delete
        zrank key element Get ranking
        zrevrank key element Get the rank in reverse order
        zrangebyscore Get the specified score range
        zrevrangebyscore Get the specified score range ( The reverse )
        
    redis Persistence problem
        RDB 
             The default way
            fork A sub thread is created , And the sub thread periodically synchronizes the data of the main thread , Save to local form file
             advantage
                 For the main thread IO No impact , All for redis The read / write performance of is not affected
                 The efficiency of backup and recovery is relatively high , It is very suitable for backing up a large amount of data
             shortcoming
                 Data may be lost , If the data requirements are strict, try not to use this
                fork One thread Double the memory usage
         How do you use it?
            redis This mode is on by default
             You can configure the dump The name and location of the file
            dump_test.rdb
            dir ./
            
         How to actively trigger saving
            save: Save after blocking
            bgsave: Do not block save
         How to restore backups
             Just put dump.rdb Just put it in the designated position
    AOF
         By appending set How to log , persist , The recovery can be completed by performing the recovery according to the log
         advantage
             Data is relatively complete
         shortcoming
             The log file is too large
             Slow backup and recovery
    rewrite Mechanism
         When the file is too large , Would be right aof Text plus compression , Keep only the smallest recoverable file size
     Application scenarios
         High requirements for data completion , When the performance is not so high
        
     How do you use it?
         Turn on AOF The configuration can be
        appendonly yes
        appendfilename "appendonly.aof"
        
        
        
     Delete mechanism
         timer
             For every one key Start a timer , Point to point deletion
            CPU Large occupation , Trade time for space
         Lazy deletion
             Visit this key It is not until the expiration date , Delete expired
             Large memory consumption , Trade space for time
         Delete periodically
             To perform a second hz Delete times
             When deleting, delete the database one by one
             It's not completely deleted , Is sample deletion , The expired memory usage after sampling is less than 25% stop it
         Eliminate the eviction mechanism
             What to do if the memory is full
             classification
                 Eliminate those that have not been used for a long time key
                 Eliminate the least frequently used key
                 Eliminate those that are about to expire key
                 To be eliminated at random
             How do you use it?
                 Use the maximum memory in the configuration
                 Specify obsolescence policy
         Main standby mode
         Sentinel mode
         Cluster building
         Common cluster architecture
            master/slave
             De centralization
            

Summarize the key points
    redis summary
     Installation and basic configuration
     Five data types
     Persistence
        rdb and aof
    jedis
     Understand the deletion and obsolescence mechanism
    redis Main standby mode
         One master and two subordinates
         It's passed down from generation to generation
         Going to
     Sentinel mode
     Cluster building
    
            
            

原网站

版权声明
本文为[It is small new]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206092012427723.html