1. 程式人生 > >SpringBoot使用Redis數據庫

SpringBoot使用Redis數據庫

request 數據庫索引 ng- ram 使用 package 超時 val artifact

  (1)pom.xml文件引入jar包,如下:

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

  (2)application.properties文件中配置redis連接信息,如下:

# Redis數據庫索引(默認為0)
spring.redis.database=0
# Redis服務器地址
spring.redis.host
=172.31.19.222 # Redis服務器連接端口 spring.redis.port=6379 # Redis服務器連接密碼(默認為空) spring.redis.password= # 連接池最大連接數(使用負值表示沒有限制) spring.redis.pool.max-active=8 # 連接池最大阻塞等待時間(使用負值表示沒有限制) spring.redis.pool.max-wait=-1 # 連接池中的最大空閑連接 spring.redis.pool.max-idle=8 # 連接池中的最小空閑連接 spring.redis.pool.min-idle=0 # 連接超時時間(毫秒) spring.redis.timeout
=0

  (3)測試redis緩存

package springboot.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController { @Autowired private StringRedisTemplate stringRedisTemplate; @RequestMapping("/redisHandler") public String redisHandler(){ stringRedisTemplate.opsForValue().set("k5", "Springboot redis"); return stringRedisTemplate.opsForValue().get("k5"); } }

  (4)啟動項目,調用reidsHandler方法,查詢redis服務器信息,如下:

   技術分享

SpringBoot使用Redis數據庫