(1)pom.xml文件引入jar包,以下:web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
(2)application.properties文件中配置redis鏈接信息,以下: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緩存spring
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服務器信息,以下:數據庫