Skip to content

Commit b0209a3

Browse files
committed
[add] 利用Cacheable注解实现简单缓存
1 parent 2ab7e24 commit b0209a3

File tree

8 files changed

+190
-1
lines changed

8 files changed

+190
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ SET FOREIGN_KEY_CHECKS = 1;
9999
<br/>
100100

101101

102+
## CacheConfig简单缓存
103+
1、启动类上需要加@EnableCaching注解
104+
2、在需要执行缓存的类上面写上缓存前缀名称
105+
@CacheConfig(cacheNames="user")
106+
3、在查询方法上使用@Cacheable(key = "'list'")配置键的名称
107+
4、在修改方法上使用@CachePut(key = "'list'")配置键的名称
108+
http://127.0.0.1:8080/cache/user/list
109+
http://127.0.0.1:8080/cache/user/add
110+
http://127.0.0.1:8080/cache/user/delete
111+
<br/>
112+
<br/>
113+
102114
## FileHandleUtil
103115
> 文件上传工具类,上传到与jar包同级的static目录下,开发环境和服务器环境均可
104116
----------

src/main/java/com/linkinstars/springBootTemplate/SpringBootTemplateApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
6+
import org.springframework.cache.annotation.EnableCaching;
67

78
/**
89
* SpringBoot启动类
910
* @author LinkinStar
1011
*/
1112
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
13+
@EnableCaching
1214
public class SpringBootTemplateApplication {
1315

1416
public static void main(String[] args) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.linkinstars.springBootTemplate.bean;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* 用户数据
7+
* @author LinkinStar
8+
*/
9+
public class UserVO implements Serializable {
10+
11+
private static final long serialVersionUID = 3732941597434585757L;
12+
13+
private int id;
14+
private String val;
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
24+
public String getVal() {
25+
return val;
26+
}
27+
28+
public void setVal(String val) {
29+
this.val = val;
30+
}
31+
32+
public UserVO(int id, String val) {
33+
this.id = id;
34+
this.val = val;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "UserEntity{" +
40+
"id=" + id +
41+
", val='" + val + '\'' +
42+
'}';
43+
}
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.linkinstars.springBootTemplate.controller;
2+
3+
import com.linkinstars.springBootTemplate.service.IUserCacheService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
/**
9+
* @author LinkinStar
10+
*/
11+
@RestController
12+
@RequestMapping("/cache/user")
13+
public class UserCacheController {
14+
15+
@Autowired
16+
private IUserCacheService userCacheService;
17+
18+
@RequestMapping("list")
19+
public String listUser() {
20+
return userCacheService.listUser().toString();
21+
}
22+
23+
@RequestMapping("add")
24+
public String addUser() {
25+
return String.valueOf(userCacheService.addUser());
26+
}
27+
28+
@RequestMapping("delete")
29+
public String deleteUser() {
30+
return String.valueOf(userCacheService.deleteUser());
31+
}
32+
}

src/main/java/com/linkinstars/springBootTemplate/controller/UserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public String test(HttpServletRequest request, @RequestParam(required = false) I
5959
//进行分页查询
6060
PageHelper.startPage(pageNum, pageSize);
6161
List<UserEntity> userList = userService.listUser();
62-
PageInfo pageInfo = new PageInfo(userList);
62+
PageInfo pageInfo = new PageInfo<>(userList);
6363

6464
//设置返回参数
6565
request.setAttribute("userList", userList);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.linkinstars.springBootTemplate.service;
2+
3+
import com.linkinstars.springBootTemplate.bean.UserVO;
4+
5+
import java.util.List;
6+
7+
/**
8+
* 缓存用户信息
9+
* @author LinkinStar
10+
*/
11+
public interface IUserCacheService {
12+
13+
/**
14+
* 查询缓存用户数据
15+
*/
16+
List<UserVO> listUser();
17+
18+
/**
19+
* 添加一个缓存用户
20+
*/
21+
List<UserVO> addUser();
22+
23+
/**
24+
* 删除一个缓存用户
25+
*/
26+
boolean deleteUser();
27+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.linkinstars.springBootTemplate.service.Impl;
2+
3+
import com.linkinstars.springBootTemplate.bean.UserVO;
4+
import com.linkinstars.springBootTemplate.service.IUserCacheService;
5+
import com.linkinstars.springBootTemplate.util.CacheUserDb;
6+
import org.springframework.cache.annotation.CacheConfig;
7+
import org.springframework.cache.annotation.CachePut;
8+
import org.springframework.cache.annotation.Cacheable;
9+
import org.springframework.stereotype.Service;
10+
11+
import java.util.List;
12+
13+
/**
14+
* 用户信息缓存服务
15+
* @author LinkinStar
16+
*/
17+
@Service
18+
@CacheConfig(cacheNames="user")
19+
public class UserCacheServiceImpl implements IUserCacheService {
20+
21+
/**
22+
* 查询缓存用户数据
23+
* 第一次查询并在控制台输出信息
24+
* 之后都从缓存中取
25+
*/
26+
@Override
27+
@Cacheable(key = "'list'")
28+
public List<UserVO> listUser() {
29+
System.out.println("调用了方法,没有从redis取");
30+
//模拟数据库查询
31+
return CacheUserDb.userVOList;
32+
}
33+
34+
/**
35+
* 添加一个缓存用户
36+
* 并且操作缓存
37+
*/
38+
@Override
39+
@CachePut(key = "'list'")
40+
public List<UserVO> addUser() {
41+
//模拟数据库添加
42+
int id = CacheUserDb.userVOList.size() + 1;
43+
CacheUserDb.userVOList.add(new UserVO(id, "val - " + id));
44+
return CacheUserDb.userVOList;
45+
}
46+
47+
/**
48+
* 删除一个缓存用户
49+
* 并不操作缓存
50+
*/
51+
@Override
52+
public boolean deleteUser() {
53+
//模拟数据库删除
54+
CacheUserDb.userVOList.remove(0);
55+
return true;
56+
}
57+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.linkinstars.springBootTemplate.util;
2+
3+
import com.linkinstars.springBootTemplate.bean.UserVO;
4+
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
/**
9+
* 模拟数据库
10+
* @author LinkinStar
11+
*/
12+
public class CacheUserDb {
13+
14+
public static List<UserVO> userVOList = new LinkedList<>();
15+
}

0 commit comments

Comments
 (0)