[Spring boot] spring boot based redis pipeline pipeline, bulk operations redis command

 

 

spring boot 2.x 

Use RedisTemplate operation

===================================

 

1.pom.xml

    <-! Spring2.0 desired integration redis-POOL2 Common -> 
        < dependency > 
            < the groupId > the org.apache.commons </ the groupId > 
            < the artifactId > Commons-POOL2 </ the artifactId > 
            < Version > 2.4.2 </ Version > 
        </ dependency > 
        <-! using the redis LUA script requires serialization of JAR -> 
        < dependency > 
            < the groupId > com.fasterxml.jackson.core </ the groupId > 
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

 

 

2.redisConfig need to add spring autoconfiguration

/**
 * @author sxd
 * @date 2019/5/27 16:13
 */
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {



    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(factory);
        RedisSerializer keySerializer = new StringRedisSerializer();
//        = New new GenericJackson2JsonRedisSerializer ValueSerializer RedisSerializer ();
         // Key string using deserialized 
        redisTemplate.setKeySerializer (keySerializer);
         // value also takes a string deserialized
         @ reasons: pipeline operation, the operation of a batch command redis , various types of command returns may vary
         // may be a Boolean type may be of type String may be byte [] type String according to the results thus uniform treatment 
        redisTemplate.setValueSerializer (keySerializer);
         return redisTemplate; 
    } 


}
View Code

 

 

3.controller

  @Autowired 
    RedisTemplate redisTemplate; 


    / ** 
     * Redis one batch operation mode 
     * redis pipeline pipelining 
     * / 
    @RequestMapping (value = "/ redisPipeline" )
     public  void redisPipeline () { 

//         1.executePipelined rewritten in the reference RedisCallback doInRedis method 
        List <Object> = resultList redisTemplate.executePipelined ( new new RedisCallback <Object> () { 

            @Override 
            public String doInRedis (RedisConnection Connection) throws the DataAccessException {
 //                 2.connection open pipe 
                connection.openPipeline ();

//                 3.connection within this conduit to add one-time execution of multiple commands 

//                 3.1 a set operation 
                byte [] = key1 "mykey1" .getBytes ();
                 byte [] VALUE1 = "string value" .getBytes (); 
                connection.set (key1, VALUE1); 

//                 3.2 mset a batch operation 
                the Map < byte [], byte []> tuple = new new the HashMap <> (); 
                tuple.put ( "m_mykey1" .getBytes (), "m_value1" .getBytes ()); 
                tuple.put ( "m_mykey2" .getBytes (), "m_value2" .getBytes ());
                tuple.put("m_mykey3" .getBytes (), "m_value3" .getBytes ()); 
                connection.mSet (tuple); 

//                  3.3 a get operation 
                connection.get ( "m_mykey2" .getBytes ()); 

//                 4. Close pipe does not otherwise, the return value close need to get
 //                 connection.closePipeline (); 

//                 here we must return null, the final result of the execution pipeline, will return to the outermost 
                return  null ; 
            } 
        }); 


//         5. the last redis pipeline conduit and the operation returns to the determination results of operations compensation 
        for (STR Object: resultList) { 
            System.out.println (String.valueOf (STR)); 
        } 
        
    }
View Code

 

 

operation result:

true
true
m_value2

 

 

redis result:

 

Guess you like

Origin www.cnblogs.com/sxdcgaq8080/p/10949727.html