springboot中使用redis的案例
生活随笔
收集整理的這篇文章主要介紹了
springboot中使用redis的案例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. application.properties中配置redis
#設(shè)置Redis配置 spring.redis.host=****** spring.redis.port=6379 spring.redis.password=******2. pom.xml中
<!-- springboot集成Redis的依賴--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.6.4</version></dependency>3. StudentServiceImpl.java中
public class StudentServiceImpl implements StudentService {@Autowiredprivate RedisTemplate<Object,Object> redisTemplate;@Autowiredprivate StudentMapper studentMapper;@Overridepublic Integer queryAllStudentCount() {//為提升系統(tǒng)性能和用戶體驗(yàn)//首先在Redis緩存中查詢,如果有,直接使用;如果沒有,去數(shù)據(jù)庫查詢并放入redis緩存Integer allStudentCount = (Integer) redisTemplate.opsForValue().get("allStudentCount");if (null == allStudentCount) {//去數(shù)據(jù)庫查詢allStudentCount = studentMapper.selectAllStudentCount();//并放入redis緩存redisTemplate.opsForValue().set("allStudentCount", allStudentCount, 30, TimeUnit.SECONDS);}return allStudentCount;}其他
其他部分該怎么寫還怎么寫!!!
總結(jié)
以上是生活随笔為你收集整理的springboot中使用redis的案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: checkPathValidity 检查
- 下一篇: h5 打包后效果失效