Skip to content

Commit 8091036

Browse files
committed
cache2 is work
1 parent 08cd977 commit 8091036

File tree

11 files changed

+437
-0
lines changed

11 files changed

+437
-0
lines changed

springboot-Cache2/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.us</groupId>
8+
<artifactId>springboot-Cache2</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.3.0.RELEASE</version>
14+
</parent>
15+
16+
<properties>
17+
<start-class>com.us.Application</start-class>
18+
19+
<maven.compiler.target>1.8</maven.compiler.target>
20+
<maven.compiler.source>1.8</maven.compiler.source>
21+
</properties>
22+
23+
<!-- Add typical dependencies for a web application -->
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-cache</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
38+
<!--缓存-->
39+
<dependency>
40+
<groupId>com.google.guava</groupId>
41+
<artifactId>guava</artifactId>
42+
<version>18.0</version>
43+
</dependency>
44+
45+
<!--db-->
46+
47+
<dependency>
48+
<groupId>mysql</groupId>
49+
<artifactId>mysql-connector-java</artifactId>
50+
<version>6.0.5</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.mchange</groupId>
54+
<artifactId>c3p0</artifactId>
55+
<version>0.9.5.2</version>
56+
<exclusions>
57+
<exclusion>
58+
<groupId>commons-logging</groupId>
59+
<artifactId>commons-logging</artifactId>
60+
</exclusion>
61+
</exclusions>
62+
</dependency>
63+
64+
</dependencies>
65+
66+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.us.example;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.cache.annotation.EnableCaching;
5+
import org.springframework.context.ConfigurableApplicationContext;
6+
import org.springframework.context.annotation.ComponentScan;
7+
8+
import static org.springframework.boot.SpringApplication.*;
9+
10+
/**
11+
* Created by yangyibo on 17/1/13.
12+
*/
13+
14+
@ComponentScan(basePackages ="com.us.example")
15+
@SpringBootApplication
16+
@EnableCaching
17+
public class Application {
18+
public static void main(String[] args) {
19+
ConfigurableApplicationContext run = run(Application.class, args);
20+
}
21+
22+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.us.example.bean;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
import javax.persistence.Table;
7+
import java.io.Serializable;
8+
9+
/**
10+
* Created by yangyibo on 17/1/13.
11+
*/
12+
@Entity
13+
@Table(name = "Person")
14+
public class Person implements Serializable {
15+
16+
private static final long serialVersionUID = 133938246231808718L;
17+
18+
@Id
19+
@GeneratedValue
20+
private Long id;
21+
22+
private String name;
23+
24+
private Integer age;
25+
26+
private String address;
27+
28+
public Person() {
29+
super();
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public void setName(String name) {
45+
this.name = name;
46+
}
47+
48+
public Integer getAge() {
49+
return age;
50+
}
51+
52+
public void setAge(Integer age) {
53+
this.age = age;
54+
}
55+
56+
public String getAddress() {
57+
return address;
58+
}
59+
60+
public void setAddress(String address) {
61+
this.address = address;
62+
}
63+
64+
public Person(Long id, String name, Integer age, String address) {
65+
super();
66+
this.id = id;
67+
this.name = name;
68+
this.age = age;
69+
this.address = address;
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return "Person{" +
75+
"id=" + id +
76+
", name=" + name +
77+
", age=" + age +
78+
", address=" + address +
79+
'}';
80+
}
81+
82+
}
83+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.us.example.config;
2+
3+
import com.us.example.bean.Person;
4+
import com.us.example.service.DemoService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cache.Cache;
7+
import org.springframework.cache.CacheManager;
8+
import org.springframework.cache.guava.GuavaCacheManager;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
/**
16+
* Created by yangyibo on 17/3/2.
17+
*/
18+
@Configuration
19+
public class CacheConfig {
20+
21+
@Autowired
22+
private DemoService demoService;
23+
24+
@Bean
25+
public CacheManager getCacheManager() {
26+
List<Person> personList = demoService.findAll();
27+
//所有缓存的名字
28+
List<String> cacheNames = new ArrayList();
29+
GuavaCacheManager cacheManager = new GuavaCacheManager();
30+
//GuavaCacheManager 的数据结构类似 Map<String,Map<Object,Object>> map =new HashMap<>();
31+
32+
//将数据放入缓存
33+
personList.stream().forEach(person -> {
34+
//用person 的id cacheName
35+
String cacheName=person.getId().toString();
36+
if(cacheManager.getCache(cacheName)==null){
37+
//为每一个person 如果不存在,创建一个新的缓存对象
38+
cacheNames.add(cacheName);
39+
cacheManager.setCacheNames(cacheNames);
40+
}
41+
Cache cache = cacheManager.getCache(cacheName);
42+
//缓存对象用person的id当作缓存的key 用person 当作缓存的value
43+
cache.put(person.getId(),person);
44+
System.out.println("为 ID 为"+cacheName+ "的person 数据做了缓存");
45+
});
46+
return cacheManager;
47+
}
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.us.example.config;
2+
3+
/**
4+
* Created by yangyibo on 17/1/13.
5+
*/
6+
import java.beans.PropertyVetoException;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.core.env.Environment;
12+
import com.mchange.v2.c3p0.ComboPooledDataSource;
13+
14+
@Configuration
15+
public class DBConfig {
16+
@Autowired
17+
private Environment env;
18+
19+
@Bean(name="dataSource")
20+
public ComboPooledDataSource dataSource() throws PropertyVetoException {
21+
ComboPooledDataSource dataSource = new ComboPooledDataSource();
22+
dataSource.setDriverClass(env.getProperty("ms.db.driverClassName"));
23+
dataSource.setJdbcUrl(env.getProperty("ms.db.url"));
24+
dataSource.setUser(env.getProperty("ms.db.username"));
25+
dataSource.setPassword(env.getProperty("ms.db.password"));
26+
dataSource.setMaxPoolSize(20);
27+
dataSource.setMinPoolSize(5);
28+
dataSource.setInitialPoolSize(10);
29+
dataSource.setMaxIdleTime(300);
30+
dataSource.setAcquireIncrement(5);
31+
dataSource.setIdleConnectionTestPeriod(60);
32+
33+
return dataSource;
34+
}
35+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.us.example.config;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import javax.persistence.EntityManagerFactory;
7+
import javax.sql.DataSource;
8+
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.ComponentScan;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
14+
import org.springframework.orm.jpa.JpaTransactionManager;
15+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
16+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
17+
import org.springframework.transaction.PlatformTransactionManager;
18+
import org.springframework.transaction.annotation.EnableTransactionManagement;
19+
20+
/**
21+
* Created by yangyibo on 17/1/13.
22+
*/
23+
24+
@Configuration
25+
@EnableJpaRepositories("com.us.example.dao")
26+
@EnableTransactionManagement
27+
@ComponentScan
28+
public class JpaConfig {
29+
@Autowired
30+
private DataSource dataSource;
31+
32+
@Bean
33+
public EntityManagerFactory entityManagerFactory() {
34+
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
35+
//vendorAdapter.setShowSql(true);
36+
//vendorAdapter.setGenerateDdl(true);
37+
38+
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
39+
factory.setJpaVendorAdapter(vendorAdapter);
40+
factory.setPackagesToScan("com.us.example.bean");
41+
factory.setDataSource(dataSource);
42+
43+
44+
Map<String, Object> jpaProperties = new HashMap<>();
45+
jpaProperties.put("hibernate.ejb.naming_strategy","org.hibernate.cfg.ImprovedNamingStrategy");
46+
jpaProperties.put("hibernate.jdbc.batch_size",50);
47+
//jpaProperties.put("hibernate.show_sql",true);
48+
49+
factory.setJpaPropertyMap(jpaProperties);
50+
factory.afterPropertiesSet();
51+
return factory.getObject();
52+
}
53+
54+
@Bean
55+
public PlatformTransactionManager transactionManager() {
56+
57+
JpaTransactionManager txManager = new JpaTransactionManager();
58+
txManager.setEntityManagerFactory(entityManagerFactory());
59+
return txManager;
60+
}
61+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.us.example.controller;
2+
import com.us.example.bean.Person;
3+
import com.us.example.service.PersonService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
/**
8+
* Created by yangyibo on 17/1/13.
9+
*/
10+
@RestController
11+
@RequestMapping(value = "/person")
12+
public class CacheController {
13+
14+
@Autowired
15+
private PersonService personService;
16+
17+
18+
@RequestMapping(method = RequestMethod.POST)
19+
@ResponseBody
20+
public Person put(Person person){
21+
return personService.save(person);
22+
23+
}
24+
25+
//http://localhost:8080/person/1
26+
@RequestMapping(value ="/{id}" ,method = RequestMethod.GET)
27+
@ResponseBody
28+
public Person cacheable( @PathVariable Long id){
29+
return personService.findOne(id);
30+
31+
}
32+
33+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.us.example.dao;
2+
3+
import com.us.example.bean.Person;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
/**
7+
* Created by yangyibo on 17/1/13.
8+
*/
9+
public interface PersonRepository extends JpaRepository<Person, Long> {
10+
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.us.example.service;
2+
3+
import com.us.example.bean.Person;
4+
import com.us.example.dao.PersonRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Created by yangyibo on 17/1/13.
12+
*/
13+
@Service
14+
public class DemoService {
15+
16+
@Autowired
17+
private PersonRepository personRepository;
18+
19+
20+
public List<Person> findAll() {
21+
return personRepository.findAll();
22+
}
23+
24+
}

0 commit comments

Comments
 (0)