mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 14:13:52 +08:00
Spring Boot 注解实现整合 Redis 作为缓存
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package org.spring.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Spring Boot 应用启动类
|
||||
*
|
||||
* Created by bysocket on 16/4/26.
|
||||
*/
|
||||
// Spring Boot 应用的标识
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 程序启动入口
|
||||
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
|
||||
SpringApplication.run(Application.class,args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
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 City(Long id, Long provinceId, String cityName, String description) {
|
||||
this.id = id;
|
||||
this.provinceId = provinceId;
|
||||
this.cityName = cityName;
|
||||
this.description = 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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.spring.springboot.service;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 城市业务逻辑接口类
|
||||
*
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
public interface CityService {
|
||||
|
||||
/**
|
||||
* 获取城市
|
||||
*
|
||||
*/
|
||||
City getCityByName(String cityName);
|
||||
|
||||
/**
|
||||
* 新增城市信息
|
||||
*
|
||||
*/
|
||||
void saveCity(City city);
|
||||
|
||||
/**
|
||||
* 更新城市信息
|
||||
*
|
||||
*/
|
||||
void updateCityDescription(String cityName, String description);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.spring.springboot.service.impl;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.service.CityService;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 城市业务逻辑实现类
|
||||
* <p>
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
@Service
|
||||
public class CityServiceImpl implements CityService {
|
||||
|
||||
|
||||
// 模拟数据库存储
|
||||
private Map<String, City> cityMap = new HashMap<String, City>();
|
||||
|
||||
public void saveCity(City city){
|
||||
// 模拟数据库插入操作
|
||||
cityMap.put(city.getCityName(), city);
|
||||
}
|
||||
|
||||
@Cacheable(value = "baseCityInfo")
|
||||
public City getCityByName(String cityName){
|
||||
// 模拟数据库查询并返回
|
||||
return cityMap.get(cityName);
|
||||
}
|
||||
|
||||
@CachePut(value = "baseCityInfo")
|
||||
public void updateCityDescription(String cityName, String description){
|
||||
City city = cityMap.get(cityName);
|
||||
city.setDescription(description);
|
||||
// 模拟更新数据库
|
||||
cityMap.put(cityName, city);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
## 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
|
||||
Reference in New Issue
Block a user