Spring Boot 整合Redis 实现缓存
2020-12-13 02:03
标签:reids 说明 环境 cell 二次 string autowire highlight config Spring Boot 整合Redis 实现缓存 标签:reids 说明 环境 cell 二次 string autowire highlight config 原文地址:https://www.cnblogs.com/Soy-technology/p/11025089.html一、缓存的应用场景
二、更新缓存的策略
三、运行 springboot-mybatis-redis 工程案例
CREATE DATABASE springbootdb;
DROP TABLE IF EXISTS `city`;
CREATE TABLE `city` (
`
id
` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT
‘城市编号‘
,
`province_id` int(10) unsigned NOT NULL COMMENT
‘省份编号‘
,
`city_name` varchar(25) DEFAULT NULL COMMENT
‘城市名称‘
,
`description` varchar(25) DEFAULT NULL COMMENT
‘描述‘
,
PRIMARY KEY (`
id
`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT city VALUES (1 ,1,
‘温岭市‘
,
‘BYSocket 的家在温岭。‘
);
springboot-mybatis-redis 工程项目结构如下图所示:
org.spring.springboot.controller - Controller 层
org.spring.springboot.dao - 数据操作层 DAO
org.spring.springboot.domain - 实体类
org.spring.springboot.service - 业务逻辑层
Application - 应用启动类
application.properties - 应用配置文件,应用启动会自动读取配置
mvn clean
install
2017-04-13 18:29:00.273 INFO 13038 --- [nio-8080-
exec
-1] o.s.s.service.impl.CityServiceImpl : CityServiceImpl.findCityById() : 城市插入缓存 >> City{
id
=12, provinceId=3, cityName=
‘三亚‘
, description=
‘水好,天蓝‘
}
2017-04-13 18:29:03.145 INFO 13038 --- [nio-8080-
exec
-2] o.s.s.service.impl.CityServiceImpl : CityServiceImpl.findCityById() : 从缓存中获取了城市 >> City{
id
=12, provinceId=3, cityName=
‘三亚‘
, description=
‘水好,天蓝‘
}
2017-04-13 18:29:52.248 INFO 13038 --- [nio-8080-
exec
-9] o.s.s.service.impl.CityServiceImpl : CityServiceImpl.deleteCity() : 从缓存中删除城市 ID >> 12
四、springboot-mybatis-redis 工程代码配置详解
"1.0"
encoding=
"UTF-8"
?>
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
/modelVersion
>
/groupId
>
springboot-mybatis-redis
/artifactId
>
/version
>
/name
>
/groupId
>
spring-boot-starter-parent
/artifactId
>
/version
>
/parent
>
/mybatis-spring-boot
>
/mysql-connector
>
/spring-boot-starter-redis-version
>
/properties
>
/groupId
>
spring-boot-starter-redis
/artifactId
>
/version
>
/dependency
>
/groupId
>
spring-boot-starter-web
/artifactId
>
/dependency
>
/groupId
>
spring-boot-starter-
test
/artifactId
>
test
/scope
>
/dependency
>
/groupId
>
mybatis-spring-boot-starter
/artifactId
>
/version
>
/dependency
>
/groupId
>
mysql-connector-java
/artifactId
>
/version
>
/dependency
>
/groupId
>
junit
/artifactId
>
/version
>
/dependency
>
/dependencies
>
/project
>
## 数据源配置
spring.datasource.url=jdbc:mysql:
//localhost
:3306
/springbootdb
?useUnicode=
true
&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=0
Serializable
java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of
type
package org.spring.springboot.domain;
import
java.io.Serializable;
/**
* 城市实体类
*
* Created by bysocket on 07
/02/2017
.
*/
public class City implements Serializable {
private static final long serialVersionUID = -1L;
/**
* 城市编号
*/
private Long
id
;
/**
* 省份编号
*/
private Long provinceId;
/**
* 城市名称
*/
private String cityName;
/**
* 描述
*/
private String description;
public Long getId() {
return
id
;
}
public void setId(Long
id
) {
this.
id
=
id
;
}
public Long getProvinceId() {
return
provinceId;
}
public void setProvinceId(Long provinceId) {
this.provinceId = provinceId;
}
public String getCityName() {
return
cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getDescription() {
return
description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return
"City{"
+
"id="
+
id
+
", provinceId="
+ provinceId +
", cityName=‘"
+ cityName + ‘\
‘‘
+
", description=‘"
+ description + ‘\
‘‘
+
‘}‘
;
}
}
package org.spring.springboot.service.impl;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
org.spring.springboot.dao.CityDao;
import
org.spring.springboot.domain.City;
import
org.spring.springboot.service.CityService;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.data.redis.core.RedisTemplate;
import
org.springframework.data.redis.core.StringRedisTemplate;
import
org.springframework.data.redis.core.ValueOperations;
import
org.springframework.stereotype.Service;
import
java.util.List;
import
java.util.concurrent.TimeUnit;
/**
* 城市业务逻辑实现类
*
* Created by bysocket on 07
/02/2017
.
*/
@Service
public class CityServiceImpl implements CityService {
private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class);
@Autowired
private CityDao cityDao;
@Autowired
private RedisTemplate redisTemplate;
/**
* 获取城市逻辑:
* 如果缓存存在,从缓存中获取城市信息
* 如果缓存不存在,从 DB 中获取城市信息,然后插入缓存
*/
public City findCityById(Long
id
) {
//
从缓存中获取城市信息
String key =
"city_"
+
id
;
ValueOperations
//
缓存存在
boolean hasKey = redisTemplate.hasKey(key);
if
(hasKey) {
City city = operations.get(key);
LOGGER.info(
"CityServiceImpl.findCityById() : 从缓存中获取了城市 >> "
+ city.toString());
return
city;
}
//
从 DB 中获取城市信息
City city = cityDao.findById(
id
);
//
插入缓存
operations.
set
(key, city, 10, TimeUnit.SECONDS);
LOGGER.info(
"CityServiceImpl.findCityById() : 城市插入缓存 >> "
+ city.toString());
return
city;
}
@Override
public Long saveCity(City city) {
return
cityDao.saveCity(city);
}
/**
* 更新城市逻辑:
* 如果缓存存在,删除
* 如果缓存不存在,不操作
*/
@Override
public Long updateCity(City city) {
Long ret = cityDao.updateCity(city);
//
缓存存在,删除缓存
String key =
"city_"
+ city.getId();
boolean hasKey = redisTemplate.hasKey(key);
if
(hasKey) {
redisTemplate.delete(key);
LOGGER.info(
"CityServiceImpl.updateCity() : 从缓存中删除城市 >> "
+ city.toString());
}
return
ret;
}
@Override
public Long deleteCity(Long
id
) {
Long ret = cityDao.deleteCity(
id
);
//
缓存存在,删除缓存
String key =
"city_"
+
id
;
boolean hasKey = redisTemplate.hasKey(key);
if
(hasKey) {
redisTemplate.delete(key);
LOGGER.info(
"CityServiceImpl.deleteCity() : 从缓存中删除城市 ID >> "
+
id
);
}
return
ret;
}
}
五、小结