一、緩存使用
為了系統性能的提升,我們一般都會将部分資料放入緩存中,加速通路。而DB承擔資料羅盤工作。
那些資料适合放入緩存?
- 即時性、資料一緻性要求不高的
- 通路量大而且更新頻率不高的資料(讀多,寫少)
二、本地緩存與分布式緩存
本地緩存:

本地緩存可以在單體應用中使用,如果分布式的就會出現問題。
分布式緩存-本地模式在分布式下的問題:
本地緩存模式下的分布式有問題,會造成緩存不統一,是以,不應該再使用本地模式的緩存
分布式緩存:
分布式模式下,所有的微服務都共用同一個緩存中間件。
三、整合redis
1、引入redis
在gulimall-product/pom.xml中引入redis
<!-- 引入redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
在
application.yml
檔案中配置redis:
spring:
datasource:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/gulimall_pms
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置nacos注冊中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
jackson:
date-format: yyyy-MM-dd HH:mm:ss
thymeleaf:
cache: false # 調試期間,關閉緩存
redis:
host: 127.0.0.1
prot: 6379
@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimallProductApplicationTests {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Test
public void redisTest(){
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
ops.set("hello", "lily");
String key = "hello";
System.out.println(ops.get(key));
}
}