Skip to content

Commit 3576a7e

Browse files
committed
使用Caching-EhCache
1 parent 0132346 commit 3576a7e

40 files changed

+1527
-0
lines changed

spring-boot-demo-19-1/pom.xml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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>com.roncoo.education</groupId>
7+
<artifactId>spring-boot-demo-19-1</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-demo-19-1</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.4.0.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-devtools</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-freemarker</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.webjars</groupId>
38+
<artifactId>jquery</artifactId>
39+
<version>2.1.4</version>
40+
</dependency>
41+
42+
<!-- 数据库 -->
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-data-jpa</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.h2database</groupId>
49+
<artifactId>h2</artifactId>
50+
<scope>runtime</scope>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-data-redis</artifactId>
56+
</dependency>
57+
58+
<!-- mongodb -->
59+
<dependency>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>de.flapdoodle.embed</groupId>
65+
<artifactId>de.flapdoodle.embed.mongo</artifactId>
66+
</dependency>
67+
68+
<!-- caching -->
69+
<dependency>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-starter-cache</artifactId>
72+
</dependency>
73+
<dependency>
74+
<groupId>net.sf.ehcache</groupId>
75+
<artifactId>ehcache</artifactId>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>org.apache.httpcomponents</groupId>
80+
<artifactId>httpclient</artifactId>
81+
</dependency>
82+
83+
<dependency>
84+
<groupId>org.springframework.boot</groupId>
85+
<artifactId>spring-boot-starter-test</artifactId>
86+
<scope>test</scope>
87+
</dependency>
88+
</dependencies>
89+
90+
<build>
91+
<plugins>
92+
<plugin>
93+
<groupId>org.springframework.boot</groupId>
94+
<artifactId>spring-boot-maven-plugin</artifactId>
95+
</plugin>
96+
</plugins>
97+
</build>
98+
99+
100+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.roncoo.example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.web.servlet.ServletComponentScan;
6+
import org.springframework.cache.annotation.EnableCaching;
7+
8+
@EnableCaching
9+
@ServletComponentScan
10+
@SpringBootApplication
11+
public class SpringBootDemo191Application {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(SpringBootDemo191Application.class, args);
15+
}
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.roncoo.example.bean;
2+
3+
import java.util.Date;
4+
5+
public class RoncooUser {
6+
private int id;
7+
private String name;
8+
private Date createTime;
9+
10+
public int getId() {
11+
return id;
12+
}
13+
14+
public void setId(int id) {
15+
this.id = id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public Date getCreateTime() {
27+
return createTime;
28+
}
29+
30+
public void setCreateTime(Date createTime) {
31+
this.createTime = createTime;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "RoncooUser [id=" + id + ", name=" + name + ", createTime=" + createTime + "]";
37+
}
38+
39+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.roncoo.example.bean;
2+
3+
import java.util.Date;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
10+
@Entity
11+
public class RoncooUserLog {
12+
13+
@Id
14+
@GeneratedValue
15+
private Integer id;
16+
17+
@Column
18+
private Date createTime;
19+
20+
@Column
21+
private String userName;
22+
23+
@Column
24+
private String userIp;
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public Date getCreateTime() {
35+
return createTime;
36+
}
37+
38+
public void setCreateTime(Date createTime) {
39+
this.createTime = createTime;
40+
}
41+
42+
public String getUserName() {
43+
return userName;
44+
}
45+
46+
public void setUserName(String userName) {
47+
this.userName = userName;
48+
}
49+
50+
public String getUserIp() {
51+
return userIp;
52+
}
53+
54+
public void setUserIp(String userIp) {
55+
this.userIp = userIp;
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return "RoncooUserLog [id=" + id + ", createTime=" + createTime + ", userName=" + userName + ", userIp=" + userIp + "]";
61+
}
62+
63+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.roncoo.example.cache;
2+
3+
import com.roncoo.example.bean.RoncooUserLog;
4+
5+
/**
6+
* @author wujing
7+
*/
8+
public interface RoncooUserLogCache {
9+
10+
/**
11+
* 查询
12+
*
13+
* @param id
14+
* @return
15+
*/
16+
RoncooUserLog selectById(Integer id);
17+
18+
/**
19+
* 更新
20+
*
21+
* @param roncooUserLog
22+
* @return
23+
*/
24+
RoncooUserLog updateById(RoncooUserLog roncooUserLog);
25+
26+
/**
27+
* 删除
28+
*
29+
* @param id
30+
* @return
31+
*/
32+
String deleteById(Integer id);
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.roncoo.example.cache.impl;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.cache.annotation.CacheConfig;
5+
import org.springframework.cache.annotation.CacheEvict;
6+
import org.springframework.cache.annotation.CachePut;
7+
import org.springframework.cache.annotation.Cacheable;
8+
import org.springframework.stereotype.Repository;
9+
10+
import com.roncoo.example.bean.RoncooUserLog;
11+
import com.roncoo.example.cache.RoncooUserLogCache;
12+
import com.roncoo.example.dao.RoncooUserLogDao;
13+
14+
/**
15+
* @author wujing
16+
*/
17+
@CacheConfig(cacheNames = "roncooCache")
18+
@Repository
19+
public class RoncooUserLogCacheImpl implements RoncooUserLogCache {
20+
21+
@Autowired
22+
private RoncooUserLogDao roncooUserLogDao;
23+
24+
@Cacheable(key = "#p0")
25+
@Override
26+
public RoncooUserLog selectById(Integer id) {
27+
System.out.println("查询功能,缓存找不到,直接读库, id=" + id);
28+
return roncooUserLogDao.findOne(id);
29+
}
30+
31+
@CachePut(key = "#p0")
32+
@Override
33+
public RoncooUserLog updateById(RoncooUserLog roncooUserLog) {
34+
System.out.println("更新功能,更新缓存,直接写库, id=" + roncooUserLog);
35+
return roncooUserLogDao.save(roncooUserLog);
36+
}
37+
38+
@CacheEvict(key = "#p0")
39+
@Override
40+
public String deleteById(Integer id) {
41+
System.out.println("删除功能,删除缓存,直接写库, id=" + id);
42+
return "清空缓存成功";
43+
}
44+
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.roncoo.example.component;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.mongodb.core.MongoTemplate;
5+
import org.springframework.data.mongodb.core.query.Criteria;
6+
import org.springframework.data.mongodb.core.query.Query;
7+
import org.springframework.data.mongodb.core.query.Update;
8+
import org.springframework.stereotype.Component;
9+
10+
import com.roncoo.example.bean.RoncooUser;
11+
12+
/**
13+
* @author wujing
14+
*/
15+
@Component
16+
public class RoncooMongodbComponent {
17+
18+
@Autowired
19+
private MongoTemplate mongoTemplate;
20+
21+
public void insert(RoncooUser roncooUser) {
22+
mongoTemplate.insert(roncooUser);
23+
}
24+
25+
public void deleteById(int id) {
26+
Criteria criteria = Criteria.where("id").in(id);
27+
Query query = new Query(criteria);
28+
mongoTemplate.remove(query, RoncooUser.class);
29+
}
30+
31+
public void updateById(RoncooUser roncooUser) {
32+
Criteria criteria = Criteria.where("id").in(roncooUser.getId());
33+
Query query = new Query(criteria);
34+
Update update = new Update();
35+
update.set("name", roncooUser.getName());
36+
update.set("createTime", roncooUser.getCreateTime());
37+
mongoTemplate.updateMulti(query, update, RoncooUser.class);
38+
}
39+
40+
public RoncooUser selectById(int id) {
41+
Criteria criteria = Criteria.where("id").in(id);
42+
Query query = new Query(criteria);
43+
return mongoTemplate.findOne(query, RoncooUser.class);
44+
}
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.roncoo.example.component;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.redis.core.StringRedisTemplate;
5+
import org.springframework.data.redis.core.ValueOperations;
6+
import org.springframework.stereotype.Component;
7+
8+
/**
9+
* @author wujing
10+
*/
11+
@Component
12+
public class RoncooRedisComponent {
13+
14+
@Autowired
15+
private StringRedisTemplate stringRedisTemplate;
16+
17+
public void set(String key, String value) {
18+
ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
19+
if (!this.stringRedisTemplate.hasKey(key)) {
20+
ops.set(key, value);
21+
System.out.println("set key success");
22+
} else {
23+
// 存在则打印之前的value值
24+
System.out.println("this key = " + ops.get(key));
25+
}
26+
}
27+
28+
public String get(String key) {
29+
return this.stringRedisTemplate.opsForValue().get(key);
30+
}
31+
32+
public void del(String key) {
33+
this.stringRedisTemplate.delete(key);
34+
}
35+
}

0 commit comments

Comments
 (0)