diff --git a/README.md b/README.md index 86d65cf..b528ce4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Spring Boot Demo -spring boot demo 一个用来学习 spring boot 的项目,已经集成 actuator、logback、mybatis 模块,后续会集成activemq,redis,email, freemarker,shiro,websocket,sitemesh,ehcache,easyui,kindeditor,quartz,springfox,swagger,jpa,hibernate,querydsl,netty等模块。 +spring boot demo 一个用来学习 spring boot 的项目,已经集成 actuator、logback、jpa、mybatis、redis缓存 模块,后续会集成activemq,email, freemarker,shiro,websocket,quartz,springfox,swagger,netty等模块。 依赖的 Spring Boot 版本: @@ -40,6 +40,7 @@ spring boot demo 一个用来学习 spring boot 的项目,已经集成 actuato ../spring-boot-demo-logback ../spring-boot-demo-jpa ../spring-boot-demo-mybatis + ../spring-boot-demo-cache-redis @@ -53,6 +54,10 @@ spring boot demo 一个用来学习 spring boot 的项目,已经集成 actuato UTF-8 UTF-8 1.8 + 3.2.0 + 3.5 + 3.2.2 + 1.10 @@ -76,9 +81,24 @@ spring boot demo 一个用来学习 spring boot 的项目,已经集成 actuato + org.apache.commons + commons-lang3 + ${commons.lang3.version} + + + commons-collections + commons-collections + ${commons.collections.version} + + + commons-codec + commons-codec + ${commons.codec.version} + + com.xiaoleilu hutool-all - 3.1.2 + ${hutool.version} com.google.guava @@ -113,6 +133,7 @@ spring boot demo 一个用来学习 spring boot 的项目,已经集成 actuato | [spring-boot-demo-logback](./spring-boot-demo-logback) | spring-boot 集成 logback 日志 | | [spring-boot-demo-jpa](./spring-boot-demo-jpa) | spring-boot 集成 spring-boot-starter-data-jpa 操作数据库 | | [spring-boot-demo-mybatis](./spring-boot-demo-mybatis) | spring-boot 集成 [mybatis-spring-boot-starter](https://github.com/mybatis/spring-boot-starter)、[mybatis-spring-boot-starter](https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter) | +| [spring-boot-demo-cache-redis](./spring-boot-demo-cache-redis) | spring-boot 使用 Redis 做缓存 | # 官方提供的 starter 介绍 diff --git a/spring-boot-demo-cache-redis/README.md b/spring-boot-demo-cache-redis/README.md new file mode 100644 index 0000000..4671681 --- /dev/null +++ b/spring-boot-demo-cache-redis/README.md @@ -0,0 +1,172 @@ +# spring-boot-demo-cache-redis + +依赖 [spring-boot-demo-parent](../spring-boot-demo-parent)、`spring-boot-starter-data-redis` + +### pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo-cache-redis + 0.0.1-SNAPSHOT + war + + spring-boot-demo-cache-redis + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo-parent + 0.0.1-SNAPSHOT + ../spring-boot-demo-parent/pom.xml + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + spring-boot-demo-cache-redis + + + +``` + +### application.yml + +```yml +server: + port: 8080 + context-path: /demo +spring: + redis: + host: localhost + port: 6379 +``` + +### SpringBootDemoCacheRedisApplication.java (开启使用缓存注解) + +```java +@SpringBootApplication +@EnableCaching +public class SpringBootDemoCacheRedisApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoCacheRedisApplication.class, args); + } +} +``` + +### User.java (一定要序列化!) + +```java +@Data +public class User implements Serializable { + private static final long serialVersionUID = 1442134563392432775L; + private Long id; + private String name; + private Date birthDay; +} +``` + +### UserController.java + +```java +@RestController +@RequestMapping("/user") +@CacheConfig(cacheNames = "users") // 整体配置缓存的名称 +public class UserController { + + @Autowired + private UserService userService; + + @GetMapping({"", "/"}) + @Cacheable + public List index() { + return userService.find(); + } + + @GetMapping("/find/{id}") + @Cacheable(key = "#id", condition = "#id != null") // #id 以及 condition 里的语法是 SpEL 语法 + public User find(@PathVariable Long id) { + return userService.find(id); + } + + @GetMapping("/find") + @Cacheable(key = "#name", condition = "#name != null") + public User find(@RequestParam String name) { + return userService.find(name); + } + + @GetMapping("/save") + @CacheEvict(allEntries = true, beforeInvocation = true) //@CacheEvict 清空缓存 + public User save() { + return userService.save(); + } + + @GetMapping("/update/{id}") + @CacheEvict(allEntries = true, beforeInvocation = true) + public User update(@PathVariable Long id) { + return userService.update(id); + } + + @GetMapping("/delete/{id}") + @CacheEvict(allEntries = true, beforeInvocation = true) + public String delete(@PathVariable Long id) { + User user = userService.delete(id); + if (user == null) { + return "用户不存在"; + } else { + return user.getName() + "删除成功"; + } + } +} +``` + +### 其余 Service、Dao 代码(采用 List 存储,只为了演示启用 cache 后,是否仍会进入对应的 method) + +详情请参见本demo。 + +### 缓存 @Annotation 详解 + +#### @CacheConfig + +用于统一配置缓存的基本信息 + +#### @Cacheable + +将返回的结果缓存,该注解里的参数如下 + +| 参数 | 作用 | 示例 | +| --------- | ---------------------------------------- | ---------------------------------------- | +| value | 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 | 例如:@Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”} | +| key | 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 | 例如:@Cacheable(value=”testcache”,key=”#userName”) | +| condition | 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 | 例如:@Cacheable(value=”testcache”,condition=”#userName.length()>2”) | + +#### @CacheEvict + +能够根据一定的条件对缓存进行清空,该注解里的参数如下 + +| 参数 | 作用 | 示例 | +| ---------------- | ---------------------------------------- | ---------------------------------------- | +| value | 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 | 例如:@CachEvict(value=”mycache”) 或者 @CachEvict(value={”cache1”,”cache2”} | +| key | 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 | 例如:@CachEvict(value=”testcache”,key=”#userName”) | +| condition | 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才清空缓存 | 例如:@CachEvict(value=”testcache”,condition=”#userName.length()>2”) | +| allEntries | 是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存 | 例如:@CachEvict(value=”testcache”,allEntries=true) | +| beforeInvocation | 是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存 | 例如:@CachEvict(value=”testcache”,beforeInvocation=true) | + +#### @CachePut + +能够根据方法的请求参数对其结果进行缓存,和 `@Cacheable` 不同的是,它每次都会触发真实方法的调用 + +| 参数 | 作用 | 示例 | +| --------- | ---------------------------------------- | ---------------------------------------- | +| value | 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 | 例如:@Cacheable(value=”mycache”) 或者 @Cacheable(value={”cache1”,”cache2”} | +| key | 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 | 例如:@Cacheable(value=”testcache”,key=”#userName”) | +| condition | 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 | 例如:@Cacheable(value=”testcache”,condition=”#userName.length()>2”) | \ No newline at end of file diff --git a/spring-boot-demo-cache-redis/pom.xml b/spring-boot-demo-cache-redis/pom.xml new file mode 100644 index 0000000..4bf3d66 --- /dev/null +++ b/spring-boot-demo-cache-redis/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + spring-boot-demo-cache-redis + 0.0.1-SNAPSHOT + war + + spring-boot-demo-cache-redis + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo-parent + 0.0.1-SNAPSHOT + ../spring-boot-demo-parent/pom.xml + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + spring-boot-demo-cache-redis + + + diff --git a/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/SpringBootDemoCacheRedisApplication.java b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/SpringBootDemoCacheRedisApplication.java new file mode 100644 index 0000000..5658cd2 --- /dev/null +++ b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/SpringBootDemoCacheRedisApplication.java @@ -0,0 +1,14 @@ +package com.xkcoding.springbootdemocacheredis; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; + +@SpringBootApplication +@EnableCaching +public class SpringBootDemoCacheRedisApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoCacheRedisApplication.class, args); + } +} diff --git a/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/controller/UserController.java b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/controller/UserController.java new file mode 100644 index 0000000..2c69df4 --- /dev/null +++ b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/controller/UserController.java @@ -0,0 +1,61 @@ +package com.xkcoding.springbootdemocacheredis.controller; + +import com.xkcoding.springbootdemocacheredis.entity.User; +import com.xkcoding.springbootdemocacheredis.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/user") +@CacheConfig(cacheNames = "users") // 整体配置缓存的名称 +public class UserController { + + @Autowired + private UserService userService; + + @GetMapping({"", "/"}) + @Cacheable + public List index() { + return userService.find(); + } + + @GetMapping("/find/{id}") + @Cacheable(key = "#id", condition = "#id != null") // #id 以及 condition 里的语法是 SpEL 语法 + public User find(@PathVariable Long id) { + return userService.find(id); + } + + @GetMapping("/find") + @Cacheable(key = "#name", condition = "#name != null") + public User find(@RequestParam String name) { + return userService.find(name); + } + + @GetMapping("/save") + @CacheEvict(allEntries = true, beforeInvocation = true) //@CacheEvict 清空缓存 + public User save() { + return userService.save(); + } + + @GetMapping("/update/{id}") + @CacheEvict(allEntries = true, beforeInvocation = true) + public User update(@PathVariable Long id) { + return userService.update(id); + } + + @GetMapping("/delete/{id}") + @CacheEvict(allEntries = true, beforeInvocation = true) + public String delete(@PathVariable Long id) { + User user = userService.delete(id); + if (user == null) { + return "用户不存在"; + } else { + return user.getName() + "删除成功"; + } + } +} diff --git a/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/dao/UserDao.java b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/dao/UserDao.java new file mode 100644 index 0000000..cf8a02c --- /dev/null +++ b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/dao/UserDao.java @@ -0,0 +1,71 @@ +package com.xkcoding.springbootdemocacheredis.dao; + +import com.google.common.collect.Lists; +import com.xkcoding.springbootdemocacheredis.entity.User; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.Date; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +@Slf4j +@Component +public class UserDao { + private static List users = Lists.newArrayList(); + private static AtomicLong SEQ = new AtomicLong(0L); + + public User save() { + log.info("save....."); + User user = new User(); + user.setId(SEQ.get()); + user.setName("xkcoding" + SEQ.get()); + user.setBirthDay(new Date()); + users.add(user); + SEQ.getAndIncrement(); + return user; + } + + public User update(Long id) { + log.info("update....."); + for (User user1 : users) { + if (user1.getId().equals(id)) { + user1.setName("修改后的名字"); + return user1; + } + } + return null; + } + + public User delete(Long id) { + log.info("delete....."); + User user = find(id); + users.remove(user); + return user; + } + + public List find() { + log.info("findAll....."); + return users; + } + + public User find(String name) { + log.info("findByName....."); + for (User user : users) { + if (user.getName().equals(name)) { + return user; + } + } + return null; + } + + public User find(Long id) { + log.info("findById....."); + for (User user : users) { + if (user.getId().equals(id)) { + return user; + } + } + return null; + } +} diff --git a/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/entity/User.java b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/entity/User.java new file mode 100644 index 0000000..ca8f46f --- /dev/null +++ b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/entity/User.java @@ -0,0 +1,14 @@ +package com.xkcoding.springbootdemocacheredis.entity; + +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +@Data +public class User implements Serializable { + private static final long serialVersionUID = 1442134563392432775L; + private Long id; + private String name; + private Date birthDay; +} diff --git a/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/service/UserService.java b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/service/UserService.java new file mode 100644 index 0000000..7565d9e --- /dev/null +++ b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/service/UserService.java @@ -0,0 +1,19 @@ +package com.xkcoding.springbootdemocacheredis.service; + +import com.xkcoding.springbootdemocacheredis.entity.User; + +import java.util.List; + +public interface UserService { + User save(); + + User update(Long id); + + User delete(Long id); + + List find(); + + User find(Long id); + + User find(String name); +} diff --git a/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/service/impl/UserServiceImpl.java b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..5e1121c --- /dev/null +++ b/spring-boot-demo-cache-redis/src/main/java/com/xkcoding/springbootdemocacheredis/service/impl/UserServiceImpl.java @@ -0,0 +1,47 @@ +package com.xkcoding.springbootdemocacheredis.service.impl; + +import com.xkcoding.springbootdemocacheredis.dao.UserDao; +import com.xkcoding.springbootdemocacheredis.entity.User; +import com.xkcoding.springbootdemocacheredis.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.CachePut; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class UserServiceImpl implements UserService { + @Autowired + private UserDao userDao; + + @Override + public User save() { + return userDao.save(); + } + + @Override + public User update(Long id) { + return userDao.update(id); + } + + @Override + public User delete(Long id) { + return userDao.delete(id); + } + + @Override + public List find() { + return userDao.find(); + } + + @Override + public User find(Long id) { + return userDao.find(id); + } + + @Override + public User find(String name) { + return userDao.find(name); + } +} diff --git a/spring-boot-demo-cache-redis/src/main/resources/application.yml b/spring-boot-demo-cache-redis/src/main/resources/application.yml new file mode 100644 index 0000000..a3e2c1c --- /dev/null +++ b/spring-boot-demo-cache-redis/src/main/resources/application.yml @@ -0,0 +1,7 @@ +server: + port: 8080 + context-path: /demo +spring: + redis: + host: localhost + port: 6379 \ No newline at end of file diff --git a/spring-boot-demo-cache-redis/src/test/java/com/xkcoding/springbootdemocacheredis/SpringBootDemoCacheRedisApplicationTests.java b/spring-boot-demo-cache-redis/src/test/java/com/xkcoding/springbootdemocacheredis/SpringBootDemoCacheRedisApplicationTests.java new file mode 100644 index 0000000..98915c9 --- /dev/null +++ b/spring-boot-demo-cache-redis/src/test/java/com/xkcoding/springbootdemocacheredis/SpringBootDemoCacheRedisApplicationTests.java @@ -0,0 +1,16 @@ +package com.xkcoding.springbootdemocacheredis; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootDemoCacheRedisApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-boot-demo-parent/pom.xml b/spring-boot-demo-parent/pom.xml index 215037e..c82c408 100644 --- a/spring-boot-demo-parent/pom.xml +++ b/spring-boot-demo-parent/pom.xml @@ -18,6 +18,7 @@ ../spring-boot-demo-logback ../spring-boot-demo-jpa ../spring-boot-demo-mybatis + ../spring-boot-demo-cache-redis