SpringBoot2 redis

使用jar包:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/>
    </parent>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

在application.properties中加入redis的配置信息

#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
# 數(shù)據(jù)庫連接超時時間,2.0 中該參數(shù)的類型為Duration,這里在配置的時候需要指明單位
spring.redis.timeout=60s
# 連接池配置,2.0中直接使用jedis或者lettuce配置連接池
# 最大活躍連接數(shù),負數(shù)為不限制
spring.redis.lettuce.pool.max-active=500
# 等待可用連接的最大時間,負數(shù)為不限制
spring.redis.lettuce.pool.max-wait=-1ms
# 最大空閑連接數(shù)
spring.redis.lettuce.pool.max-idle=100
# 最小空閑連接數(shù)
spring.redis.lettuce.pool.min-idle=20

新增RedisConfig.java文件,其中注釋掉的兩行代碼不能打開。打開的話會使value使序例化,序例化了以后會跟后面要使用的@Cacheable不一致,導致取不到值

package com.chenyingjun.springboot2.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * RedisConfig工具類配置
 *
 * @author chenyingjun
 * @since 1.0
 * @version 2018年8月15日 chenyingjun
 */
@Configuration
@EnableCaching
public class RedisConfig {

    /**
     * RedisTemplate配置
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        // 設(shè)置序列化
        Jackson2JsonRedisSerializer<Object> 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);
        // 配置redisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        RedisSerializer<?> stringSerializer = new StringRedisSerializer();
//        // key序列化
        redisTemplate.setKeySerializer(stringSerializer);
        // value序列化
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // Hash key序列化
        redisTemplate.setHashKeySerializer(stringSerializer);
        // Hash value序列化
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}

在service層中加入@Cacheable(key="'userId_'+#id", value="user")來緩存信息,這樣println只會打印一次

@Cacheable(key="'userId_'+#id", value="user")
    public SystemUserVo info(String id) {
        System.out.println("-------------------------------------------------------");
        SystemUserVo user = systemUserMapper.info(id);
        return user;
    }

緩存后數(shù)據(jù)如下圖


使用RedisTemplate和StringRedisTemplate來獲取緩存中的值,其中obj1也已經(jīng)獲取到了redis中的數(shù)據(jù)。效果如下


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容