1. 程式人生 > 實用技巧 >SpringBoot(三)——整合redis

SpringBoot(三)——整合redis

springboot2.3.2+redis5.0.9

1.依賴包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</
groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>

2.配置application.yml

spring:
  redis:
    #指定資料庫,預設為0,在黑窗口裡通過 select idx切換
    database: 0
    port: 6379
    host: 127.0.0.1
    #redis密碼,預設為空,這裡沒有設定所以註釋掉了
    #password: root
    pool:
      max-active: 8
        # 連線池最大阻塞等待時間(使用負值表示沒有限制)
      max-wait: -1
        # 連線池中的最大空閒連線
      max-idle: 8
        # 連線池中的最小空閒連線
      min-idle: 0

3.配置RedisTemplate

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); //使用fastjson序列化 FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); // value值的序列化採用fastJsonRedisSerializer template.setValueSerializer(fastJsonRedisSerializer); template.setHashValueSerializer(fastJsonRedisSerializer); // key的序列化採用StringRedisSerializer template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean(StringRedisTemplate.class) public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } }

這個當作是一個模板套用,如果不使用那些序列化,會使得【對Java的redisTemplate設定key,但是在redis中的key有一些亂七八糟的編碼】,例如在Java中設定name,在redis中卻這樣顯示。

RedisTemplate和StringRedisTemplate有一些區別,一般RedisTemplate就夠用了

4.測試

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;

@SpringBootTest
class DemoApplicationTests {

    @Resource
    RedisTemplate redisTemplate;
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Test
    void testRedis() {
        redisTemplate.opsForValue().set("person","shoulinniao");
        redisTemplate.opsForValue().set("id",10086);
        System.out.println(redisTemplate.opsForValue().get("person"));
        System.out.println(redisTemplate.opsForValue().get("id"));
        System.out.println(stringRedisTemplate.opsForValue().get("person"));
        System.out.println(stringRedisTemplate.opsForValue().get("id"));
    }
}
/*輸出
shoulinniao
10086
"shoulinniao"
10086
*/

在redis黑視窗也同步更新