├── spring-boot-guava-cache ├── src │ ├── main │ │ ├── resources │ │ │ └── application.yml │ │ └── java │ │ │ └── com │ │ │ └── km │ │ │ ├── entity │ │ │ ├── Info.java │ │ │ └── User.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ │ ├── SpringBootGuavaCacheApplication.java │ │ │ ├── web │ │ │ └── IndexController.java │ │ │ └── config │ │ │ └── GuavaConfig.java │ └── test │ │ └── java │ │ └── com │ │ └── km │ │ └── SpringBootGuavaCacheApplicationTests.java └── pom.xml ├── spring-boot-ehcache ├── src │ ├── main │ │ ├── resources │ │ │ ├── application.yml │ │ │ └── ehcache.xml │ │ └── java │ │ │ └── com │ │ │ └── km │ │ │ ├── entity │ │ │ ├── Info.java │ │ │ └── User.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ │ ├── SpringBootEhcacheApplication.java │ │ │ └── web │ │ │ └── IndexController.java │ └── test │ │ └── java │ │ └── com │ │ └── km │ │ └── SpringBootEhcacheApplicationTests.java └── pom.xml ├── spring-boot2-redis-cache ├── src │ ├── main │ │ ├── resources │ │ │ └── application.yml │ │ └── java │ │ │ └── com │ │ │ └── zy │ │ │ ├── entity │ │ │ ├── Info.java │ │ │ └── User.java │ │ │ ├── SpringBoot2RedisCacheApplication.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ │ ├── controller │ │ │ └── IndexController.java │ │ │ └── config │ │ │ └── RedisConfig.java │ └── test │ │ └── java │ │ └── com │ │ └── zy │ │ └── SpringBoot2RedisCacheApplicationTests.java └── pom.xml ├── guava-cache ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── km │ │ │ │ ├── cache │ │ │ │ ├── Persistable.java │ │ │ │ ├── DefaultCacheContainer.java │ │ │ │ ├── CacheService.java │ │ │ │ ├── CacheContainer.java │ │ │ │ └── CacheOptions.java │ │ │ │ ├── entity │ │ │ │ └── User.java │ │ │ │ ├── service │ │ │ │ └── UserService.java │ │ │ │ └── db │ │ │ │ ├── DBPoolConnection.java │ │ │ │ ├── DBOperation.java │ │ │ │ └── DBUtil.java │ │ └── resources │ │ │ └── db_server.properties │ └── test │ │ └── java │ │ └── com │ │ └── km │ │ └── AppTest.java └── pom.xml ├── spring-boot-caffeine-cache ├── src │ ├── main │ │ ├── resources │ │ │ └── application.yml │ │ └── java │ │ │ └── com │ │ │ └── km │ │ │ ├── entity │ │ │ ├── Info.java │ │ │ └── User.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ │ ├── SpringBootCaffeineCacheApplication.java │ │ │ ├── web │ │ │ └── IndexController.java │ │ │ └── config │ │ │ └── CacheConfig.java │ └── test │ │ └── java │ │ └── com │ │ └── km │ │ └── SpringBootCaffeineCacheApplicationTests.java └── pom.xml ├── spring-boot-redis-cache ├── src │ ├── main │ │ ├── resources │ │ │ └── application.yml │ │ └── java │ │ │ └── com │ │ │ └── km │ │ │ ├── entity │ │ │ ├── Info.java │ │ │ └── User.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ │ ├── SpringBootRedisCacheApplication.java │ │ │ ├── config │ │ │ ├── RedisPrefix.java │ │ │ └── RedisConfig.java │ │ │ └── web │ │ │ └── IndexController.java │ └── test │ │ └── java │ │ └── com │ │ └── km │ │ ├── SpringBootGuavaCacheApplicationTests.java │ │ └── SpringBootRedisCacheApplicationTests.java └── pom.xml ├── .gitignore ├── spring-boot2-multi-redis-cache ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── zy │ │ │ │ ├── entity │ │ │ │ ├── Info.java │ │ │ │ └── User.java │ │ │ │ ├── SpringBoot2MultiRedisCacheApplication.java │ │ │ │ ├── service │ │ │ │ ├── UserService.java │ │ │ │ └── impl │ │ │ │ │ └── UserServiceImpl.java │ │ │ │ ├── controller │ │ │ │ └── IndexController.java │ │ │ │ └── config │ │ │ │ └── RedisConfig.java │ │ └── resources │ │ │ └── application.yml │ └── test │ │ └── java │ │ └── com │ │ └── zy │ │ └── SpringBoot2MultiRedisCacheApplicationTests.java └── pom.xml ├── README.md ├── pom.xml └── LICENSE /spring-boot-guava-cache/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | cache: 5 | type: guava 6 | -------------------------------------------------------------------------------- /spring-boot-ehcache/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | cache: 3 | type: ehcache 4 | ehcache: 5 | config: classpath:ehcache.xml -------------------------------------------------------------------------------- /spring-boot2-redis-cache/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-boot2-redis-cache 4 | redis: 5 | host: 192.168.97.147 6 | port: 6379 7 | database: 10 8 | timeout: 1s -------------------------------------------------------------------------------- /guava-cache/src/main/java/com/km/cache/Persistable.java: -------------------------------------------------------------------------------- 1 | package com.km.cache; 2 | 3 | /** 4 | *

可持久化的

5 | * Created by zhezhiyong@163.com on 2017/9/25. 6 | */ 7 | public interface Persistable { 8 | V load(K k) throws Exception; 9 | } 10 | -------------------------------------------------------------------------------- /spring-boot-caffeine-cache/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | cache: 5 | caffeine: 6 | spec: maximumSize=500,expireAfterWrite=5s 7 | type: caffeine 8 | cache-names: 9 | - user 10 | - info -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | spring: 4 | cache: 5 | type: redis 6 | redis: 7 | host: 192.168.97.57 # server host 8 | port: 6379 # connection port 9 | pool.max-idle: 8 # pool settings ... 10 | pool.min-idle: 1 11 | pool.max-active: 8 12 | pool.max-wait: -1 13 | database: 0 14 | -------------------------------------------------------------------------------- /spring-boot-caffeine-cache/src/main/java/com/km/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.km.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | *

8 | * Created by zhezhiyong@163.com on 2017/9/22. 9 | */ 10 | @Data 11 | @AllArgsConstructor 12 | public class Info { 13 | private String phone; 14 | private String address; 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-guava-cache/src/main/java/com/km/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.km.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | *

8 | * Created by zhezhiyong@163.com on 2017/9/22. 9 | */ 10 | @Data 11 | @AllArgsConstructor 12 | public class Info { 13 | private String phone; 14 | private String address; 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/main/java/com/km/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.km.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | *

8 | * Created by zhezhiyong@163.com on 2017/9/22. 9 | */ 10 | @Data 11 | @AllArgsConstructor 12 | public class Info { 13 | private String phone; 14 | private String address; 15 | 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # maven ignore 2 | target/ 3 | *.jar 4 | *.war 5 | *.zip 6 | *.tar 7 | *.tar.gz 8 | 9 | # eclipse ignore 10 | .settings/ 11 | .project 12 | .classpath 13 | 14 | # idea ignore 15 | .idea/ 16 | *.ipr 17 | *.iml 18 | *.iws 19 | 20 | # temp ignore 21 | *.log 22 | *.cache 23 | *.diff 24 | *.patch 25 | *.tmp 26 | 27 | # system ignore 28 | .DS_Store 29 | Thumbs.db 30 | 31 | -------------------------------------------------------------------------------- /spring-boot-ehcache/src/main/java/com/km/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.km.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | *

10 | * Created by zhezhiyong@163.com on 2017/9/22. 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | public class Info implements Serializable { 15 | private String phone; 16 | private String address; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /spring-boot2-redis-cache/src/main/java/com/zy/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.zy.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | *

10 | * Created by zhezhiyong@163.com on 2017/9/22. 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | public class Info implements Serializable { 15 | private String phone; 16 | private String address; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /spring-boot-ehcache/src/main/java/com/km/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.km.service; 2 | 3 | import com.km.entity.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | *

9 | * Created by zhezhiyong@163.com on 2017/9/21. 10 | */ 11 | public interface UserService { 12 | 13 | List list(); 14 | 15 | User findUserById(Long id); 16 | 17 | void update(User user); 18 | 19 | void remove(Long id); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-boot2-multi-redis-cache/src/main/java/com/zy/entity/Info.java: -------------------------------------------------------------------------------- 1 | package com.zy.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | *

10 | * Created by zhezhiyong@163.com on 2017/9/22. 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | public class Info implements Serializable { 15 | private String phone; 16 | private String address; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /guava-cache/src/main/java/com/km/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.km.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | *

9 | * Created by zhezhiyong@163.com on 2017/9/25. 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | public class User { 15 | 16 | private Long id; 17 | private String name; 18 | private String password; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-boot-caffeine-cache/src/main/java/com/km/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.km.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | *

9 | * Created by zhezhiyong@163.com on 2017/9/21. 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | public class User { 15 | 16 | private Long id; 17 | private String name; 18 | private String password; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-boot-guava-cache/src/main/java/com/km/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.km.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | *

9 | * Created by zhezhiyong@163.com on 2017/9/21. 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | public class User { 15 | 16 | private Long id; 17 | private String name; 18 | private String password; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/main/java/com/km/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.km.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | /** 8 | *

9 | * Created by zhezhiyong@163.com on 2017/9/21. 10 | */ 11 | @Data 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | public class User { 15 | 16 | private Long id; 17 | private String name; 18 | private String password; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /spring-boot2-redis-cache/src/main/java/com/zy/SpringBoot2RedisCacheApplication.java: -------------------------------------------------------------------------------- 1 | package com.zy; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBoot2RedisCacheApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBoot2RedisCacheApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-ehcache/src/test/java/com/km/SpringBootEhcacheApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.km; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootEhcacheApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-guava-cache/src/main/java/com/km/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.km.service; 2 | 3 | import com.km.entity.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | *

9 | * Created by zhezhiyong@163.com on 2017/9/21. 10 | */ 11 | public interface UserService { 12 | 13 | List list(); 14 | 15 | User findUserById(Long id); 16 | 17 | User findInfoById(Long id); 18 | 19 | User update(User user); 20 | 21 | void remove(Long id); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/main/java/com/km/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.km.service; 2 | 3 | import com.km.entity.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | *

9 | * Created by zhezhiyong@163.com on 2017/9/21. 10 | */ 11 | public interface UserService { 12 | 13 | List list(); 14 | 15 | User findUserById(Long id); 16 | 17 | User findInfoById(Long id); 18 | 19 | User update(User user); 20 | 21 | void remove(Long id); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /guava-cache/src/main/resources/db_server.properties: -------------------------------------------------------------------------------- 1 | driverClassName=com.mysql.jdbc.Driver 2 | url=jdbc:mysql://127.0.0.1:3306/test 3 | username=root 4 | password=666666 5 | filters=stat 6 | initialSize=2 7 | maxActive=300 8 | maxWait=60000 9 | timeBetweenEvictionRunsMillis=60000 10 | minEvictableIdleTimeMillis=300000 11 | validationQuery=SELECT 1 12 | testWhileIdle=true 13 | testOnBorrow=false 14 | testOnReturn=false 15 | poolPreparedStatements=false 16 | maxPoolPreparedStatementPerConnectionSize=200 -------------------------------------------------------------------------------- /spring-boot-caffeine-cache/src/main/java/com/km/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.km.service; 2 | 3 | import com.km.entity.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | *

9 | * Created by zhezhiyong@163.com on 2017/9/21. 10 | */ 11 | public interface UserService { 12 | 13 | List list(); 14 | 15 | User findUserById(Long id); 16 | 17 | User findInfoById(Long id); 18 | 19 | User update(User user); 20 | 21 | void remove(Long id); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-guava-cache/src/test/java/com/km/SpringBootGuavaCacheApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.km; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootGuavaCacheApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/test/java/com/km/SpringBootGuavaCacheApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.km; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootGuavaCacheApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/test/java/com/km/SpringBootRedisCacheApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.km; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootRedisCacheApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot2-multi-redis-cache/src/main/java/com/zy/SpringBoot2MultiRedisCacheApplication.java: -------------------------------------------------------------------------------- 1 | package com.zy; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBoot2MultiRedisCacheApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBoot2MultiRedisCacheApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot2-multi-redis-cache/src/main/java/com/zy/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.zy.service; 2 | 3 | import com.zy.entity.User; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | *

9 | * Created by zhezhiyong@163.com on 2017/9/21. 10 | */ 11 | public interface UserService { 12 | 13 | List list(); 14 | 15 | User findUserById(Long id); 16 | 17 | User findInfoById(Long id); 18 | 19 | void update(User user); 20 | 21 | void remove(Long id); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-caffeine-cache/src/test/java/com/km/SpringBootCaffeineCacheApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.km; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringBootCaffeineCacheApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-ehcache/src/main/java/com/km/SpringBootEhcacheApplication.java: -------------------------------------------------------------------------------- 1 | package com.km; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | 7 | @SpringBootApplication 8 | @EnableCaching 9 | public class SpringBootEhcacheApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootEhcacheApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-boot-ehcache/src/main/java/com/km/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.km.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | *

11 | * Created by zhezhiyong@163.com on 2017/9/21. 12 | */ 13 | @Data 14 | @AllArgsConstructor 15 | @NoArgsConstructor 16 | public class User implements Serializable{ 17 | 18 | private Long id; 19 | private String name; 20 | private String password; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot2-redis-cache/src/main/java/com/zy/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.zy.service; 2 | 3 | import com.zy.entity.Info; 4 | import com.zy.entity.User; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | *

10 | * Created by zhezhiyong@163.com on 2017/9/21. 11 | */ 12 | public interface UserService { 13 | 14 | List list(); 15 | 16 | User findUserById(Long id); 17 | 18 | Info findInfoById(Long id); 19 | 20 | User update(User user); 21 | 22 | void remove(Long id); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /spring-boot2-redis-cache/src/main/java/com/zy/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.zy.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | *

11 | * Created by zhezhiyong@163.com on 2017/9/21. 12 | */ 13 | @Data 14 | @AllArgsConstructor 15 | @NoArgsConstructor 16 | public class User implements Serializable { 17 | 18 | private Long id; 19 | private String name; 20 | private String password; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot2-multi-redis-cache/src/main/java/com/zy/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.zy.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | *

11 | * Created by zhezhiyong@163.com on 2017/9/21. 12 | */ 13 | @Data 14 | @AllArgsConstructor 15 | @NoArgsConstructor 16 | public class User implements Serializable { 17 | 18 | private Long id; 19 | private String name; 20 | private String password; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-guava-cache/src/main/java/com/km/SpringBootGuavaCacheApplication.java: -------------------------------------------------------------------------------- 1 | package com.km; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | 7 | @SpringBootApplication 8 | @EnableCaching 9 | public class SpringBootGuavaCacheApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootGuavaCacheApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/main/java/com/km/SpringBootRedisCacheApplication.java: -------------------------------------------------------------------------------- 1 | package com.km; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | 7 | @SpringBootApplication 8 | @EnableCaching 9 | public class SpringBootRedisCacheApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootRedisCacheApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-boot-caffeine-cache/src/main/java/com/km/SpringBootCaffeineCacheApplication.java: -------------------------------------------------------------------------------- 1 | package com.km; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | 7 | @SpringBootApplication 8 | @EnableCaching 9 | public class SpringBootCaffeineCacheApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootCaffeineCacheApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## springboot使用redis做数据缓存 2 | 3 | * [spring boot caffeine cache 缓存学习](http://blog.csdn.net/hy245120020/article/details/78065698) 4 | * [spring boot guava cache 缓存学习](http://blog.csdn.net/hy245120020/article/details/78065676) 5 | * [spring boot redis cache 缓存学习](http://blog.csdn.net/hy245120020/article/details/78065654) 6 | * [guava和caffeine性能测试](http://blog.csdn.net/hy245120020/article/details/78080686) 7 | * [springboot2.x版本redis缓存学习](https://blog.csdn.net/hy245120020/article/details/89028833) 8 | * [springboot2.x版本多个redis实例配置学习](https://blog.csdn.net/hy245120020/article/details/89029111) 9 | -------------------------------------------------------------------------------- /guava-cache/src/main/java/com/km/cache/DefaultCacheContainer.java: -------------------------------------------------------------------------------- 1 | package com.km.cache; 2 | 3 | /** 4 | *

默认实现

5 | * Created by zhezhiyong@163.com on 2017/9/25. 6 | */ 7 | public class DefaultCacheContainer extends CacheContainer { 8 | 9 | private Persistable persistable; 10 | 11 | public DefaultCacheContainer(Persistable persistable, CacheOptions p) { 12 | super(p); 13 | this.persistable = persistable; 14 | } 15 | 16 | @Override 17 | public V loadOnce(K k) throws Exception { 18 | return persistable.load(k); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /spring-boot2-multi-redis-cache/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-boot2-multi-redis-cache 4 | redis: 5 | timeToLive: 15 6 | lettuce: 7 | pool: 8 | MaxTotal: 50 9 | minIdle: 1 10 | maxWaitMillis: 5000 11 | maxIdle: 5 12 | testOnBorrow: true 13 | testOnReturn: true 14 | testWhileIdle: true 15 | redis-a: 16 | database: 7 17 | hostName: 192.168.96.24 18 | port: 16379 19 | timeout: 5000 20 | redis-b: 21 | database: 3 22 | hostName: ${spring.redis.redis-a.hostName} 23 | port: ${spring.redis.redis-a.port} 24 | timeout: ${spring.redis.redis-a.timeout} 25 | 26 | -------------------------------------------------------------------------------- /spring-boot2-redis-cache/src/test/java/com/zy/SpringBoot2RedisCacheApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zy; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.data.redis.core.RedisTemplate; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | 9 | import javax.annotation.Resource; 10 | 11 | @RunWith(SpringRunner.class) 12 | @SpringBootTest 13 | public class SpringBoot2RedisCacheApplicationTests { 14 | 15 | @Resource(name = "redisTemplateB") 16 | private RedisTemplate redisTemplateB; 17 | 18 | @Resource(name = "redisTemplateA") 19 | private RedisTemplate redisTemplate; 20 | 21 | @Test 22 | public void contextLoads() { 23 | redisTemplate.opsForValue().set("redisTemplateA", "A"); 24 | redisTemplateB.opsForValue().set("redisTemplateB", "B"); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot2-multi-redis-cache/src/test/java/com/zy/SpringBoot2MultiRedisCacheApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.zy; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.data.redis.core.RedisTemplate; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | 9 | import javax.annotation.Resource; 10 | 11 | @RunWith(SpringRunner.class) 12 | @SpringBootTest 13 | public class SpringBoot2MultiRedisCacheApplicationTests { 14 | 15 | @Resource(name = "redisTemplateB") 16 | private RedisTemplate redisTemplateB; 17 | 18 | @Resource(name = "redisTemplateA") 19 | private RedisTemplate redisTemplate; 20 | 21 | @Test 22 | public void contextLoads() { 23 | redisTemplate.opsForValue().set("redisTemplateA", "A"); 24 | redisTemplateB.opsForValue().set("redisTemplateB", "B"); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /guava-cache/src/main/java/com/km/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.km.service; 2 | 3 | import com.km.cache.CacheService; 4 | import com.km.db.DBUtil; 5 | import com.km.entity.User; 6 | 7 | import java.sql.ResultSet; 8 | 9 | /** 10 | *

11 | * Created by zhezhiyong@163.com on 2017/9/25. 12 | */ 13 | public class UserService extends CacheService { 14 | 15 | public static UserService INSTANCE = new UserService(); 16 | 17 | @Override 18 | public User load(Long id) throws Exception { 19 | System.out.println("query db"); 20 | User user = null; 21 | DBUtil dbUtil = new DBUtil(); 22 | ResultSet rs = dbUtil.select("user", "id, username, password", "where id = 1"); 23 | while (rs.next()) { 24 | String username = rs.getString("username"); 25 | String password = rs.getString("password"); 26 | user = new User(id, username, password); 27 | } 28 | return user; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/main/java/com/km/config/RedisPrefix.java: -------------------------------------------------------------------------------- 1 | package com.km.config; 2 | 3 | import org.springframework.data.redis.cache.RedisCachePrefix; 4 | import org.springframework.data.redis.serializer.RedisSerializer; 5 | import org.springframework.data.redis.serializer.StringRedisSerializer; 6 | 7 | /** 8 | *

redis前缀配置,有时候多个工程共用一个db需要区分

9 | * Created by zhezhiyong@163.com on 2017/9/22. 10 | */ 11 | public class RedisPrefix implements RedisCachePrefix { 12 | private final RedisSerializer serializer; 13 | private final String delimiter; 14 | 15 | public RedisPrefix() { 16 | this(":"); 17 | } 18 | 19 | public RedisPrefix(String delimiter) { 20 | this.serializer = new StringRedisSerializer(); 21 | this.delimiter = delimiter; 22 | } 23 | 24 | @Override 25 | public byte[] prefix(String cacheName) { 26 | return this.serializer.serialize(this.delimiter != null ? this.delimiter.concat(":").concat(cacheName).concat(":") : cacheName.concat(":")); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /guava-cache/src/main/java/com/km/cache/CacheService.java: -------------------------------------------------------------------------------- 1 | package com.km.cache; 2 | 3 | /** 4 | *

抽象缓存服务

5 | * Created by zhezhiyong@163.com on 2017/9/25. 6 | */ 7 | public abstract class CacheService implements Persistable { 8 | 9 | 10 | private final CacheContainer container; 11 | 12 | public CacheService() { 13 | this(CacheOptions.defaultCacheOptions()); 14 | } 15 | 16 | public CacheService(CacheOptions p) { 17 | container = new DefaultCacheContainer<>(this, p); 18 | } 19 | 20 | /** 21 | * 通过key获取对象 22 | * 23 | * @param key 24 | * @return 25 | */ 26 | public V get(K key) { 27 | return container.get(key); 28 | } 29 | 30 | /** 31 | * 手动移除缓存 32 | * 33 | * @param key 34 | * @return 35 | */ 36 | public void remove(K key) { 37 | container.remove(key); 38 | } 39 | 40 | /** 41 | * 手动加入缓存 42 | * 43 | * @param key 44 | * @return 45 | */ 46 | public void put(K key, V v) { 47 | this.container.put(key, v); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.km 7 | spring-boot-redis-guava-caffeine-cache 8 | 0.0.1-SNAPSHOT 9 | pom 10 | 11 | spring-boot-redis-guava-caffeine-cache 12 | Demo project for Spring Boot 13 | 14 | 15 | spring-boot-redis-cache 16 | spring-boot2-multi-redis-cache 17 | spring-boot2-redis-cache 18 | spring-boot-guava-cache 19 | spring-boot-caffeine-cache 20 | spring-boot-ehcache 21 | guava-cache 22 | 23 | 24 | 25 | UTF-8 26 | UTF-8 27 | 1.8 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /guava-cache/src/test/java/com/km/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.km; 2 | 3 | import com.km.db.DBUtil; 4 | import com.km.entity.User; 5 | import com.km.service.UserService; 6 | import org.junit.Test; 7 | 8 | import java.sql.ResultSet; 9 | import java.sql.SQLException; 10 | 11 | /** 12 | * Unit test for simple App. 13 | */ 14 | public class AppTest { 15 | 16 | /** 17 | * 测试缓存是否生效 18 | */ 19 | @Test 20 | public void test() { 21 | UserService userService = UserService.INSTANCE; 22 | User user = userService.get(1L); 23 | System.out.println(user.getName()); 24 | user.setName("haha"); 25 | System.out.println(user.getName()); 26 | User user1 = userService.get(1L); 27 | System.out.println(user1.getName()); 28 | // 删除缓存中数据 29 | userService.remove(1L); 30 | User user2 = userService.get(1L); 31 | System.out.println(user2.getName()); 32 | } 33 | 34 | @Test 35 | public void testDb() throws SQLException { 36 | User user = null; 37 | DBUtil dbUtil = new DBUtil(); 38 | ResultSet rs = dbUtil.select("user", "id, username, password", "where id = 1"); 39 | while (rs.next()) { 40 | long id = rs.getLong("id"); 41 | String username = rs.getString("username"); 42 | String password = rs.getString("password"); 43 | user = new User(id, username, password); 44 | } 45 | System.out.println(user.toString()); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /spring-boot-ehcache/src/main/java/com/km/web/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.km.web; 2 | 3 | import com.google.common.collect.ImmutableMap; 4 | import com.km.entity.User; 5 | import com.km.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | /** 16 | *

17 | * Created by zhezhiyong@163.com on 2017/9/21. 18 | */ 19 | @RestController 20 | public class IndexController { 21 | 22 | @Autowired 23 | private UserService userService; 24 | 25 | @GetMapping("/users") 26 | @ResponseBody 27 | public List users() { 28 | return userService.list(); 29 | } 30 | 31 | @GetMapping("/findUser/{id}") 32 | @ResponseBody 33 | public User findUserById(@PathVariable("id") Long id) { 34 | return userService.findUserById(id); 35 | } 36 | 37 | @GetMapping("/updateUser/{id}") 38 | @ResponseBody 39 | public User updateUserById(@PathVariable("id") Long id) { 40 | User user = userService.findUserById(id); 41 | user.setName("updateUserById"); 42 | userService.update(user); 43 | return user; 44 | } 45 | 46 | @GetMapping("/deleteUser/{id}") 47 | @ResponseBody 48 | public Map deleteUser(@PathVariable("id") Long id) { 49 | userService.remove(id); 50 | return ImmutableMap.of("ret", 0, "msg", "ok"); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /spring-boot-guava-cache/src/main/java/com/km/web/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.km.web; 2 | 3 | import com.google.common.collect.ImmutableMap; 4 | import com.km.entity.User; 5 | import com.km.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | /** 16 | *

17 | * Created by zhezhiyong@163.com on 2017/9/21. 18 | */ 19 | @RestController 20 | public class IndexController { 21 | 22 | @Autowired 23 | private UserService userService; 24 | 25 | @GetMapping("/users") 26 | @ResponseBody 27 | public List users() { 28 | return userService.list(); 29 | } 30 | 31 | @GetMapping("/user/{id}") 32 | @ResponseBody 33 | public User findUserById(@PathVariable("id") Long id) { 34 | return userService.findUserById(id); 35 | } 36 | 37 | @GetMapping("/info/{id}") 38 | @ResponseBody 39 | public User findInfoById(@PathVariable("id") Long id) { 40 | return userService.findInfoById(id); 41 | } 42 | 43 | @GetMapping("/user/{id}/{name}") 44 | @ResponseBody 45 | public Map update(@PathVariable("id") Long id, @PathVariable("name") String name) { 46 | User user = userService.findUserById(id); 47 | user.setName(name); 48 | userService.update(user); 49 | return ImmutableMap.of("ret", 0, "msg", "ok"); 50 | } 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/main/java/com/km/web/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.km.web; 2 | 3 | import com.google.common.collect.ImmutableMap; 4 | import com.km.entity.User; 5 | import com.km.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | /** 16 | *

17 | * Created by zhezhiyong@163.com on 2017/9/21. 18 | */ 19 | @RestController 20 | public class IndexController { 21 | 22 | @Autowired 23 | private UserService userService; 24 | 25 | @GetMapping("/users") 26 | @ResponseBody 27 | public List users() { 28 | return userService.list(); 29 | } 30 | 31 | @GetMapping("/user/{id}") 32 | @ResponseBody 33 | public User findUserById(@PathVariable("id") Long id) { 34 | return userService.findUserById(id); 35 | } 36 | 37 | @GetMapping("/info/{id}") 38 | @ResponseBody 39 | public User findInfoById(@PathVariable("id") Long id) { 40 | return userService.findInfoById(id); 41 | } 42 | 43 | @GetMapping("/user/{id}/{name}") 44 | @ResponseBody 45 | public Map update(@PathVariable("id") Long id, @PathVariable("name") String name) { 46 | User user = userService.findUserById(id); 47 | user.setName(name); 48 | userService.update(user); 49 | return ImmutableMap.of("ret", 0, "msg", "ok"); 50 | } 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /spring-boot-caffeine-cache/src/main/java/com/km/web/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.km.web; 2 | 3 | import com.km.entity.User; 4 | import com.km.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import java.util.HashMap; 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | /** 16 | *

17 | * Created by zhezhiyong@163.com on 2017/9/21. 18 | */ 19 | @RestController 20 | public class IndexController { 21 | 22 | @Autowired 23 | private UserService userService; 24 | 25 | @GetMapping("/users") 26 | @ResponseBody 27 | public List users() { 28 | return userService.list(); 29 | } 30 | 31 | @GetMapping("/user/{id}") 32 | @ResponseBody 33 | public User findUserById(@PathVariable("id") Long id) { 34 | return userService.findUserById(id); 35 | } 36 | 37 | @GetMapping("/info/{id}") 38 | @ResponseBody 39 | public User findInfoById(@PathVariable("id") Long id) { 40 | return userService.findInfoById(id); 41 | } 42 | 43 | @GetMapping("/user/{id}/{name}") 44 | @ResponseBody 45 | public Map update(@PathVariable("id") Long id, @PathVariable("name") String name) { 46 | User user = userService.findUserById(id); 47 | user.setName(name); 48 | userService.update(user); 49 | Map result = new HashMap<>(); 50 | result.put("ret", 0); 51 | result.put("msg", "ok"); 52 | return result; 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /spring-boot-ehcache/src/main/java/com/km/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.km.service.impl; 2 | 3 | import com.km.entity.User; 4 | import com.km.service.UserService; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.cache.annotation.CacheConfig; 7 | import org.springframework.cache.annotation.CacheEvict; 8 | import org.springframework.cache.annotation.CachePut; 9 | import org.springframework.cache.annotation.Cacheable; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.Arrays; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | /** 18 | *

19 | * Created by zhezhiyong@163.com on 2017/9/21. 20 | */ 21 | @Service 22 | @Slf4j 23 | @CacheConfig(cacheNames = "user") 24 | public class UserServiceImpl implements UserService { 25 | 26 | private Map userMap = new HashMap<>(); 27 | 28 | public UserServiceImpl() { 29 | userMap.put(1L, new User(1L, "aaa", "666666")); 30 | userMap.put(2L, new User(2L, "bbb", "666666")); 31 | userMap.put(3L, new User(3L, "ccc", "666666")); 32 | } 33 | 34 | @Override 35 | public List list() { 36 | return Arrays.asList(userMap.values().toArray()); 37 | } 38 | 39 | @Override 40 | @Cacheable(key = "#id") 41 | public User findUserById(Long id) { 42 | log.info("findUserById query from db, id: {}", id); 43 | return userMap.get(id); 44 | } 45 | 46 | @Override 47 | @CachePut(key = "#user.id") 48 | public void update(User user) { 49 | log.info("update db, user: {}", user.toString()); 50 | userMap.put(user.getId(), user); 51 | } 52 | 53 | @Override 54 | @CacheEvict(key = "#id") 55 | public void remove(Long id) { 56 | log.info("remove from db, id: {}", id); 57 | // userMap.remove(id); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /spring-boot2-redis-cache/src/main/java/com/zy/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.zy.controller; 2 | 3 | import com.google.common.collect.ImmutableMap; 4 | import com.zy.entity.Info; 5 | import com.zy.entity.User; 6 | import com.zy.service.UserService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.ResponseBody; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | import java.util.List; 14 | import java.util.Map; 15 | 16 | /** 17 | *

18 | * 参考:https://blog.csdn.net/chenypgg/article/details/85698209 19 | *

20 | * Created by zhezhiyong@163.com on 2017/9/21. 21 | */ 22 | @RestController 23 | public class IndexController { 24 | 25 | @Autowired 26 | private UserService userService; 27 | 28 | @GetMapping("/users") 29 | @ResponseBody 30 | public List users() { 31 | return userService.list(); 32 | } 33 | 34 | @GetMapping("/user/{id}") 35 | @ResponseBody 36 | public User findUserById(@PathVariable("id") Long id) { 37 | return userService.findUserById(id); 38 | } 39 | 40 | @GetMapping("/info/{id}") 41 | @ResponseBody 42 | public Info findInfoById(@PathVariable("id") Long id) { 43 | return userService.findInfoById(id); 44 | } 45 | 46 | @GetMapping("/user/{id}/{name}") 47 | @ResponseBody 48 | public Map update(@PathVariable("id") Long id, @PathVariable("name") String name) { 49 | User user = userService.findUserById(id); 50 | user.setName(name); 51 | User user1 = userService.update(user); 52 | return ImmutableMap.of("ret", 0, "msg", "ok", "data", user1); 53 | } 54 | 55 | @GetMapping("/deleteUser/{id}") 56 | @ResponseBody 57 | public Map deleteUser(@PathVariable("id") Long id) { 58 | userService.remove(id); 59 | return ImmutableMap.of("ret", 0, "msg", "ok"); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /guava-cache/src/main/java/com/km/cache/CacheContainer.java: -------------------------------------------------------------------------------- 1 | package com.km.cache; 2 | 3 | import com.google.common.cache.*; 4 | import com.google.common.util.concurrent.UncheckedExecutionException; 5 | 6 | import java.util.concurrent.ConcurrentMap; 7 | import java.util.concurrent.ExecutionException; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | /** 11 | *

缓存容器

12 | * Created by zhezhiyong@163.com on 2017/9/25. 13 | */ 14 | public abstract class CacheContainer { 15 | 16 | private LoadingCache cache; 17 | 18 | public CacheContainer(CacheOptions p) { 19 | cache = CacheBuilder.newBuilder() 20 | .initialCapacity(p.initialCapacity) 21 | .maximumSize(p.maximumSize) 22 | //超时自动删除 23 | .expireAfterAccess(p.expireAfterAccessSeconds, TimeUnit.SECONDS) 24 | .expireAfterWrite(p.expireAfterWriteSeconds, TimeUnit.SECONDS) 25 | .removalListener(new MyRemovalListener()) 26 | .build(new DataLoader()); 27 | } 28 | 29 | public final V get(K k) { 30 | try { 31 | return cache.get(k); 32 | } catch (ExecutionException e) { 33 | System.out.println("CacheContainer get error:" + e.getMessage()); 34 | throw new UncheckedExecutionException(e); 35 | } 36 | } 37 | 38 | public abstract V loadOnce(K k) throws Exception; 39 | 40 | public final void put(K k, V v) { 41 | cache.put(k, v); 42 | } 43 | 44 | public final void remove(K k) { 45 | cache.invalidate(k); 46 | } 47 | 48 | public final ConcurrentMap asMap() { 49 | return cache.asMap(); 50 | } 51 | 52 | class DataLoader extends CacheLoader { 53 | @Override 54 | public V load(K key) throws Exception { 55 | return loadOnce(key); 56 | } 57 | } 58 | 59 | class MyRemovalListener implements RemovalListener { 60 | @Override 61 | public void onRemoval(RemovalNotification notification) { 62 | System.out.println("onRemoval"); 63 | //logger 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /spring-boot-ehcache/src/main/resources/ehcache.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 37 | 38 | 39 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /spring-boot-ehcache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.km 7 | spring-boot-ehcache 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-boot-ehcache 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.7.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | org.projectlombok 34 | lombok 35 | true 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-cache 46 | 47 | 48 | net.sf.ehcache 49 | ehcache 50 | 51 | 52 | com.google.guava 53 | guava 54 | 22.0 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-maven-plugin 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /spring-boot2-multi-redis-cache/src/main/java/com/zy/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.zy.service.impl; 2 | 3 | import com.zy.entity.Info; 4 | import com.zy.entity.User; 5 | import com.zy.service.UserService; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.cache.annotation.CacheEvict; 8 | import org.springframework.cache.annotation.CachePut; 9 | import org.springframework.cache.annotation.Cacheable; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.Arrays; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | /** 18 | *

19 | * Created by zhezhiyong@163.com on 2017/9/21. 20 | */ 21 | @Service 22 | @Slf4j 23 | public class UserServiceImpl implements UserService { 24 | 25 | private Map userMap = new HashMap<>(); 26 | private Map infoMap = new HashMap<>(); 27 | 28 | public UserServiceImpl() { 29 | userMap.put(1L, new User(1L, "aaa", "666666")); 30 | userMap.put(2L, new User(2L, "bbb", "666666")); 31 | userMap.put(3L, new User(3L, "ccc", "666666")); 32 | infoMap.put(1L, new Info("18559198715", "福州市")); 33 | } 34 | 35 | @Override 36 | public List list() { 37 | return Arrays.asList(userMap.values().toArray()); 38 | } 39 | 40 | @Override 41 | @Cacheable(value = "user", key = "'user'.concat(#id.toString())") 42 | public User findUserById(Long id) { 43 | log.info("findUserById query from db, id: {}", id); 44 | return userMap.get(id); 45 | } 46 | 47 | @Override 48 | @Cacheable(value = "info", key = "'info'.concat(#id.toString())") 49 | public User findInfoById(Long id) { 50 | log.info("findInfoById query from db, id: {}", id); 51 | return userMap.get(id); 52 | } 53 | 54 | @Override 55 | @CachePut(value = "user", key = "'user'.concat(#user.id.toString())") 56 | public void update(User user) { 57 | log.info("update db, user: {}", user.toString()); 58 | userMap.put(user.getId(), user); 59 | } 60 | 61 | @Override 62 | @CacheEvict(value = "user", key = "'user'.concat(#id.toString())") 63 | public void remove(Long id) { 64 | log.info("remove from db, id: {}", id); 65 | userMap.remove(id); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /spring-boot-guava-cache/src/main/java/com/km/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.km.service.impl; 2 | 3 | import com.km.entity.Info; 4 | import com.km.entity.User; 5 | import com.km.service.UserService; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.cache.annotation.CacheEvict; 8 | import org.springframework.cache.annotation.CachePut; 9 | import org.springframework.cache.annotation.Cacheable; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.Arrays; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | /** 18 | *

19 | * Created by zhezhiyong@163.com on 2017/9/21. 20 | */ 21 | @Service 22 | @Slf4j 23 | public class UserServiceImpl implements UserService { 24 | 25 | private Map userMap = new HashMap<>(); 26 | private Map infoMap = new HashMap<>(); 27 | 28 | public UserServiceImpl() { 29 | userMap.put(1L, new User(1L, "aaa", "666666")); 30 | userMap.put(2L, new User(2L, "bbb", "666666")); 31 | userMap.put(3L, new User(3L, "ccc", "666666")); 32 | infoMap.put(1L, new Info("18559198715", "福州市")); 33 | } 34 | 35 | @Override 36 | public List list() { 37 | return Arrays.asList(userMap.values().toArray()); 38 | } 39 | 40 | @Override 41 | @Cacheable(value = "user", key = "'user'.concat(#id.toString())") 42 | public User findUserById(Long id) { 43 | log.info("findUserById query from db, id: {}", id); 44 | return userMap.get(id); 45 | } 46 | 47 | @Override 48 | @Cacheable(value = "info", key = "'info'.concat(#id.toString())") 49 | public User findInfoById(Long id) { 50 | log.info("findInfoById query from db, id: {}", id); 51 | return userMap.get(id); 52 | } 53 | 54 | @Override 55 | @CachePut(value = "user", key = "'user'.concat(#user.id.toString())") 56 | public User update(User user) { 57 | log.info("update db, user: {}", user.toString()); 58 | userMap.put(user.getId(), user); 59 | return user; 60 | } 61 | 62 | @Override 63 | @CacheEvict(value = "user", key = "'user'.concat(#id.toString())") 64 | public void remove(Long id) { 65 | log.info("remove from db, id: {}", id); 66 | userMap.remove(id); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/main/java/com/km/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.km.service.impl; 2 | 3 | import com.km.entity.Info; 4 | import com.km.entity.User; 5 | import com.km.service.UserService; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.cache.annotation.CacheEvict; 8 | import org.springframework.cache.annotation.CachePut; 9 | import org.springframework.cache.annotation.Cacheable; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.Arrays; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | /** 18 | *

19 | * Created by zhezhiyong@163.com on 2017/9/21. 20 | */ 21 | @Service 22 | @Slf4j 23 | public class UserServiceImpl implements UserService { 24 | 25 | private Map userMap = new HashMap<>(); 26 | private Map infoMap = new HashMap<>(); 27 | 28 | public UserServiceImpl() { 29 | userMap.put(1L, new User(1L, "aaa", "666666")); 30 | userMap.put(2L, new User(2L, "bbb", "666666")); 31 | userMap.put(3L, new User(3L, "ccc", "666666")); 32 | infoMap.put(1L, new Info("18559198715", "福州市")); 33 | } 34 | 35 | @Override 36 | public List list() { 37 | return Arrays.asList(userMap.values().toArray()); 38 | } 39 | 40 | @Override 41 | @Cacheable(value = "user", key = "'user'.concat(#id.toString())") 42 | public User findUserById(Long id) { 43 | log.info("findUserById query from db, id: {}", id); 44 | return userMap.get(id); 45 | } 46 | 47 | @Override 48 | @Cacheable(value = "info", key = "'info'.concat(#id.toString())") 49 | public User findInfoById(Long id) { 50 | log.info("findInfoById query from db, id: {}", id); 51 | return userMap.get(id); 52 | } 53 | 54 | @Override 55 | @CachePut(value = "user", key = "'user'.concat(#user.id.toString())") 56 | public User update(User user) { 57 | log.info("update db, user: {}", user.toString()); 58 | userMap.put(user.getId(), user); 59 | return user; 60 | } 61 | 62 | @Override 63 | @CacheEvict(value = "user", key = "'user'.concat(#id.toString())") 64 | public void remove(Long id) { 65 | log.info("remove from db, id: {}", id); 66 | userMap.remove(id); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /spring-boot-caffeine-cache/src/main/java/com/km/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.km.service.impl; 2 | 3 | import com.km.entity.Info; 4 | import com.km.entity.User; 5 | import com.km.service.UserService; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.cache.annotation.CacheEvict; 8 | import org.springframework.cache.annotation.CachePut; 9 | import org.springframework.cache.annotation.Cacheable; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.Arrays; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | /** 18 | *

19 | * Created by zhezhiyong@163.com on 2017/9/21. 20 | */ 21 | @Service 22 | @Slf4j 23 | public class UserServiceImpl implements UserService { 24 | 25 | private Map userMap = new HashMap<>(); 26 | private Map infoMap = new HashMap<>(); 27 | 28 | public UserServiceImpl() { 29 | userMap.put(1L, new User(1L, "aaa", "666666")); 30 | userMap.put(2L, new User(2L, "bbb", "666666")); 31 | userMap.put(3L, new User(3L, "ccc", "666666")); 32 | infoMap.put(1L, new Info("18559198715", "福州市")); 33 | } 34 | 35 | @Override 36 | public List list() { 37 | return Arrays.asList(userMap.values().toArray()); 38 | } 39 | 40 | @Override 41 | @Cacheable(value = "user", key = "'user'.concat(#id.toString())") 42 | public User findUserById(Long id) { 43 | log.info("findUserById query from db, id: {}", id); 44 | return userMap.get(id); 45 | } 46 | 47 | @Override 48 | @Cacheable(value = "info", key = "'info'.concat(#id.toString())") 49 | public User findInfoById(Long id) { 50 | log.info("findInfoById query from db, id: {}", id); 51 | return userMap.get(id); 52 | } 53 | 54 | @Override 55 | @CachePut(value = "user", key = "'user'.concat(#user.id.toString())") 56 | public User update(User user) { 57 | log.info("update db, user: {}", user.toString()); 58 | userMap.put(user.getId(), user); 59 | return user; 60 | } 61 | 62 | @Override 63 | @CacheEvict(value = "user", key = "'user'.concat(#id.toString())") 64 | public void remove(Long id) { 65 | log.info("remove from db, id: {}", id); 66 | userMap.remove(id); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /spring-boot2-redis-cache/src/main/java/com/zy/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.zy.service.impl; 2 | 3 | import com.zy.entity.Info; 4 | import com.zy.entity.User; 5 | import com.zy.service.UserService; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.cache.annotation.CacheEvict; 8 | import org.springframework.cache.annotation.CachePut; 9 | import org.springframework.cache.annotation.Cacheable; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.Arrays; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | /** 18 | *

19 | * Created by zhezhiyong@163.com on 2017/9/21. 20 | */ 21 | @Service 22 | @Slf4j 23 | public class UserServiceImpl implements UserService { 24 | 25 | private Map userMap = new HashMap<>(); 26 | private Map infoMap = new HashMap<>(); 27 | 28 | public UserServiceImpl() { 29 | userMap.put(1L, new User(1L, "aaa", "666666")); 30 | userMap.put(2L, new User(2L, "bbb", "666666")); 31 | userMap.put(3L, new User(3L, "ccc", "666666")); 32 | infoMap.put(1L, new Info("18559198715", "福州市")); 33 | } 34 | 35 | @Override 36 | @Cacheable(value = "userList", key = "methodName") 37 | public List list() { 38 | return Arrays.asList(userMap.values().toArray()); 39 | } 40 | 41 | @Override 42 | @Cacheable(value = "user", key = "'user'.concat(#id.toString())") 43 | public User findUserById(Long id) { 44 | log.info("findUserById query from db, id: {}", id); 45 | return userMap.get(id); 46 | } 47 | 48 | @Override 49 | @Cacheable(value = "info", key = "'info'.concat(#id.toString())") 50 | public Info findInfoById(Long id) { 51 | log.info("findInfoById query from db, id: {}", id); 52 | return infoMap.get(id); 53 | } 54 | 55 | @Override 56 | @CachePut(value = "user", key = "'user'.concat(#user.id.toString())") 57 | public User update(User user) { 58 | log.info("update db, user: {}", user.toString()); 59 | userMap.put(user.getId(), user); 60 | return user; 61 | } 62 | 63 | @Override 64 | @CacheEvict(value = "user", key = "'user'.concat(#id.toString())") 65 | public void remove(Long id) { 66 | log.info("remove from db, id: {}", id); 67 | userMap.remove(id); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /spring-boot-caffeine-cache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.km 7 | spring-boot-caffeine-cache 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-boot-caffeine-cache 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.7.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-cache 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | com.github.ben-manes.caffeine 38 | caffeine 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | org.projectlombok 47 | lombok 48 | compile 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /spring-boot-guava-cache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.km 7 | spring-boot-guava-cache 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-boot-guava-cache 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.7.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-cache 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | com.google.guava 38 | guava 39 | 19.0 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | org.projectlombok 48 | lombok 49 | compile 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-maven-plugin 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /spring-boot2-multi-redis-cache/src/main/java/com/zy/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.zy.controller; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.google.common.collect.ImmutableMap; 5 | import com.zy.entity.User; 6 | import com.zy.service.UserService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.data.redis.core.RedisTemplate; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.PathVariable; 11 | import org.springframework.web.bind.annotation.ResponseBody; 12 | import org.springframework.web.bind.annotation.RestController; 13 | 14 | import javax.annotation.Resource; 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | /** 19 | *

20 | * 参考:https://blog.csdn.net/chenypgg/article/details/85698209 21 | *

22 | * Created by zhezhiyong@163.com on 2017/9/21. 23 | */ 24 | @RestController 25 | public class IndexController { 26 | 27 | @Autowired 28 | private UserService userService; 29 | 30 | @Resource(name = "redisTemplateB") 31 | private RedisTemplate redisTemplateB; 32 | 33 | @Resource(name = "redisTemplateA") 34 | private RedisTemplate redisTemplate; 35 | 36 | 37 | 38 | @GetMapping("/db/users") 39 | @ResponseBody 40 | public List users3() { 41 | redisTemplate.opsForValue().set("usersa", JSON.toJSONString(userService.list())); 42 | redisTemplateB.opsForValue().set("usersb", JSON.toJSONString(userService.list())); 43 | return userService.list(); 44 | } 45 | 46 | @GetMapping("/users") 47 | @ResponseBody 48 | public List users() { 49 | return userService.list(); 50 | } 51 | 52 | @GetMapping("/user/{id}") 53 | @ResponseBody 54 | public User findUserById(@PathVariable("id") Long id) { 55 | return userService.findUserById(id); 56 | } 57 | 58 | @GetMapping("/info/{id}") 59 | @ResponseBody 60 | public User findInfoById(@PathVariable("id") Long id) { 61 | return userService.findInfoById(id); 62 | } 63 | 64 | @GetMapping("/user/{id}/{name}") 65 | @ResponseBody 66 | public Map update(@PathVariable("id") Long id, @PathVariable("name") String name) { 67 | User user = userService.findUserById(id); 68 | user.setName(name); 69 | userService.update(user); 70 | return ImmutableMap.of("ret", 0, "msg", "ok"); 71 | } 72 | 73 | 74 | } 75 | -------------------------------------------------------------------------------- /guava-cache/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | spring-boot-redis-guava-caffeine-cache 5 | com.km 6 | 0.0.1-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | com.km 11 | guava-cache 12 | jar 13 | 14 | guava-cache 15 | http://maven.apache.org 16 | 17 | 18 | 19 | UTF-8 20 | 1.8 21 | 22 | 23 | 24 | 25 | junit 26 | junit 27 | 4.12 28 | test 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | com.google.guava 37 | guava 38 | 19.0 39 | 40 | 41 | org.projectlombok 42 | lombok 43 | 1.16.10 44 | compile 45 | 46 | 47 | mysql 48 | mysql-connector-java 49 | 5.1.39 50 | 51 | 52 | com.alibaba 53 | druid 54 | 1.0.22 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-compiler-plugin 64 | 65 | 1.8 66 | 1.8 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.km 7 | spring-boot-redis-cache 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-boot-redis-cache 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.7.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-cache 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | com.google.guava 38 | guava 39 | 19.0 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-data-redis 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | org.projectlombok 52 | lombok 53 | compile 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /spring-boot-caffeine-cache/src/main/java/com/km/config/CacheConfig.java: -------------------------------------------------------------------------------- 1 | package com.km.config; 2 | 3 | import com.github.benmanes.caffeine.cache.Caffeine; 4 | import org.springframework.cache.CacheManager; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | import org.springframework.cache.caffeine.CaffeineCache; 7 | import org.springframework.cache.support.SimpleCacheManager; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | import java.util.ArrayList; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | /** 15 | *

java方式:caffeine缓存配置

16 | * Created by zhezhiyong@163.com on 2017/9/22. 17 | */ 18 | @Configuration 19 | @EnableCaching 20 | public class CacheConfig { 21 | 22 | private static final int DEFAULT_MAXSIZE = 1000; 23 | private static final int DEFAULT_TTL = 3600; 24 | 25 | /** 26 | * 个性化配置缓存 27 | */ 28 | @Bean 29 | public CacheManager cacheManager() { 30 | SimpleCacheManager manager = new SimpleCacheManager(); 31 | //把各个cache注册到cacheManager中,CaffeineCache实现了org.springframework.cache.Cache接口 32 | ArrayList caches = new ArrayList<>(); 33 | for (Caches c : Caches.values()) { 34 | caches.add(new CaffeineCache(c.name(), 35 | Caffeine.newBuilder().recordStats() 36 | .expireAfterWrite(c.getTtl(), TimeUnit.SECONDS) 37 | .maximumSize(c.getMaxSize()) 38 | .build()) 39 | ); 40 | } 41 | manager.setCaches(caches); 42 | return manager; 43 | } 44 | 45 | /** 46 | * 定义cache名称、超时时长秒、最大个数 47 | * 每个cache缺省3600秒过期,最大个数1000 48 | */ 49 | public enum Caches { 50 | user(60, 2), 51 | info(5), 52 | role; 53 | 54 | private int maxSize = DEFAULT_MAXSIZE; //最大數量 55 | private int ttl = DEFAULT_TTL; //过期时间(秒) 56 | 57 | Caches() { 58 | } 59 | 60 | Caches(int ttl) { 61 | this.ttl = ttl; 62 | } 63 | 64 | Caches(int ttl, int maxSize) { 65 | this.ttl = ttl; 66 | this.maxSize = maxSize; 67 | } 68 | 69 | public int getMaxSize() { 70 | return maxSize; 71 | } 72 | 73 | public void setMaxSize(int maxSize) { 74 | this.maxSize = maxSize; 75 | } 76 | 77 | public int getTtl() { 78 | return ttl; 79 | } 80 | 81 | public void setTtl(int ttl) { 82 | this.ttl = ttl; 83 | } 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /guava-cache/src/main/java/com/km/db/DBPoolConnection.java: -------------------------------------------------------------------------------- 1 | package com.km.db; 2 | 3 | import com.alibaba.druid.pool.DruidDataSource; 4 | import com.alibaba.druid.pool.DruidDataSourceFactory; 5 | import com.alibaba.druid.pool.DruidPooledConnection; 6 | 7 | import java.io.File; 8 | import java.io.FileInputStream; 9 | import java.io.InputStream; 10 | import java.sql.SQLException; 11 | import java.util.Properties; 12 | 13 | /** 14 | *

单例模式,保证全局只有一个数据库连接池

15 | * Created by zhezhiyong@163.com on 2017/9/25. 16 | */ 17 | public class DBPoolConnection { 18 | private static DBPoolConnection dbPoolConnection = null; 19 | private static DruidDataSource druidDataSource = null; 20 | 21 | static { 22 | Properties properties = loadPropertiesFile("db_server.properties"); 23 | try { 24 | druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); //DruidDataSrouce工厂模式 25 | } catch (Exception e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | 30 | /** 31 | * 数据库连接池单例 32 | * 33 | * @return 34 | */ 35 | public static synchronized DBPoolConnection getInstance() { 36 | if (null == dbPoolConnection) { 37 | dbPoolConnection = new DBPoolConnection(); 38 | } 39 | return dbPoolConnection; 40 | } 41 | 42 | /** 43 | * @param fullFile 配置文件名 44 | * @return Properties对象 45 | */ 46 | private static Properties loadPropertiesFile(String fullFile) { 47 | String webRootPath = null; 48 | if (null == fullFile || fullFile.equals("")) { 49 | throw new IllegalArgumentException("Properties file path can not be null" + fullFile); 50 | } 51 | webRootPath = DBPoolConnection.class.getClassLoader().getResource("").getPath(); 52 | webRootPath = new File(webRootPath).getParent(); 53 | InputStream inputStream = null; 54 | Properties p = null; 55 | try { 56 | inputStream = new FileInputStream(new File(webRootPath + File.separator + fullFile)); 57 | p = new Properties(); 58 | p.load(inputStream); 59 | } catch (Exception e) { 60 | e.printStackTrace(); 61 | } finally { 62 | try { 63 | if (null != inputStream) { 64 | inputStream.close(); 65 | } 66 | } catch (Exception e) { 67 | e.printStackTrace(); 68 | } 69 | } 70 | return p; 71 | } 72 | 73 | /** 74 | * 返回druid数据库连接 75 | * 76 | * @return 77 | * @throws SQLException 78 | */ 79 | public DruidPooledConnection getConnection() throws SQLException { 80 | return druidDataSource.getConnection(); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /spring-boot2-redis-cache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.3.RELEASE 9 | 10 | 11 | com.zy 12 | spring-boot2-redis-cache 13 | 0.0.1-SNAPSHOT 14 | spring-boot2-redis-cache 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-cache 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-data-redis 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | 36 | org.projectlombok 37 | lombok 38 | true 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | com.google.guava 47 | guava 48 | 27.0.1-jre 49 | compile 50 | 51 | 52 | com.alibaba 53 | fastjson 54 | 1.2.49 55 | 56 | 57 | 58 | org.apache.commons 59 | commons-pool2 60 | 61 | 62 | 63 | 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-maven-plugin 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /spring-boot2-multi-redis-cache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.3.RELEASE 9 | 10 | 11 | com.zy 12 | spring-boot2-multi-redis-cache 13 | 0.0.1-SNAPSHOT 14 | spring-boot2-multi-redis-cache 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-cache 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-data-redis 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | 36 | org.projectlombok 37 | lombok 38 | true 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | com.google.guava 47 | guava 48 | 27.0.1-jre 49 | compile 50 | 51 | 52 | com.alibaba 53 | fastjson 54 | 1.2.49 55 | 56 | 57 | 58 | org.apache.commons 59 | commons-pool2 60 | 61 | 62 | 63 | 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-maven-plugin 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /spring-boot-guava-cache/src/main/java/com/km/config/GuavaConfig.java: -------------------------------------------------------------------------------- 1 | package com.km.config; 2 | 3 | import com.google.common.cache.CacheBuilder; 4 | import org.springframework.cache.CacheManager; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | import org.springframework.cache.guava.GuavaCache; 7 | import org.springframework.cache.support.SimpleCacheManager; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | 11 | import java.util.ArrayList; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | /** 15 | *

guava缓存配置

16 | * Created by zhezhiyong@163.com on 2017/9/22. 17 | */ 18 | @Configuration 19 | @EnableCaching 20 | public class GuavaConfig { 21 | 22 | private static final int DEFAULT_MAXSIZE = 1000; 23 | private static final int DEFAULT_TTL = 3600; 24 | 25 | /** 26 | * 个性化配置缓存 27 | */ 28 | @Bean 29 | public CacheManager cacheManager() { 30 | SimpleCacheManager manager = new SimpleCacheManager(); 31 | //把各个cache注册到cacheManager中,GuavaCache实现了org.springframework.cache.Cache接口 32 | ArrayList caches = new ArrayList<>(); 33 | for (Caches c : Caches.values()) { 34 | caches.add(new GuavaCache(c.name(), CacheBuilder.newBuilder().recordStats().expireAfterWrite(c.getTtl(), TimeUnit.SECONDS).maximumSize(c.getMaxSize()).build())); 35 | } 36 | manager.setCaches(caches); 37 | return manager; 38 | } 39 | 40 | /** 41 | * 定义cache名称、超时时长秒、最大个数 42 | * 每个cache缺省3600秒过期,最大个数1000 43 | */ 44 | public enum Caches { 45 | user(60, 2), 46 | info(5), 47 | role; 48 | 49 | private int maxSize = DEFAULT_MAXSIZE; //最大數量 50 | private int ttl = DEFAULT_TTL; //过期时间(秒) 51 | 52 | Caches() { 53 | } 54 | 55 | Caches(int ttl) { 56 | this.ttl = ttl; 57 | } 58 | 59 | Caches(int ttl, int maxSize) { 60 | this.ttl = ttl; 61 | this.maxSize = maxSize; 62 | } 63 | 64 | public int getMaxSize() { 65 | return maxSize; 66 | } 67 | 68 | public void setMaxSize(int maxSize) { 69 | this.maxSize = maxSize; 70 | } 71 | 72 | public int getTtl() { 73 | return ttl; 74 | } 75 | 76 | public void setTtl(int ttl) { 77 | this.ttl = ttl; 78 | } 79 | } 80 | 81 | // /** 82 | // * 配置全局缓存参数,3600秒过期,最大个数1000 83 | // */ 84 | // @Bean 85 | // public CacheManager cacheManager() { 86 | // GuavaCacheManager cacheManager = new GuavaCacheManager(); 87 | // cacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(3600, TimeUnit.SECONDS).maximumSize(1000)); 88 | // return cacheManager; 89 | // } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /spring-boot2-redis-cache/src/main/java/com/zy/config/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.zy.config; 2 | 3 | import com.fasterxml.jackson.annotation.JsonAutoDetect; 4 | import com.fasterxml.jackson.annotation.PropertyAccessor; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import lombok.extern.slf4j.Slf4j; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.cache.CacheManager; 9 | import org.springframework.cache.annotation.CachingConfigurerSupport; 10 | import org.springframework.cache.annotation.EnableCaching; 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.data.redis.cache.RedisCacheConfiguration; 14 | import org.springframework.data.redis.cache.RedisCacheManager; 15 | import org.springframework.data.redis.connection.RedisConnectionFactory; 16 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 17 | import org.springframework.data.redis.serializer.RedisSerializationContext; 18 | import org.springframework.data.redis.serializer.RedisSerializer; 19 | import org.springframework.data.redis.serializer.StringRedisSerializer; 20 | 21 | import java.time.Duration; 22 | 23 | /** 24 | *

25 | * Created by @author zhezhiyong@163.com on 2019/3/29. 26 | */ 27 | @Configuration 28 | @EnableCaching 29 | @Slf4j 30 | public class RedisConfig extends CachingConfigurerSupport { 31 | 32 | @Value("${spring.application.name:unknown}") 33 | private String appName; 34 | @Value("${spring.redis.timeToLive:600s}") 35 | private Duration timeToLive; 36 | 37 | @Bean 38 | public CacheManager cacheManager(RedisConnectionFactory factory) { 39 | RedisSerializer redisSerializer = new StringRedisSerializer(); 40 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 41 | 42 | //解决查询缓存转换异常的问题 43 | ObjectMapper om = new ObjectMapper(); 44 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 45 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 46 | jackson2JsonRedisSerializer.setObjectMapper(om); 47 | 48 | // 配置序列化(解决乱码的问题) 49 | RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() 50 | .entryTtl(timeToLive) 51 | .prefixKeysWith(appName + ":") 52 | .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) 53 | .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) 54 | // 是否允许控制存储 55 | .disableCachingNullValues(); 56 | 57 | RedisCacheManager cacheManager = RedisCacheManager.builder(factory) 58 | .cacheDefaults(config) 59 | .build(); 60 | return cacheManager; 61 | } 62 | } -------------------------------------------------------------------------------- /guava-cache/src/main/java/com/km/cache/CacheOptions.java: -------------------------------------------------------------------------------- 1 | package com.km.cache; 2 | 3 | /** 4 | *

缓存相关配置

5 | * Created by zhezhiyong@163.com on 2017/9/25. 6 | */ 7 | public class CacheOptions { 8 | 9 | private final static int DEFAULT_INITIAL_CAPACITY = 1024; 10 | private final static int DEFAULT_MAXIMUM_SIZE = 65536; 11 | private final static int DEFAULT_EXPIRE_AFTER_ACCESS_SECONDS = (int) (60 * 60 * 24); 12 | private final static int DEFAULT_EXPIRE_AFTER_WRITE_SECONDS = (int) (60 * 60 * 24); 13 | 14 | public final int initialCapacity; 15 | public final int maximumSize; 16 | public final int expireAfterAccessSeconds; 17 | public final int expireAfterWriteSeconds; 18 | 19 | private CacheOptions(int initialCapacity, int maximumSize, int expireAfterAccessSeconds, int expireAfterWriteSeconds) { 20 | this.initialCapacity = initialCapacity; 21 | this.maximumSize = maximumSize; 22 | this.expireAfterAccessSeconds = expireAfterAccessSeconds; 23 | this.expireAfterWriteSeconds = expireAfterWriteSeconds; 24 | } 25 | 26 | public static CacheOptions defaultCacheOptions() { 27 | return new Builder().build(); 28 | } 29 | 30 | static class Builder { 31 | private int initialCapacity; 32 | private int maximumSize; 33 | private int expireAfterAccessSeconds; 34 | private int expireAfterWriteSeconds; 35 | 36 | private Builder() { 37 | 38 | } 39 | 40 | public Builder setInitialCapacity(int initialCapacity) { 41 | this.initialCapacity = initialCapacity; 42 | return this; 43 | } 44 | 45 | public Builder setMaximumSize(int maximumSize) { 46 | this.maximumSize = maximumSize; 47 | return this; 48 | } 49 | 50 | public Builder setExpireAfterAccessSeconds(int expireAfterAccessSeconds) { 51 | this.expireAfterAccessSeconds = expireAfterAccessSeconds; 52 | return this; 53 | } 54 | 55 | public Builder setExpireAfterWriteSeconds(int expireAfterWriteSeconds) { 56 | this.expireAfterWriteSeconds = expireAfterWriteSeconds; 57 | return this; 58 | } 59 | 60 | private CacheOptions build() { 61 | if (initialCapacity == 0) { 62 | setInitialCapacity(DEFAULT_INITIAL_CAPACITY); 63 | } 64 | if (maximumSize == 0) { 65 | setMaximumSize(DEFAULT_MAXIMUM_SIZE); 66 | } 67 | if (expireAfterAccessSeconds == 0) { 68 | setExpireAfterAccessSeconds(DEFAULT_EXPIRE_AFTER_ACCESS_SECONDS); 69 | } 70 | if (expireAfterWriteSeconds == 0) { 71 | setExpireAfterWriteSeconds(DEFAULT_EXPIRE_AFTER_WRITE_SECONDS); 72 | } 73 | return new CacheOptions(initialCapacity, maximumSize, expireAfterAccessSeconds, expireAfterWriteSeconds); 74 | } 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /spring-boot-redis-cache/src/main/java/com/km/config/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.km.config; 2 | 3 | import com.fasterxml.jackson.annotation.JsonAutoDetect; 4 | import com.fasterxml.jackson.annotation.PropertyAccessor; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import org.springframework.cache.CacheManager; 7 | import org.springframework.cache.annotation.CachingConfigurerSupport; 8 | import org.springframework.cache.annotation.EnableCaching; 9 | import org.springframework.cache.interceptor.KeyGenerator; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.data.redis.cache.RedisCacheManager; 13 | import org.springframework.data.redis.cache.RedisCachePrefix; 14 | import org.springframework.data.redis.connection.RedisConnectionFactory; 15 | import org.springframework.data.redis.core.RedisTemplate; 16 | import org.springframework.data.redis.core.StringRedisTemplate; 17 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 18 | 19 | import java.lang.reflect.Method; 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | /** 24 | *

redis缓存配置

25 | * Created by zhezhiyong@163.com on 2017/9/21. 26 | */ 27 | @Configuration 28 | @EnableCaching 29 | public class RedisConfig extends CachingConfigurerSupport { 30 | 31 | @Bean 32 | public KeyGenerator KeyGenerator() { 33 | return new KeyGenerator() { 34 | @Override 35 | public Object generate(Object target, Method method, Object... params) { 36 | StringBuilder sb = new StringBuilder(); 37 | sb.append(target.getClass().getName()); 38 | sb.append(method.getName()); 39 | for (Object obj : params) { 40 | sb.append(obj.toString()); 41 | } 42 | return sb.toString(); 43 | } 44 | }; 45 | } 46 | 47 | @Bean 48 | public CacheManager cacheManager(RedisTemplate redisTemplate) { 49 | RedisCacheManager manager = new RedisCacheManager(redisTemplate); 50 | manager.setUsePrefix(true); 51 | RedisCachePrefix cachePrefix = new RedisPrefix("prefix"); 52 | manager.setCachePrefix(cachePrefix); 53 | // 整体缓存过期时间 54 | manager.setDefaultExpiration(3600L); 55 | // 设置缓存过期时间。key和缓存过期时间,单位秒 56 | Map expiresMap = new HashMap<>(); 57 | expiresMap.put("user", 1000L); 58 | manager.setExpires(expiresMap); 59 | return manager; 60 | } 61 | 62 | @Bean 63 | public RedisTemplate redisTemplate(RedisConnectionFactory factory) { 64 | StringRedisTemplate template = new StringRedisTemplate(factory); 65 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 66 | ObjectMapper om = new ObjectMapper(); 67 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 68 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 69 | jackson2JsonRedisSerializer.setObjectMapper(om); 70 | template.setValueSerializer(jackson2JsonRedisSerializer); 71 | template.afterPropertiesSet(); 72 | return template; 73 | } 74 | } -------------------------------------------------------------------------------- /guava-cache/src/main/java/com/km/db/DBOperation.java: -------------------------------------------------------------------------------- 1 | package com.km.db; 2 | 3 | import com.alibaba.druid.pool.DruidPooledConnection; 4 | 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.sql.Statement; 9 | import java.util.HashMap; 10 | 11 | /** 12 | *

db操作类

13 | * Created by zhezhiyong@163.com on 2017/9/25. 14 | */ 15 | public class DBOperation { 16 | 17 | private DruidPooledConnection con = null;//数据库连接 18 | 19 | private void open() throws SQLException { 20 | con = DBPoolConnection.getInstance().getConnection(); 21 | } 22 | 23 | public void close() { 24 | if (this.con != null) { 25 | try { 26 | this.con.close(); 27 | } catch (SQLException e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | } 32 | 33 | 34 | /** 35 | * sql语句参数转化 36 | * 37 | * @param sql sql语句 38 | * @param params 参数 39 | * @throws SQLException sql异常 40 | * @throws ClassNotFoundException sql异常 41 | */ 42 | private PreparedStatement setPres(String sql, HashMap params) throws SQLException, ClassNotFoundException { 43 | if (null == params || params.size() < 1) { 44 | return null; 45 | } 46 | PreparedStatement pres = this.con.prepareStatement(sql); 47 | for (int i = 1; i <= params.size(); i++) { 48 | if (params.get(i) == null) { 49 | pres.setString(i, ""); 50 | } else if (params.get(i).getClass() == Class.forName("java.lang.String")) { 51 | pres.setString(i, params.get(i).toString()); 52 | } else if (params.get(i).getClass() == Class.forName("java.lang.Integer")) { 53 | pres.setInt(i, (Integer) params.get(i)); 54 | } else if (params.get(i).getClass() == Class.forName("java.lang.Long")) { 55 | pres.setLong(i, (Long) params.get(i)); 56 | } else if (params.get(i).getClass() == Class.forName("java.lang.Double")) { 57 | pres.setDouble(i, (Double) params.get(i)); 58 | } else if (params.get(i).getClass() == Class.forName("java.lang.Flaot")) { 59 | pres.setFloat(i, (Float) params.get(i)); 60 | } else if (params.get(i).getClass() == Class.forName("java.lang.Boolean")) { 61 | pres.setBoolean(i, (Boolean) params.get(i)); 62 | } else if (params.get(i).getClass() == Class.forName("java.sql.Date")) { 63 | pres.setDate(i, java.sql.Date.valueOf(params.get(i).toString())); 64 | } else { 65 | return null; 66 | } 67 | } 68 | return pres; 69 | } 70 | 71 | /** 72 | * @param sql sql语句 73 | */ 74 | public int executeUpdate(String sql) throws SQLException { 75 | this.open(); 76 | Statement state = this.con.createStatement(); 77 | return state.executeUpdate(sql); 78 | } 79 | 80 | /** 81 | * @param sql sql语句 82 | * @param params 参数 83 | */ 84 | public int executeUpdate(String sql, HashMap params) throws SQLException, ClassNotFoundException { 85 | this.open(); 86 | PreparedStatement pres = setPres(sql, params); 87 | if (null == pres) { 88 | return 0; 89 | } 90 | return pres.executeUpdate(); 91 | } 92 | 93 | /** 94 | * @param sql sql语句 95 | */ 96 | public ResultSet executeQuery(String sql) throws SQLException { 97 | this.open(); 98 | Statement state = this.con.createStatement(); 99 | return state.executeQuery(sql); 100 | } 101 | 102 | /** 103 | * @param sql sql语句 104 | */ 105 | public ResultSet executeQuery(String sql, HashMap params) throws SQLException, ClassNotFoundException { 106 | this.open(); 107 | PreparedStatement pres = setPres(sql, params); 108 | if (null == pres) { 109 | return null; 110 | } 111 | return pres.executeQuery(); 112 | } 113 | 114 | 115 | } 116 | -------------------------------------------------------------------------------- /guava-cache/src/main/java/com/km/db/DBUtil.java: -------------------------------------------------------------------------------- 1 | package com.km.db; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | import java.util.HashMap; 6 | 7 | /** 8 | *

9 | * Created by zhezhiyong@163.com on 2017/9/25. 10 | */ 11 | public class DBUtil { 12 | private DBOperation dbOperation; 13 | 14 | public DBUtil() { 15 | dbOperation = new DBOperation(); 16 | } 17 | 18 | /** 19 | * 关闭数据库连接 20 | */ 21 | public void close() { 22 | dbOperation.close(); 23 | } 24 | 25 | /** 26 | * 数据库新增操作 27 | */ 28 | public int insert(String sql) throws SQLException { 29 | return dbOperation.executeUpdate(sql); 30 | } 31 | 32 | /** 33 | * 数据库新增操作 34 | * 35 | * @param tableName 表名 36 | * @param columns 字段名 37 | * @param params 参数 38 | */ 39 | public int insert(String tableName, String columns, HashMap params) throws SQLException, ClassNotFoundException { 40 | String sql = insertSql(tableName, columns); 41 | return dbOperation.executeUpdate(sql, params); 42 | } 43 | 44 | /** 45 | * 数据库删除操作 46 | */ 47 | public int delete(String sql) throws SQLException { 48 | return dbOperation.executeUpdate(sql); 49 | } 50 | 51 | /** 52 | * 数据库删除操作 53 | * 54 | * @param tableName 表名 55 | * @param condition 条件 56 | */ 57 | public int delete(String tableName, String condition) throws SQLException { 58 | if (null == tableName) { 59 | return 0; 60 | } 61 | String sql = "delete from " + tableName + " " + condition; 62 | return dbOperation.executeUpdate(sql); 63 | } 64 | 65 | /** 66 | * 数据库更新操作 67 | */ 68 | public int update(String sql) throws SQLException { 69 | return dbOperation.executeUpdate(sql); 70 | } 71 | 72 | /** 73 | * 数据库更新操作 74 | * 75 | * @param tableName 表名 76 | * @param columns 字段 77 | * @param condition 条件 78 | * @param params 参数 79 | */ 80 | public int update(String tableName, String columns, String condition, HashMap params) throws SQLException, ClassNotFoundException { 81 | String sql = updateSql(tableName, columns, condition); 82 | return dbOperation.executeUpdate(sql, params); 83 | } 84 | 85 | /** 86 | * 数据库查询操作 87 | */ 88 | public ResultSet select(String sql) throws SQLException { 89 | return dbOperation.executeQuery(sql); 90 | } 91 | 92 | /** 93 | * 数据库查询操作 94 | * 95 | * @param tableName 表名 96 | * @param columns 字段 97 | * @param condition 条件 98 | */ 99 | public ResultSet select(String tableName, String columns, String condition) throws SQLException { 100 | String sql = "select " + columns + " from " + tableName + " " + condition; 101 | return dbOperation.executeQuery(sql); 102 | } 103 | 104 | /** 105 | * 组装 update sql eg: update tableName set column1=?,column2=? condition 106 | * 107 | * @param tableName 表名 108 | * @param columns 字段 109 | * @param condition 条件 110 | */ 111 | private String updateSql(String tableName, String columns, String condition) { 112 | if (tableName == null || columns == null) { 113 | return ""; 114 | } 115 | String[] column = columns.split(","); 116 | StringBuilder sb = new StringBuilder(); 117 | sb.append("update "); 118 | sb.append(tableName); 119 | sb.append(" set "); 120 | sb.append(column[0]); 121 | sb.append("=?"); 122 | for (int i = 1; i < column.length; i++) { 123 | sb.append(", "); 124 | sb.append(column[i]); 125 | sb.append("=?"); 126 | } 127 | sb.append(" "); 128 | sb.append(condition); 129 | return sb.toString(); 130 | } 131 | 132 | /** 133 | * 组装 insert sql eg: insert into tableName (column1, column2) values (?,?) 134 | * 135 | * @param tableName 表名 136 | * @param columns 字段 137 | */ 138 | private String insertSql(String tableName, String columns) { 139 | if (tableName == null || columns == null) { 140 | return ""; 141 | } 142 | int n = columns.split(",").length; 143 | StringBuilder sb = new StringBuilder(""); 144 | sb.append("insert into "); 145 | sb.append(tableName); 146 | sb.append(" ("); 147 | sb.append(columns); 148 | sb.append(") values (?"); 149 | for (int i = 1; i < n; i++) { 150 | sb.append(",?"); 151 | } 152 | sb.append(")"); 153 | return sb.toString(); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /spring-boot2-multi-redis-cache/src/main/java/com/zy/config/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.zy.config; 2 | 3 | import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; 4 | import com.fasterxml.jackson.annotation.JsonAutoDetect; 5 | import com.fasterxml.jackson.annotation.PropertyAccessor; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.apache.commons.pool2.impl.GenericObjectPoolConfig; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.beans.factory.annotation.Qualifier; 11 | import org.springframework.beans.factory.annotation.Value; 12 | import org.springframework.boot.context.properties.ConfigurationProperties; 13 | import org.springframework.cache.CacheManager; 14 | import org.springframework.cache.annotation.CachingConfigurerSupport; 15 | import org.springframework.cache.annotation.EnableCaching; 16 | import org.springframework.context.annotation.Bean; 17 | import org.springframework.context.annotation.Configuration; 18 | import org.springframework.context.annotation.Primary; 19 | import org.springframework.context.annotation.Scope; 20 | import org.springframework.data.redis.cache.RedisCacheConfiguration; 21 | import org.springframework.data.redis.cache.RedisCacheManager; 22 | import org.springframework.data.redis.connection.RedisConnectionFactory; 23 | import org.springframework.data.redis.connection.RedisStandaloneConfiguration; 24 | import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; 25 | import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; 26 | import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; 27 | import org.springframework.data.redis.core.StringRedisTemplate; 28 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 29 | import org.springframework.data.redis.serializer.RedisSerializationContext; 30 | import org.springframework.data.redis.serializer.RedisSerializer; 31 | import org.springframework.data.redis.serializer.StringRedisSerializer; 32 | 33 | import java.time.Duration; 34 | 35 | /** 36 | *

37 | * Created by @author zhezhiyong@163.com on 2019/3/29. 38 | */ 39 | @Configuration 40 | @EnableCaching 41 | @Slf4j 42 | public class RedisConfig extends CachingConfigurerSupport { 43 | 44 | @Value("${spring.application.name:unknown}") 45 | private String appName; 46 | @Value("${spring.redis.timeToLive:15}") 47 | private Long timeToLive; 48 | 49 | @Bean 50 | @ConfigurationProperties(prefix = "spring.redis.lettuce.pool") 51 | @Scope(value = "prototype") 52 | public GenericObjectPoolConfig redisPool() { 53 | return new GenericObjectPoolConfig(); 54 | } 55 | 56 | @Bean 57 | @ConfigurationProperties(prefix = "spring.redis.redis-a") 58 | public RedisStandaloneConfiguration redisConfigA() { 59 | return new RedisStandaloneConfiguration(); 60 | } 61 | 62 | @Bean 63 | @ConfigurationProperties(prefix = "spring.redis.redis-b") 64 | public RedisStandaloneConfiguration redisConfigB() { 65 | return new RedisStandaloneConfiguration(); 66 | } 67 | 68 | @Bean 69 | @Primary 70 | public LettuceConnectionFactory factoryA(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfigA) { 71 | LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder() 72 | .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build(); 73 | return new LettuceConnectionFactory(redisConfigA, clientConfiguration); 74 | } 75 | 76 | @Bean 77 | public LettuceConnectionFactory factoryB(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfigB) { 78 | LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder() 79 | .poolConfig(config).commandTimeout(Duration.ofMillis(config.getMaxWaitMillis())).build(); 80 | return new LettuceConnectionFactory(redisConfigB, clientConfiguration); 81 | } 82 | 83 | @Bean(name = "redisTemplateA") 84 | public StringRedisTemplate redisTemplateA(LettuceConnectionFactory factoryA) { 85 | StringRedisTemplate template = new StringRedisTemplate(factoryA); 86 | RedisSerializer redisSerializer = new StringRedisSerializer(); 87 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 88 | ObjectMapper om = new ObjectMapper(); 89 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 90 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 91 | jackson2JsonRedisSerializer.setObjectMapper(om); 92 | template.setKeySerializer(redisSerializer); 93 | template.setValueSerializer(jackson2JsonRedisSerializer); 94 | template.setHashValueSerializer(jackson2JsonRedisSerializer); 95 | return template; 96 | } 97 | 98 | @Bean(name = "redisTemplateB") 99 | public StringRedisTemplate redisTemplateB(@Autowired @Qualifier("factoryB") LettuceConnectionFactory factoryB) { 100 | StringRedisTemplate template = new StringRedisTemplate(factoryB); 101 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 102 | ObjectMapper om = new ObjectMapper(); 103 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 104 | // 将类名称序列化到json串中 105 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 106 | jackson2JsonRedisSerializer.setObjectMapper(om); 107 | template.setValueSerializer(jackson2JsonRedisSerializer); 108 | template.afterPropertiesSet(); 109 | return template; 110 | } 111 | 112 | @Bean 113 | public CacheManager cacheManager(RedisConnectionFactory factory) { 114 | RedisSerializer redisSerializer = new StringRedisSerializer(); 115 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 116 | ObjectMapper om = new ObjectMapper(); 117 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 118 | // 将类名称序列化到json串中 119 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 120 | jackson2JsonRedisSerializer.setObjectMapper(om); 121 | 122 | // 配置序列化 123 | RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() 124 | .entryTtl(Duration.ofMinutes(timeToLive)) 125 | .prefixKeysWith(appName + ":"); 126 | RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) 127 | .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)); 128 | RedisCacheManager cacheManager = RedisCacheManager.builder(factory) 129 | .cacheDefaults(redisCacheConfiguration) 130 | .build(); 131 | return cacheManager; 132 | } 133 | 134 | // RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig(); 135 | // configuration.prefixKeysWith("bda"); 136 | // configuration = configuration.entryTtl(Duration.ofMinutes(30)); 137 | // Set cacheNames = new HashSet<>(); 138 | // cacheNames.add("test1"); 139 | // cacheNames.add("test2"); 140 | // Map configurationMap = new HashMap<>(10); 141 | // configurationMap.put("test1", configuration); 142 | // configurationMap.put("test2", configuration.entryTtl(Duration.ofMinutes(60))); 143 | // RedisCacheManager manager = RedisCacheManager.builder(factory) 144 | // .initialCacheNames(cacheNames) 145 | // .withInitialCacheConfigurations(configurationMap) 146 | // .build(); 147 | // return manager; 148 | 149 | private StringRedisTemplate getRedisTemplate() { 150 | StringRedisTemplate template = new StringRedisTemplate(); 151 | template.setValueSerializer(new GenericFastJsonRedisSerializer()); 152 | template.setValueSerializer(new StringRedisSerializer()); 153 | return template; 154 | } 155 | 156 | // @Bean 157 | // public CacheManager cacheManager(RedisConnectionFactory factory) { 158 | // RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig(); 159 | // configuration.prefixKeysWith("bda"); 160 | // configuration = configuration.entryTtl(Duration.ofMinutes(30)); 161 | // Set cacheNames = new HashSet<>(); 162 | // cacheNames.add("test1"); 163 | // cacheNames.add("test2"); 164 | // Map configurationMap = new HashMap<>(10); 165 | // configurationMap.put("test1", configuration); 166 | // configurationMap.put("test2", configuration.entryTtl(Duration.ofMinutes(60))); 167 | // RedisCacheManager manager = RedisCacheManager.builder(factory) 168 | // .initialCacheNames(cacheNames) 169 | // .withInitialCacheConfigurations(configurationMap) 170 | // .build(); 171 | // return manager; 172 | // } 173 | 174 | // @Bean 175 | // public RedisTemplate redisTemplate(RedisConnectionFactory factory) { 176 | // StringRedisTemplate template = new StringRedisTemplate(factory); 177 | // Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 178 | // ObjectMapper om = new ObjectMapper(); 179 | // om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 180 | // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 181 | // jackson2JsonRedisSerializer.setObjectMapper(om); 182 | // template.setValueSerializer(jackson2JsonRedisSerializer); 183 | // template.afterPropertiesSet(); 184 | // return template; 185 | // } 186 | // 187 | // @Autowired 188 | // private RedisProperties redisProperties; 189 | // 190 | // 191 | // @Bean 192 | // @ConfigurationProperties(prefix = "spring.redis3") 193 | // public RedisStandaloneConfiguration redis3(){ 194 | // return new RedisStandaloneConfiguration(); 195 | // } 196 | // 197 | // 198 | // @Bean(name = "db3redisTemplate") 199 | // public RedisTemplate db3RedisTemplate() { 200 | // log.info("redisProperties:{}", JSON.toJSONString(redisProperties)); 201 | // RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); 202 | // configuration.setDatabase(3); 203 | // configuration.setHostName(redisProperties.getHost()); 204 | // configuration.setPort(redisProperties.getPort()); 205 | // LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfigurationBuilder = LettuceClientConfiguration.builder(); 206 | // LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration, lettuceClientConfigurationBuilder.build()); 207 | // 208 | // StringRedisTemplate template = new StringRedisTemplate(factory); 209 | // Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 210 | // ObjectMapper om = new ObjectMapper(); 211 | // om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 212 | // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 213 | // jackson2JsonRedisSerializer.setObjectMapper(om); 214 | // template.setValueSerializer(jackson2JsonRedisSerializer); 215 | // template.afterPropertiesSet(); 216 | // return template; 217 | // } 218 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------