当前位置:网站首页>Redis quick start

Redis quick start

2022-07-06 17:20:00 bestkasscn

Redis

Concept

redis It is a high-performance non relational database . The storage form is key-value.

Characteristics of non relational database

  1. There is no correlation between the data
  2. Data stored in memory

Download and install

  1. Official website :https://redis.io
  2. Chinese net :http://www.redis.net.cn
  3. Decompression can be used directly
    • redis.windows.conf: The configuration file
    • redis-cli.exe:redis The client of
    • redis-server.exe:redis Server side of

The command operation

  1. redis Data structure of

    • redis What's stored is :key-value Formatted data , among key All strings ,value Yes 5 Different data structures
      • value Data structure of
        • String type :string
        • Hash type :hash:map Format
        • List the type :list:linkedlist Format
        • Collection types :set;
        • Ordered set type :sortedset
  2. String type string

    1. Storage :set key value
    2. obtain :get key
    3. Delete :del key
  3. Hash type hash

    1. Storage :hset key field value

    2. obtain :hget key field

      ​ hgetall key( Get all field Of value)

    3. Delete :hdel key field

  4. List the type list Head and tail interpolation can be used

    1. Storage :lpush key value( The first interpolation )

      ​ rpush key value( The tail interpolation )

    2. obtain :lrange key start end : Scope acquisition

    3. Delete :lpop key Delete the leftmost element , And return the element to

      ​ rpop key Delete the rightmost element , And return the element to

  5. Collection types set

    1. Storage :sadd key value
    2. obtain :smembers key
    3. Delete :srem key value
  6. General Command

    1. keys *: Query all the keys
    2. type key: Get the key corresponding to value type
    3. del key: Delete key

Persistence

  1. redis It's a memory database , When redis Server restart , Or restart the computer , Data will be lost , We can redis The data in memory is persisted and saved to the file on the hard disk

  2. redis Persistence mechanism :

    1. RDB: Default mode , There is no need to configure , This mechanism is used by default

      At certain intervals , testing key The change of , Then persist the data

    2. AOF: How to log , The operation of each command can be recorded . You can persist data after every operation

Jedis

  1. Introduce dependencies

    <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.7.0</version>
            </dependency>
    
  2. test

    public class JedisTest {
          
        private Jedis jedis;
    
        @BeforeEach
        void setup(){
          
    //  Establishing a connection 
            jedis = new Jedis("127.0.0.1",6379);
    //  Select database 
            jedis.select(0);
        }
    
        @Test
        void testString(){
          
            // In the data 
            String result = jedis.set("name","zhangsan");
            System.out.println("result=" + result);
            // get data 
            String name = jedis.get("name");
            System.out.println("name=" + name);
        }
    
        @AfterEach
        void tearDown(){
          
            if (jedis != null){
          
                jedis.close();
            }
        }
    }
    

SpringDataRedis

Spring-data-redis yes spring Part of a large family , Provided in srping Access through simple configuration in the application redis service , Yes reids Underlying development package (Jedis, JRedis, and RJC) It's highly encapsulated ,RedisTemplate Provides redis Various operations 、 Exception handling and serialization , Support publish subscription , Also on spring 3.1 cache Implemented .

spring-data-redis in the light of jedis The following functions are provided :

 Connection pool automatic management , Provides a highly encapsulated “RedisTemplate” class 

 in the light of jedis There are a lot of api It is classified and packaged , Encapsulate the same type of operation as operation Interface 

    ValueOperations: Simple K-V operation 

    SetOperations:set Type data operation 

    ZSetOperations:zset Type data operation 

    HashOperations: in the light of map Type of data operation 

    ListOperations: in the light of list Type of data operation 
原网站

版权声明
本文为[bestkasscn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060928273041.html