8000 Merge pull request #8 from JavaCourse00/main · JavaCourse00/JavaCourseCodes@bce3bd6 · GitHub
[go: up one dir, main page]

Skip to content

Commit bce3bd6

Browse files
authored
Merge pull request #8 from JavaCourse00/main
upgrade from kk
2 parents 6358228 + d5a8f4a commit bce3bd6

File tree

12 files changed

+444
-0
lines changed

12 files changed

+444
-0
lines changed

08cache/cache/pom.xml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.0.9.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>io.kimmking.08cache</groupId>
12+
<artifactId>cache</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>cache</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-jdbc</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.mybatis.spring.boot</groupId>
36+
<artifactId>mybatis-spring-boot-starter</artifactId>
37+
<version>2.1.4</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>mysql</groupId>
41+
<artifactId>mysql-connector-java</artifactId>
42+
<version>5.1.47</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.projectlombok</groupId>
46+
<artifactId>lombok</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-cache</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-data-redis</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>io.lettuce</groupId>
58+
<artifactId>lettuce-core</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.apache.commons</groupId>
62+
<artifactId>commons-pool2</artifactId>
63+
</dependency>
64+
<dependency>
65+
<groupId>net.sf.ehcache</groupId>
66+
<artifactId>ehcache</artifactId>
67+
<version>2.8.3</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.mybatis</groupId>
71+
<artifactId>mybatis-ehcache</artifactId>
72+
<version>1.0.0</version>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>org.springframework.boot</groupId>
77+
<artifactId>spring-boot-starter-test</artifactId>
78+
<scope>test</scope>
79+
<exclusions>
80+
<exclusion>
81+
<groupId>org.junit.vintage</groupId>
82+
<artifactId>junit-vintage-engine</artifactId>
83+
</exclusion>
84+
</exclusions>
85+
</dependency>
86+
</dependencies>
87+
88+
<build>
89+
<plugins>
90+
<plugin>
91+
<groupId>org.springframework.boot</groupId>
92+
<artifactId>spring-boot-maven-plugin</artifactId>
93+
</plugin>
94+
</plugins>
95+
</build>
96+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.kimmking.cache;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cache.annotation.EnableCaching;
7+
8+
@SpringBootApplication(scanBasePackages = "io.kimmking.cache")
9+
@MapperScan("io.kimmking.cache.mapper")
10+
@EnableCaching
11+
public class CacheApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(CacheApplication.class, args);
15+
}
16+
17+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package io.kimmking.cache;
2+
3+
import org.springframework.cache.CacheManager;
4+
import org.springframework.cache.annotation.CachingConfigurerSupport;
5+
import org.springframework.cache.interceptor.*;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.data.redis.cache.RedisCacheConfiguration;
9+
import org.springframework.data.redis.cache.RedisCacheManager;
10+
import org.springframework.data.redis.connection.RedisConnectionFactory;
11+
import org.springframework.data.redis.core.RedisTemplate;
12+
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
13+
import org.springframework.data.redis.serializer.RedisSerializationContext;
14+
import org.springframework.data.redis.serializer.StringRedisSerializer;
15+
16+
import javax.annotation.Resource;
17+
18+
import static org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig;
19+
20+
@Configuration
21+
public class CacheConfig extends CachingConfigurerSupport {
22+
23+
@Resource
24+
private RedisConnectionFactory factory;
25+
26+
/**
27+
* 自定义生成redis-key
28+
*
29+
* @return
30+
*/
31+
@Override
32+
@Bean
33+
public KeyGenerator keyGenerator() {
34+
return (o, method, objects) -> {
35+
StringBuilder sb = new StringBuilder();
36+
sb.append(o.getClass().getName()).append(".");
37+
sb.append(method.getName()).append(".");
38+
for (Object obj : objects) {
39+
sb.append(obj.toString());
40+
}
41+
//System.out.println("keyGenerator=" + sb.toString());
42+
return sb.toString();
43+
};
44+
}
45+
46+
@Bean
47+
public RedisTemplate<Object, Object> redisTemplate() {
48+
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
49+
redisTemplate.setConnectionFactory(factory);
50+
51+
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
52+
53+
redisTemplate.setKeySerializer(genericJackson2JsonRedisSerializer);
54+
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
55+
56+
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
57+
redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
58+
return redisTemplate;
59+
}
60+
61+
@Bean
62+
@Override
63+
public CacheResolver cacheResolver() {
64+
return new SimpleCacheResolver(cacheManager());
65+
}
66+
67+
@Bean
68+
@Override
69+
public CacheErrorHandler errorHandler() {
70+
// 用于捕获从Cache中进行CRUD时的异常的回调处理器。
71+
return new SimpleCacheErrorHandler();
72+
}
73+
74+
@Bean
75+
@Override
76+
public CacheManager cacheManager() {
77+
RedisCacheConfiguration cacheConfiguration =
78+
defaultCacheConfig()
79+
.disableCachingNullValues()
80+
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
81+
return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();
82+
}
83+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.kimmking.cache.controller;
2+
3+
import io.kimmking.cache.entity.User;
4+
import io.kimmking.cache.service.UserService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import java.util.List;
11+
12+
@RestController
13+
@EnableAutoConfiguration
14+
public class UserController {
15+
16+
@Autowired
17+
UserService userService;
18+
19+
@RequestMapping("/user/find")
20+
User find(Integer id) {
21+
return userService.find(id);
22+
//return new User(1,"KK", 28);
23+
}
24+
25+
@RequestMapping("/user/list")
26+
List<User> list() {
27+
return userService.list();
28+
// return Arrays.asList(new User(1,"KK", 28),
29+
// new User(2,"CC", 18));
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.kimmking.cache.entity;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.io.Serializable;
8+
9+
@Data
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class User implements Serializable {
13+
private Integer id;
14+
private String name;
15+
private Integer age;
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.kimmking.cache.mapper;
2+
3+
import io.kimmking.cache.entity.User;
4+
import org.apache.ibatis.annotations.Mapper;
5+
6+
import java.util.List;
7+
8+
@Mapper
9+
public interface UserMapper {
10+
11+
User find(int id);
12+
13+
List<User> list();
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.kimmking.cache.service;
2+
3+
import io.kimmking.cache.entity.User;
4+
import org.springframework.cache.annotation.CacheConfig;
5+
6+
import java.util.List;
7+
8+
@CacheConfig(cacheNames = "users")
9+
public interface UserService {
10+
11+
User find(int id);
12+
13+
List<User> list();
14+
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.kimmking.cache.service;
2+
3+
import io.kimmking.cache.entity.User;
4+
import io.kimmking.cache.mapper.UserMapper;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cache.annotation.Cacheable;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.List;
10+
11+
@Service
12+
public class UserServiceImpl implements UserService {
13+
14+
@Autowired
15+
UserMapper userMapper;
16+
17+
// 开启spring cache
18+
@Cacheable //(key="#id",value="userCache")
19+
public User find(int id) {
20+
//System.out.println(" ==> find " + id);
21+
return userMapper.find(id);
22+
}
23+
24+
// 开启spring cache
25+
@Cacheable //(key="methodName",value="userCache")
26+
public List<User> list(){
27+
return userMapper.list();
28+
}
29+
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
server:
2+
port: 8080
3+
4+
spring:
5+
datasource:
6+
username: root
7+
password:
8+
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
9+
driver-class-name: com.mysql.jdbc.Driver
10+
# cache:
11+
# type: redis
12+
redis:
13+
host: localhost
14+
lettuce:
15+
pool:
16+
max-active: 32
17+
max-wait: 10ms
18+
19+
# type: ehcache
20+
# ehcache:
21+
# config: ehcache.xml
22+
23+
mybatis:
24+
mapper-locations: classpath:mapper/*Mapper.xml
25+
type-aliases-package: io.kimmking.cache.entity
26+
27+
logging:
28+
level:
29+
io:
30+
kimmking:
31+
cache : info
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
4+
<diskStore path="java.io.tmpdir" />
5+
<defaultCache eternal="false" maxElementsInMemory="1000"
6+
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
7+
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
8+
<cache
9+
name="userCache"
10+
maxElementsInMemory="1000"
11+
eternal="false"
12+
timeToIdleSeconds="300"
13+
timeToLiveSeconds="300"
14+
overflowToDisk="false"
15+
memoryStoreEvictionPolicy="LRU">
16+
<!-- 配置缓存事件监听器 replicateAsynchronously 操作是否异步,默认值为true. replicatePuts 添加操作是否同步到集群内的其他缓存,默认为true.
17+
replicateUpdates 更新操作是否同步到集群内的其他缓存,默认为true. replicateUpdatesViaCopy 更新之后的对象是否复制到集群中的其他缓存(true);
18+
replicateRemovals 删除操作是否同步到集群内的其他缓存,默认为true. -->
19+
<cacheEventListenerFactory
20+
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
21+
properties="
22+
replicateAsynchronously=true,
23+
replicatePuts=true,
24+
replicateUpdates=true,
25+
replicateUpdatesViaCopy=true,
26+
replicateRemovals=true " />
27+
28+
<!-- 初始化缓存,以及自动设置 -->
29+
<bootstrapCacheLoaderFactory
30+
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
31+
properties="bootstrapAsynchronously=true" />
32+
</cache>
33+
34+
</ehcache>

0 commit comments

Comments
 (0)
0