Redis cache and related instructions

Time:2021-7-16

Redis cache

System optimization strategy:

Role: the introduction of caching mechanism can effectively reduce the frequency of user access to physical devices, so as to improve the response speed
Redis cache and related instructions

How to design cache

1) How to store cache data? What kind of data structure should be used? Uniqueness of K-V key
2) The capacity of cache data should be maintained dynamically. Unnecessary data should be deleted in advance. LRU algorithm / LFU algorithm / random algorithm / TTL algorithm
3) Cache data is saved to memory, but the characteristic of memory is to erase when power is off
4) The performance of a single cache server is insufficient, so it is necessary to build a cluster
5) Use C language development (nginx is also C development)

Redis cache service

What is redis?

Redis is an open source (BSD licensed) in memory data structure storage system, which can be used toAs database, cache and message middleware.It supports multiple types of data structures, such asStrings, hashes, lists, sets, sorted setsWith range query, bitmaps, hyperlogs and geospatial index radius query. Redis has built-in replication, Lua scripting, LRU activation, transactions and different levels of disk persistence, and provides high availability through redis sentinel and cluster.

Which data structures are supported?

String, hash, lists, sets, sorted sets

Redis read / write speed: 112000 / s read and 86000 / s write

Install redis (www.redis.cn)

1) Execute the command in the redis root directory
2) Install make
3) Start make install

You need to modify the configuration file after installation:

4) Modify the redis.conf file in the redis root directory
a. Remove IP binding
Redis cache and related instructions
b. Modify protection mode
Redis cache and related instructions
c. Start background startup
Redis cache and related instructions

Redis server command

Note: when running redis service, it must rely on the configuration file redis.conf. It is better to operate redis in the root directory
1) Start redis
redis-server redis.conf
Redis cache and related instructions

2) Enter the redis client
Redis-cli – P 6379 (9379 is the default port number of the cache server)
If it is a native service, it can also be written as redis cli
CTRL + C exit client
Redis cache and related instructions

3) Close the redis server
Find out the process: PS – EF | grep redis
End process: Kill – 9 XXX kill XXX
Redis cache and related instructions

You can also use the redis-cli – P 6379 shutdown command to shut down the server directly

Redis command

String type

Redis cache and related instructions

Hash type

Redis cache and related instructions

List type

Note: the list set in redis is a double ended circular list, which can access data from left and right directions
The list collection can be used as a queue or as a stack
Queue: the direction of storing data is opposite to that of acquiring data
Stack: store data in the same direction as get data
Redis cache and related instructions
Redis cache and related instructions

Redis transaction command

Note: the operation in redis can add transaction support. A task can be completed by multiple redis commands. If one command fails, which leads to the failure of warehousing, transaction rollback needs to be implemented
Redis cache and related instructions

Redis advanced application

Introduction to redis

1. Add jar package file
Redis cache and related instructions

2. String data structure case
Redis cache and related instructions
Redis cache and related instructions

3. Hash data structure case
Redis cache and related instructions
The results showed that: 1
Redis cache and related instructions

4. List data structure case
Redis cache and related instructions
Results display
Redis cache and related instructions

Test basic command

Redis cache and related instructions

Setnx / setex / set command

/**
 *Requirement: modify the data if it does not exist
 */
@Test
public void testSetNx(){
    Jedis jedis = new Jedis("192.168.126.129",6379);
    jedis.set("key1", "aaa");
    //If the key does not exist, the value is assigned
    Jedis. Setnx ("key1", "test method");
    System.out.println(jedis.get("key1"));
}

/**
 *Requirements: to achieve the setting of timeout and the atomicity of assignment operation
 *Test site: when setting the timeout time for data, remember to meet the atomic requirements
 *Otherwise, the key can never be deleted
 */
@Test
public void testSetEx(){
    Jedis jedis = new Jedis("192.168.126.129",6379);
    /*jedis.set("key2", "bbb");
    jedis.expire("key2", 3);*/
    //??? Will data be deleted after timeout??? Wrong
    jedis.setex("key2", 10, "bbb");
}

/**
 *Requirement: if the data exists, the data will be modified, and a timeout of 10 seconds (atomicity) will be added to the data
 *Parameter Description:
 *Nx: the value is assigned only when the data does not exist
 *XX: only when the data exists, the value will be assigned
 *Ex: seconds
 *PX: ms
 */
public void testSet(){
    Jedis jedis = new Jedis("192.168.126.129",6379);
   /* if(jedis.exists("key3")){
        jedis.setex("key3",10 ,"ccc");
    }*/
    SetParams setParams = new SetParams();
    setParams.xx().ex(10);
    //All operations are controlled atomically
    jedis.set("key3", "ccc", setParams);
}

Test hash

/**

*Hash test
 */
@Test
public void testHash(){
    Jedis jedis = new Jedis("192.168.126.129",6379);
    jedis.hset("person", "id", "100");
    Jedis. Hset ("person", "name", "Tomcat");
    System.out.println(jedis.hgetAll("person"));
}

Test list

/**

*List set test
 */
@Test
public void testList(){
    Jedis jedis = new Jedis("192.168.126.129",6379);
    jedis.lpush("list", "1","2","3","4");
    String value = jedis.rpop("list");
    System.out.println(value);
}

Test transaction

/**

*Set set test
 *1. Sadd new element
 *2. Scar gets the number of elements
 *3. Singer key1 key2 gets the intersection of elements
 *4. Smembers set gets the set elements
 *Make it up for yourself
 *  * */

@Test
public void testMulti(){
    Jedis jedis = new Jedis("192.168.126.129",6379);
    //Open transaction
    Transaction transaction = jedis.multi();
    try {
        transaction.set("a", "a");
        transaction.set("b", "b");
        //Commit transaction
        transaction.exec();
    }catch (Exception e){
        //Rollback transaction
        transaction.discard();
    }
}

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]