Skip to content

Commit 97fdb14

Browse files
committed
更新实战缓存代码
1 parent 5270dbe commit 97fdb14

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

passjava-question/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.jackson0714.passjava</groupId>
12-
<artifactId>passjava-question</artifactId>
12+
<artifactId>passjava-question</artifactId>mysql-connector-java
1313
<version>0.0.1-SNAPSHOT</version>
1414
<name>passjava-question</name>
1515
<description>佳必过-题目服务</description>
@@ -25,6 +25,10 @@
2525
<artifactId>passjava-common</artifactId>
2626
<version>0.0.1-SNAPSHOT</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-redis</artifactId>
31+
</dependency>
2832
<!--
2933
<dependency>
3034
<groupId>com.aliyun.oss</groupId>

passjava-question/src/main/java/com/jackson0714/passjava/question/controller/TypeAppController.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.jackson0714.passjava.question.controller;
22

3+
import com.alibaba.fastjson.JSON;
34
import com.jackson0714.common.utils.PageUtils;
45
import com.jackson0714.common.utils.R;
56
import com.jackson0714.passjava.question.entity.TypeEntity;
67
import com.jackson0714.passjava.question.service.ITypeService;
78
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.data.redis.core.StringRedisTemplate;
10+
import org.springframework.data.redis.core.ValueOperations;
811
import org.springframework.web.bind.annotation.*;
912

10-
import java.util.Arrays;
11-
import java.util.HashMap;
12-
import java.util.List;
13-
import java.util.Map;
13+
import java.util.*;
1414

1515

1616
/**
@@ -29,19 +29,54 @@ public class TypeAppController {
2929
@Autowired
3030
private ITypeService ITypeService;
3131

32+
@Autowired
33+
StringRedisTemplate stringRedisTemplate;
34+
3235
/**
33-
* 查询
36+
* 查询题目类型列表
37+
*
38+
* @author:公众号:悟空聊架构
39+
* @website:www.passjava.cn
3440
*/
3541
@RequestMapping("/list")
36-
//@RequiresPermissions("question:type:list")
3742
public R list(){
3843
List<TypeEntity> typeEntityListCache = (List<TypeEntity>) cache.get("typeEntityList");
39-
List<TypeEntity> typeEntityList = null;
44+
// 如果缓存中没有数据
45+
if (typeEntityListCache == null) {
46+
System.out.println("The cache is empty");
47+
// 从数据库中查询数据
48+
List<TypeEntity> typeEntityList = ITypeService.list();
49+
// 将数据放入缓存中
50+
typeEntityListCache = typeEntityList;
51+
cache.put("typeEntityList", typeEntityListCache);
52+
}
53+
return R.ok().put("typeEntityList", typeEntityListCache);
54+
}
55+
56+
/**
57+
* 查询题目类型列表
58+
*
59+
* @author:公众号:悟空聊架构
60+
* @website:www.passjava.cn
61+
*/
62+
@RequestMapping("/list-by-redis")
63+
public R listByRedis(){
64+
65+
// 初始化 redis 组件
66+
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
67+
68+
// 查询数据
69+
String typeEntityListCache = ops.get("typeEntityList");
70+
// 如果缓存中没有数据
4071
if (typeEntityListCache == null) {
4172
System.out.println("The cache is empty");
42-
typeEntityList = ITypeService.list();
43-
cache.put("typeEntityList", typeEntityList);
73+
// 从数据库中查询数据
74+
List<TypeEntity> typeEntityListFromDb = ITypeService.list();
75+
// 将数据放入缓存中
76+
String s = JSON.toJSONString(typeEntityListFromDb);
77+
ops.set("typeEntityList", s);
4478
}
79+
List<TypeEntity> typeEntityList = (List<TypeEntity>) JSON.parseObject(typeEntityListCache);
4580
return R.ok().put("typeEntityList", typeEntityList);
4681
}
4782
}

passjava-question/src/main/java/com/jackson0714/passjava/question/service/impl/QuestionServiceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ public PageUtils queryPage(Map<String, Object> params) {
5454
@Override
5555
public boolean saveQuestion(QuestionEntity question) {
5656
boolean saveResult = save(question);
57-
saveEs(question);
57+
//saveEs(question);
5858

5959
return true;
6060
}
6161

6262
@Override
6363
public boolean updateQuestion(QuestionEntity question) {
6464
updateById(question);
65-
saveEs(question);
65+
//saveEs(question);
66+
6667
return true;
6768
}
6869

passjava-question/src/main/resources/application.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#spring:
1+
#=spring:
22
# datasource:
33
# driver-class-name: com.mysql.cj.jdbc.Driver
44
# url: jdbc:mysql://129.211.188.xxxx:xxxx/passjava_qms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
@@ -38,3 +38,13 @@ logging:
3838
spring:
3939
jackson:
4040
date-format: yyyy-MM-dd HH:mm:ss
41+
42+
redis:
43+
host: 127.0.0.1
44+
port: 6379
45+
46+
# datasource:
47+
# driver-class-name: com.mysql.cj.jdbc.Driver
48+
# url: jdbc:mysql://127.0.0.1:3306/passjava_qms?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
49+
# username: root
50+
# password: huangz01

passjava-question/src/test/java/com/jackson0714/passjava/question/PassjavaQuestionApplicationTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,32 @@
66
import org.junit.jupiter.api.Test;
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.data.redis.core.StringRedisTemplate;
10+
import org.springframework.data.redis.core.ValueOperations;
911

1012
import java.util.List;
13+
import java.util.UUID;
1114

1215
@SpringBootTest
1316
class PassjavaQuestionApplicationTests {
1417

1518
@Autowired
1619
ITypeService ITypeService;
1720

21+
@Autowired
22+
StringRedisTemplate stringRedisTemplate;
23+
24+
@Test
25+
public void TestStringRedisTemplate() {
26+
// 初始化 redis 组件
27+
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
28+
// 存储数据
29+
ops.set("悟空", "悟空聊架构_" + UUID.randomUUID().toString());
30+
// 查询数据
31+
String wukong = ops.get("悟空");
32+
System.out.println(wukong);
33+
}
34+
1835
// 创建题目类型
1936
@Test
2037
void testCreateType() {

0 commit comments

Comments
 (0)