Skip to content

Commit bdaff26

Browse files
author
liqiangqiang
committed
Spring Boot 注解实现整合 Redis 作为缓存
1 parent 26656f6 commit bdaff26

7 files changed

Lines changed: 326 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>springboot</groupId>
7+
<artifactId>springboot-mybatis-redis-annotation</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>springboot-mybatis-redis-annotation :: 注解实现整合 Redis 作为缓存</name>
10+
11+
<!-- Spring Boot 启动父依赖 -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>1.5.3.RELEASE</version>
16+
</parent>
17+
18+
<properties>
19+
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
20+
<mysql-connector>5.1.39</mysql-connector>
21+
<fastjson-version>1.2.32</fastjson-version>
22+
</properties>
23+
24+
<dependencies>
25+
26+
<!-- Spring Boot Data Cache 依赖 -->
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-cache</artifactId>
30+
</dependency>
31+
32+
<!-- Spring Boot Data Redis 依赖 -->
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-redis</artifactId>
36+
</dependency>
37+
38+
<!-- Spring Boot Web 依赖 -->
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-web</artifactId>
42+
</dependency>
43+
44+
<!-- Spring Boot Test 依赖 -->
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
51+
<!-- Junit -->
52+
<dependency>
53+
<groupId>junit</groupId>
54+
<artifactId>junit</artifactId>
55+
<version>4.12</version>
56+
</dependency>
57+
</dependencies>
58+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.spring.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot 应用启动类
8+
*
9+
* Created by bysocket on 16/4/26.
10+
*/
11+
// Spring Boot 应用的标识
12+
@SpringBootApplication
13+
public class Application {
14+
15+
public static void main(String[] args) {
16+
// 程序启动入口
17+
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18+
SpringApplication.run(Application.class,args);
19+
}
20+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.spring.springboot.domain;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* 城市实体类
7+
*
8+
* Created by bysocket on 07/02/2017.
9+
*/
10+
public class City implements Serializable {
11+
12+
private static final long serialVersionUID = -1L;
13+
14+
/**
15+
* 城市编号
16+
*/
17+
private Long id;
18+
19+
/**
20+
* 省份编号
21+
*/
22+
private Long provinceId;
23+
24+
/**
25+
* 城市名称
26+
*/
27+
private String cityName;
28+
29+
/**
30+
* 描述
31+
*/
32+
private String description;
33+
34+
public City(Long id, Long provinceId, String cityName, String description) {
35+
this.id = id;
36+
this.provinceId = provinceId;
37+
this.cityName = cityName;
38+
this.description = description;
39+
}
40+
41+
public Long getId() {
42+
return id;
43+
}
44+
45+
public void setId(Long id) {
46+
this.id = id;
47+
}
48+
49+
public Long getProvinceId() {
50+
return provinceId;
51+
}
52+
53+
public void setProvinceId(Long provinceId) {
54+
this.provinceId = provinceId;
55+
}
56+
57+
public String getCityName() {
58+
return cityName;
59+
}
60+
61+
public void setCityName(String cityName) {
62+
this.cityName = cityName;
63+
}
64+
65+
public String getDescription() {
66+
return description;
67+
}
68+
69+
public void setDescription(String description) {
70+
this.description = description;
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return "City{" +
76+
"id=" + id +
77+
", provinceId=" + provinceId +
78+
", cityName='" + cityName + '\'' +
79+
", description='" + description + '\'' +
80+
'}';
81+
}
82+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.spring.springboot.service;
2+
3+
import org.spring.springboot.domain.City;
4+
5+
import java.util.List;
6+
7+
/**
8+
* 城市业务逻辑接口类
9+
*
10+
* Created by bysocket on 07/02/2017.
11+
*/
12+
public interface CityService {
13+
14+
/**
15+
* 获取城市
16+
*
17+
*/
18+
City getCityByName(String cityName);
19+
20+
/**
21+
* 新增城市信息
22+
*
23+
*/
24+
void saveCity(City city);
25+
26+
/**
27+
* 更新城市信息
28+
*
29+
*/
30+
void updateCityDescription(String cityName, String description);
31+
32+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.spring.springboot.service.impl;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.spring.springboot.domain.City;
6+
import org.spring.springboot.service.CityService;
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.HashMap;
12+
import java.util.Map;
13+
14+
/**
15+
* 城市业务逻辑实现类
16+
* <p>
17+
* Created by bysocket on 07/02/2017.
18+
*/
19+
@Service
20+
public class CityServiceImpl implements CityService {
21+
22+
23+
// 模拟数据库存储
24+
private Map<String, City> cityMap = new HashMap<String, City>();
25+
26+
public void saveCity(City city){
27+
// 模拟数据库插入操作
28+
cityMap.put(city.getCityName(), city);
29+
}
30+
31+
@Cacheable(value = "baseCityInfo")
32+
public City getCityByName(String cityName){
33+
// 模拟数据库查询并返回
34+
return cityMap.get(cityName);
35+
}
36+
37+
@CachePut(value = "baseCityInfo")
38+
public void updateCityDescription(String cityName, String description){
39+
City city = cityMap.get(cityName);
40+
city.setDescription(description);
41+
// 模拟更新数据库
42+
cityMap.put(cityName, city);
43+
}
44+
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Redis 配置
2+
## Redis数据库索引(默认为0)
3+
spring.redis.database=0
4+
## Redis服务器地址
5+
spring.redis.host=127.0.0.1
6+
## Redis服务器连接端口
7+
spring.redis.port=6379
8+
## Redis服务器连接密码(默认为空)
9+
spring.redis.password=
10+
## 连接池最大连接数(使用负值表示没有限制)
11+
spring.redis.pool.max-active=8
12+
## 连接池最大阻塞等待时间(使用负值表示没有限制)
13+
spring.redis.pool.max-wait=-1
14+
## 连接池中的最大空闲连接
15+
spring.redis.pool.max-idle=8
16+
## 连接池中的最小空闲连接
17+
spring.redis.pool.min-idle=0
18+
## 连接超时时间(毫秒)
19+
spring.redis.timeout=0
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.spring.springboot;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.spring.springboot.domain.City;
8+
import org.spring.springboot.service.CityService;
9+
import org.spring.springboot.service.impl.CityServiceImpl;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
14+
/**
15+
* Created by bysocket on 05/06/2017.
16+
*/
17+
@RunWith(SpringRunner.class)
18+
@SpringBootTest
19+
public class ApplicationTests {
20+
21+
private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class);
22+
23+
24+
@Autowired
25+
private CityService cityService;
26+
27+
@Test
28+
public void testRedis() {
29+
City city = getShanghai();
30+
// 向 redis 中存入数据
31+
cityService.saveCity(city);
32+
33+
// 从 redis 中取数据
34+
City cityInfo = cityService.getCityByName("上海");
35+
36+
LOGGER.info(cityInfo.toString());
37+
38+
}
39+
40+
@Test
41+
public void testRedisCache() {
42+
City city = getBeijing();
43+
// 向 redis 中存入数据
44+
cityService.saveCity(city);
45+
46+
// 从 redis 中取数据, 第一次查询
47+
City cityInfo = cityService.getCityByName("北京");
48+
LOGGER.info("第一次查询:" + cityInfo.toString());
49+
50+
// 从 redis 中取数据, 第二次查询
51+
cityInfo = cityService.getCityByName("北京");
52+
LOGGER.info("第二次查询:" + cityInfo.toString());
53+
54+
// 更新 city 的描述信息后查询
55+
cityService.updateCityDescription("北京", "想不想去北京玩玩呢?");
56+
cityInfo = cityService.getCityByName("北京");
57+
LOGGER.info("更新描述后查询:" + cityInfo.toString());
58+
59+
}
60+
61+
62+
63+
private City getShanghai(){
64+
return new City(1L, 10L, "上海", "人称魔都的地方");
65+
}
66+
67+
private City getBeijing(){
68+
return new City(2L, 20L, "北京", "中国帝都");
69+
}
70+
}

0 commit comments

Comments
 (0)