Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
springBoot
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
WitCloud
springBoot
Commits
8091036e
Commit
8091036e
authored
Mar 02, 2017
by
杨伊博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cache2 is work
parent
08cd9770
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
437 additions
and
0 deletions
+437
-0
pom.xml
springboot-Cache2/pom.xml
+66
-0
Application.java
...boot-Cache2/src/main/java/com/us/example/Application.java
+22
-0
Person.java
...boot-Cache2/src/main/java/com/us/example/bean/Person.java
+83
-0
CacheConfig.java
...che2/src/main/java/com/us/example/config/CacheConfig.java
+48
-0
DBConfig.java
...-Cache2/src/main/java/com/us/example/config/DBConfig.java
+35
-0
JpaConfig.java
...Cache2/src/main/java/com/us/example/config/JpaConfig.java
+61
-0
CacheController.java
.../main/java/com/us/example/controller/CacheController.java
+33
-0
PersonRepository.java
...e2/src/main/java/com/us/example/dao/PersonRepository.java
+11
-0
DemoService.java
...he2/src/main/java/com/us/example/service/DemoService.java
+24
-0
PersonService.java
...2/src/main/java/com/us/example/service/PersonService.java
+49
-0
application.properties
springboot-Cache2/src/main/resources/application.properties
+5
-0
No files found.
springboot-Cache2/pom.xml
0 → 100644
View file @
8091036e
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"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>
4.0.0
</modelVersion>
<groupId>
com.us
</groupId>
<artifactId>
springboot-Cache2
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
1.3.0.RELEASE
</version>
</parent>
<properties>
<start-class>
com.us.Application
</start-class>
<maven.compiler.target>
1.8
</maven.compiler.target>
<maven.compiler.source>
1.8
</maven.compiler.source>
</properties>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-cache
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-jpa
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<!--缓存-->
<dependency>
<groupId>
com.google.guava
</groupId>
<artifactId>
guava
</artifactId>
<version>
18.0
</version>
</dependency>
<!--db-->
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<version>
6.0.5
</version>
</dependency>
<dependency>
<groupId>
com.mchange
</groupId>
<artifactId>
c3p0
</artifactId>
<version>
0.9.5.2
</version>
<exclusions>
<exclusion>
<groupId>
commons-logging
</groupId>
<artifactId>
commons-logging
</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
\ No newline at end of file
springboot-Cache2/src/main/java/com/us/example/Application.java
0 → 100644
View file @
8091036e
package
com
.
us
.
example
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cache.annotation.EnableCaching
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.ComponentScan
;
import
static
org
.
springframework
.
boot
.
SpringApplication
.*;
/**
* Created by yangyibo on 17/1/13.
*/
@ComponentScan
(
basePackages
=
"com.us.example"
)
@SpringBootApplication
@EnableCaching
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
ConfigurableApplicationContext
run
=
run
(
Application
.
class
,
args
);
}
}
springboot-Cache2/src/main/java/com/us/example/bean/Person.java
0 → 100644
View file @
8091036e
package
com
.
us
.
example
.
bean
;
import
javax.persistence.Entity
;
import
javax.persistence.GeneratedValue
;
import
javax.persistence.Id
;
import
javax.persistence.Table
;
import
java.io.Serializable
;
/**
* Created by yangyibo on 17/1/13.
*/
@Entity
@Table
(
name
=
"Person"
)
public
class
Person
implements
Serializable
{
private
static
final
long
serialVersionUID
=
133938246231808718L
;
@Id
@GeneratedValue
private
Long
id
;
private
String
name
;
private
Integer
age
;
private
String
address
;
public
Person
()
{
super
();
}
public
Long
getId
()
{
return
id
;
}
public
void
setId
(
Long
id
)
{
this
.
id
=
id
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
Integer
getAge
()
{
return
age
;
}
public
void
setAge
(
Integer
age
)
{
this
.
age
=
age
;
}
public
String
getAddress
()
{
return
address
;
}
public
void
setAddress
(
String
address
)
{
this
.
address
=
address
;
}
public
Person
(
Long
id
,
String
name
,
Integer
age
,
String
address
)
{
super
();
this
.
id
=
id
;
this
.
name
=
name
;
this
.
age
=
age
;
this
.
address
=
address
;
}
@Override
public
String
toString
()
{
return
"Person{"
+
"id="
+
id
+
", name="
+
name
+
", age="
+
age
+
", address="
+
address
+
'}'
;
}
}
springboot-Cache2/src/main/java/com/us/example/config/CacheConfig.java
0 → 100644
View file @
8091036e
package
com
.
us
.
example
.
config
;
import
com.us.example.bean.Person
;
import
com.us.example.service.DemoService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.Cache
;
import
org.springframework.cache.CacheManager
;
import
org.springframework.cache.guava.GuavaCacheManager
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* Created by yangyibo on 17/3/2.
*/
@Configuration
public
class
CacheConfig
{
@Autowired
private
DemoService
demoService
;
@Bean
public
CacheManager
getCacheManager
()
{
List
<
Person
>
personList
=
demoService
.
findAll
();
//所有缓存的名字
List
<
String
>
cacheNames
=
new
ArrayList
();
GuavaCacheManager
cacheManager
=
new
GuavaCacheManager
();
//GuavaCacheManager 的数据结构类似 Map<String,Map<Object,Object>> map =new HashMap<>();
//将数据放入缓存
personList
.
stream
().
forEach
(
person
->
{
//用person 的id cacheName
String
cacheName
=
person
.
getId
().
toString
();
if
(
cacheManager
.
getCache
(
cacheName
)==
null
){
//为每一个person 如果不存在,创建一个新的缓存对象
cacheNames
.
add
(
cacheName
);
cacheManager
.
setCacheNames
(
cacheNames
);
}
Cache
cache
=
cacheManager
.
getCache
(
cacheName
);
//缓存对象用person的id当作缓存的key 用person 当作缓存的value
cache
.
put
(
person
.
getId
(),
person
);
System
.
out
.
println
(
"为 ID 为"
+
cacheName
+
"的person 数据做了缓存"
);
});
return
cacheManager
;
}
}
springboot-Cache2/src/main/java/com/us/example/config/DBConfig.java
0 → 100644
View file @
8091036e
package
com
.
us
.
example
.
config
;
/**
* Created by yangyibo on 17/1/13.
*/
import
java.beans.PropertyVetoException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.env.Environment
;
import
com.mchange.v2.c3p0.ComboPooledDataSource
;
@Configuration
public
class
DBConfig
{
@Autowired
private
Environment
env
;
@Bean
(
name
=
"dataSource"
)
public
ComboPooledDataSource
dataSource
()
throws
PropertyVetoException
{
ComboPooledDataSource
dataSource
=
new
ComboPooledDataSource
();
dataSource
.
setDriverClass
(
env
.
getProperty
(
"ms.db.driverClassName"
));
dataSource
.
setJdbcUrl
(
env
.
getProperty
(
"ms.db.url"
));
dataSource
.
setUser
(
env
.
getProperty
(
"ms.db.username"
));
dataSource
.
setPassword
(
env
.
getProperty
(
"ms.db.password"
));
dataSource
.
setMaxPoolSize
(
20
);
dataSource
.
setMinPoolSize
(
5
);
dataSource
.
setInitialPoolSize
(
10
);
dataSource
.
setMaxIdleTime
(
300
);
dataSource
.
setAcquireIncrement
(
5
);
dataSource
.
setIdleConnectionTestPeriod
(
60
);
return
dataSource
;
}
}
springboot-Cache2/src/main/java/com/us/example/config/JpaConfig.java
0 → 100644
View file @
8091036e
package
com
.
us
.
example
.
config
;
import
java.util.HashMap
;
import
java.util.Map
;
import
javax.persistence.EntityManagerFactory
;
import
javax.sql.DataSource
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.ComponentScan
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.jpa.repository.config.EnableJpaRepositories
;
import
org.springframework.orm.jpa.JpaTransactionManager
;
import
org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
;
import
org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
;
import
org.springframework.transaction.PlatformTransactionManager
;
import
org.springframework.transaction.annotation.EnableTransactionManagement
;
/**
* Created by yangyibo on 17/1/13.
*/
@Configuration
@EnableJpaRepositories
(
"com.us.example.dao"
)
@EnableTransactionManagement
@ComponentScan
public
class
JpaConfig
{
@Autowired
private
DataSource
dataSource
;
@Bean
public
EntityManagerFactory
entityManagerFactory
()
{
HibernateJpaVendorAdapter
vendorAdapter
=
new
HibernateJpaVendorAdapter
();
//vendorAdapter.setShowSql(true);
//vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean
factory
=
new
LocalContainerEntityManagerFactoryBean
();
factory
.
setJpaVendorAdapter
(
vendorAdapter
);
factory
.
setPackagesToScan
(
"com.us.example.bean"
);
factory
.
setDataSource
(
dataSource
);
Map
<
String
,
Object
>
jpaProperties
=
new
HashMap
<>();
jpaProperties
.
put
(
"hibernate.ejb.naming_strategy"
,
"org.hibernate.cfg.ImprovedNamingStrategy"
);
jpaProperties
.
put
(
"hibernate.jdbc.batch_size"
,
50
);
//jpaProperties.put("hibernate.show_sql",true);
factory
.
setJpaPropertyMap
(
jpaProperties
);
factory
.
afterPropertiesSet
();
return
factory
.
getObject
();
}
@Bean
public
PlatformTransactionManager
transactionManager
()
{
JpaTransactionManager
txManager
=
new
JpaTransactionManager
();
txManager
.
setEntityManagerFactory
(
entityManagerFactory
());
return
txManager
;
}
}
springboot-Cache2/src/main/java/com/us/example/controller/CacheController.java
0 → 100644
View file @
8091036e
package
com
.
us
.
example
.
controller
;
import
com.us.example.bean.Person
;
import
com.us.example.service.PersonService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
/**
* Created by yangyibo on 17/1/13.
*/
@RestController
@RequestMapping
(
value
=
"/person"
)
public
class
CacheController
{
@Autowired
private
PersonService
personService
;
@RequestMapping
(
method
=
RequestMethod
.
POST
)
@ResponseBody
public
Person
put
(
Person
person
){
return
personService
.
save
(
person
);
}
//http://localhost:8080/person/1
@RequestMapping
(
value
=
"/{id}"
,
method
=
RequestMethod
.
GET
)
@ResponseBody
public
Person
cacheable
(
@PathVariable
Long
id
){
return
personService
.
findOne
(
id
);
}
}
\ No newline at end of file
springboot-Cache2/src/main/java/com/us/example/dao/PersonRepository.java
0 → 100644
View file @
8091036e
package
com
.
us
.
example
.
dao
;
import
com.us.example.bean.Person
;
import
org.springframework.data.jpa.repository.JpaRepository
;
/**
* Created by yangyibo on 17/1/13.
*/
public
interface
PersonRepository
extends
JpaRepository
<
Person
,
Long
>
{
}
springboot-Cache2/src/main/java/com/us/example/service/DemoService.java
0 → 100644
View file @
8091036e
package
com
.
us
.
example
.
service
;
import
com.us.example.bean.Person
;
import
com.us.example.dao.PersonRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.List
;
/**
* Created by yangyibo on 17/1/13.
*/
@Service
public
class
DemoService
{
@Autowired
private
PersonRepository
personRepository
;
public
List
<
Person
>
findAll
()
{
return
personRepository
.
findAll
();
}
}
\ No newline at end of file
springboot-Cache2/src/main/java/com/us/example/service/PersonService.java
0 → 100644
View file @
8091036e
package
com
.
us
.
example
.
service
;
import
com.us.example.bean.Person
;
import
com.us.example.dao.PersonRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.Cache
;
import
org.springframework.cache.CacheManager
;
import
org.springframework.stereotype.Service
;
/**
* Created by yangyibo on 17/3/2.
*/
@Service
public
class
PersonService
{
@Autowired
CacheManager
cacheManager
;
@Autowired
private
PersonRepository
personRepository
;
public
Person
findOne
(
Long
id
)
{
Person
person
=
getCache
(
id
,
cacheManager
);
if
(
person
!=
null
)
{
System
.
out
.
println
(
"从缓存中取出:"
+
person
.
toString
());
}
else
{
person
=
personRepository
.
findOne
(
id
);
System
.
out
.
println
(
"从数据库中取出:"
+
person
.
toString
());
}
return
person
;
}
public
Person
save
(
Person
person
)
{
Person
p
=
personRepository
.
save
(
person
);
return
p
;
}
public
Person
getCache
(
Long
id
,
CacheManager
cacheManager
)
{
// Person person=(Person) cacheManager.getCache(id.toString()).get(id).get();
Cache
cache
=
cacheManager
.
getCache
(
id
.
toString
());
Cache
.
ValueWrapper
valueWrapper
=
cache
.
get
(
id
);
Person
person
=
(
Person
)
valueWrapper
.
get
();
return
person
;
}
}
springboot-Cache2/src/main/resources/application.properties
0 → 100755
View file @
8091036e
ms.db.driverClassName
=
com.mysql.jdbc.Driver
ms.db.url
=
jdbc:mysql://localhost:3306/cache?prepStmtCacheSize=517&cachePrepStmts=true&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
ms.db.username
=
root
ms.db.password
=
admin
ms.db.maxActive
=
500
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment