spring-data-redis 2.0 的使用

在使用Spring Boot2.x運行Redis時,發現百度不到順手的文檔,搞通後發現其實這個過程很是簡單和簡潔,以爲有必要拿出來分享一下。redis

Spring Boot2.x 再也不使用Jedis,換成了Lettuce。Lettuce是基於 Netty 實現的,因此性能更好。可是我看到不少文章竟然在Spring Boot 2.x還在寫Jedis的配置。spring

依賴

依賴比較簡單,spring-boot-starter-data-redis、commons-pool2 便可。數據庫

<!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>


        <!--spring2.0集成redis所需common-pool2-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.4.2</version>
        </dependency>

屬性配置

在屬性中配置Redis Server的訪問地址、密碼、數據庫,並配置鏈接池的屬性。apache

redis:
    #    reids的鏈接ip
    host: 127.0.0.1
    port: 6379
    password: helloworld
    
    # Redis默認狀況下有16個分片,這裏配置具體使用的分片,默認是0
    database: 0
    
    # 鏈接超時時間(毫秒)
    timeout: 10000ms
    
    #  redis client配置,使用lettuce
    lettuce:
      pool:
        # 鏈接池中的最小空閒鏈接 默認 0
        min-idle: 0
        # 鏈接池最大阻塞等待時間(使用負值表示沒有限制) 默認 -1
        max-wait: 1000ms
        # 鏈接池最大鏈接數(使用負值表示沒有限制) 默認 8
        max-active: 8
        # 鏈接池中的最大空閒鏈接 默認 8
        max-idle: 8

註解配置

全局使能緩存json

@EnableSwagger2          // 使用swagger api 功能
@EnableCaching           // 使用緩存
@SpringBootApplication
public class Starter {

    public static void main(String[] args) {
        SpringApplication.run(Starter.class, args);
    }
}

經過註解使用緩存,@Cacheable 將獲取值存入緩存api

/**
     * 基於id 獲取用戶信息
     */
    @Cacheable(value="user", key="#id", unless="#result == null")
    public UserDTO GetUserById(int id) {
        User userEntity = userMapper.getUserByID(id);
        if (userEntity == null){
            return null;
        }

        /* entity 轉 DTO */
        UserDTO userDTO = new UserDTO();
        userDTO.setAge(userEntity.getAge());
        userDTO.setId(id);
        userDTO.setName(userEntity.getName());
        userDTO.setCreateTime(unixTime2String(userEntity.getCreateTime()));
        userDTO.setPhone(userEntity.getPhone());
        userDTO.setEmail(userEntity.getEmail());
        return userDTO;
    }

@CachePut 更新緩存緩存

@CachePut(value = "user", key="#p0.id")
    public UserDTO updateUser(InputUserInfoDTO inputUserInfoDTO){
        userMapper.updateUser(inputUserInfoDTO.getId(), inputUserInfoDTO.getName(), inputUserInfoDTO.getAge());
        User userEntity = userMapper.getUserByID(inputUserInfoDTO.getId());/* entity 轉 DTO */

        if (null == userEntity){
            return null;
        }
        UserDTO userDTO = new UserDTO();
        userDTO.setAge(userEntity.getAge());
        userDTO.setId(userEntity.getId());
        userDTO.setName(userEntity.getName());
        userDTO.setCreateTime(unixTime2String(userEntity.getCreateTime()));
        userDTO.setPhone(userEntity.getPhone());
        userDTO.setEmail(userEntity.getEmail());
        return userDTO;
    }

@CacheEvict 刪除緩存app

@CacheEvict(value = "user", key="#id")
    public void deleteUser(int id){
        userMapper.deleteUser(id);
    }

固然爲了支持序列化,個人UserDTO得implements Serializableless

@Data
public class UserDTO implements Serializable {
//public class UserDTO implements Serializable {
    private int id;
    private String name;
    private int age;
    private String createTime;
    private String phone;
    private String email;
}

至此緩存已經能夠用起來了,不須要編寫RedisConfig代碼,有點小遺憾,直接去Redis查看數據,發現是亂碼。這是由於我使用的是Java自帶的序列化,若是要更換Redis序列化方法,就要重寫RedisConfig了。spring-boot

RedisConfig

這個配置也不復雜,使用Jackson2JsonRedisSerializer將對象轉換爲Json串,注意這裏必定要使用ObjectMapper,不然再將json串反序列化爲對象時會報錯。

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig extends CachingConfigurerSupport{

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        //解決查詢緩存轉換異常的問題
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        // 配置序列化(解決亂碼的問題)
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                                            .entryTtl(Duration.ZERO)
                                            .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                                            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                                            .disableCachingNullValues();

        RedisCacheManager cacheManager = RedisCacheManager.builder(factory).cacheDefaults(config).build();
        return cacheManager;
    }
}
相關文章
相關標籤/搜索