springboot (9) springboot integration redis

springboot整合redis相关依赖引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

## Redis configuration 
## Redis database indexes (default 0)
spring.redis.database = 0
## Redis server address
spring.redis.host = 192.168.37.140
## Redis server port
spring.redis.port 6379 =
## Redis server connection password (blank by default)
spring.redis.password =
maximum number of connections the connection pool ## (negative values no limit)
spring.redis.jedis.pool.max. 8 = Active-
connection pool ## to wait for the maximum blocking time (negative values no limit)
spring.redis.jedis.pool.max the wait--1ms =
## the maximum idle connection pool connection
spring.redis.jedis.pool.max. 8-iDLE =
## connection pool the minimum idle connection
spring.redis.jedis.pool.min-iDLE = 0
## connection time (ms)
spring.redis.timeout = 5000

================== key to solve the garbage problem when ============ redis storage
package com.example.demo.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author Suncm
 * @ClassName RedisConfig
 * @Description TODO
 * @Date 2019/6/6 14:10
 * @Version 1.0
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    /**
     * retemplate相关配置
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // Configure connection factory 
        Key value // use StringRedisSerializer to serialize and deserialize the redis
        template.setConnectionFactory(factory);

        // Use Jackson2JsonRedisSerializer value to serialize and deserialize the redis value (JDK use default serialization) 
        Jackson2JsonRedisSerializer jacksonSeial = new new Jackson2JsonRedisSerializer (Object.class); 

        ObjectMapper new new ObjectMapper OM = (); 
        // specified to be serialized field, field, get, and SET, and range modifiers, there is the ANY including private and public 
        om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 
        // the specified sequence of input types, non-final class must be modified the, final modified type, such as String, Integer, etc. will run out abnormal 
        om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); 
        jacksonSeial.setObjectMapper (OM); 

        // value takes json serialization 
        template.setValueSerializer (jacksonSeial);
        template.setKeySerializer (new new StringRedisSerializer ()); 

        // set the hash key and the value sequence pattern 
        template.setHashKeySerializer (new new StringRedisSerializer ()); 
        template.setHashValueSerializer (jacksonSeial); 
        template.afterPropertiesSet (); 

        return Template; 
    } 

    / ** 
     * operation of the hash data type 
     * 
     * @param redisTemplate 
     * @return 
     * / 
    @Bean 
    public hashOperations <String, String, Object> hashOperations (redisTemplate <String, Object> redisTemplate) { 
        return redisTemplate.opsForHash (); 
    } 

    / ** 
     * redis string type of data manipulation 
     *
     @Param redisTemplate *  
     * @return 
     * /
    @Bean 
    public ValueOperations <String, Object> valueOperations (RedisTemplate <String, Object> redisTemplate) { 
        return redisTemplate.opsForValue (); 
    } 

    / ** 
     * list of types of data manipulation 
     * 
     * @param redisTemplate 
     * @return 
     * / 
    @Bean 
    public listOperations <String, Object> listOperations (redisTemplate <String, Object> redisTemplate) { 
        return redisTemplate.opsForList (); 
    } 

    / ** 
     * unordered set of data manipulation type 
     * 
     * @param redisTemplate 
     * @return 
     * / 
    @Bean
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    /**
     * 对有序集合类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }
}

  

Guess you like

Origin www.cnblogs.com/suncm/p/10985277.html