From 1fe832deafbe3e83a76c789665810b7e1ceb15a2 Mon Sep 17 00:00:00 2001 From: lance Date: Sat, 2 Jul 2016 13:49:01 +0800 Subject: [PATCH] add spring-boot-cache-redis project add spring-boot-cache-redis project --- .../java/com/lance/cache/redis/CityInfo.java | 32 ++++++++++ .../com/lance/cache/redis/CityService.java | 19 ++++++ .../lance/cache/redis/SimpleApplication.java | 16 +++++ .../java/com/lance/cache/redis/TaskJob.java | 40 ++++++++++++ .../src/main/resources/application.properties | 20 ++++++ .../src/main/resources/log4j2.xml | 61 +++++++++++++++++++ .../src/main/webapp/WEB-INF/web.xml | 7 +++ .../src/main/webapp/index.jsp | 5 ++ 8 files changed, 200 insertions(+) create mode 100644 spring-boot-cache-redis/src/main/java/com/lance/cache/redis/CityInfo.java create mode 100644 spring-boot-cache-redis/src/main/java/com/lance/cache/redis/CityService.java create mode 100644 spring-boot-cache-redis/src/main/java/com/lance/cache/redis/SimpleApplication.java create mode 100644 spring-boot-cache-redis/src/main/java/com/lance/cache/redis/TaskJob.java create mode 100644 spring-boot-cache-redis/src/main/resources/application.properties create mode 100644 spring-boot-cache-redis/src/main/resources/log4j2.xml create mode 100644 spring-boot-cache-redis/src/main/webapp/WEB-INF/web.xml create mode 100644 spring-boot-cache-redis/src/main/webapp/index.jsp diff --git a/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/CityInfo.java b/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/CityInfo.java new file mode 100644 index 0000000..eae18e9 --- /dev/null +++ b/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/CityInfo.java @@ -0,0 +1,32 @@ +package com.lance.cache.redis; + +import java.io.Serializable; + +public class CityInfo implements Serializable{ + private static final long serialVersionUID = 2845294956907027149L; + + private int id; + private String city; + + public CityInfo() { + + } + + public CityInfo(int id, String city) { + this.id = id; + this.city = city; + } + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + public String getCity() { + return city; + } + public void setCity(String city) { + this.city = city; + } +} diff --git a/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/CityService.java b/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/CityService.java new file mode 100644 index 0000000..abe0995 --- /dev/null +++ b/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/CityService.java @@ -0,0 +1,19 @@ +package com.lance.cache.redis; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Component; + +@Component +@CacheConfig(cacheNames="CityService") +public class CityService { + Logger logger = LogManager.getLogger(getClass()); + + @Cacheable + public CityInfo getCity(int id, String city) { + logger.info("id: {}, city: {}", id, city); + return new CityInfo(id, city); + } +} diff --git a/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/SimpleApplication.java b/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/SimpleApplication.java new file mode 100644 index 0000000..89b606c --- /dev/null +++ b/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/SimpleApplication.java @@ -0,0 +1,16 @@ +package com.lance.cache.redis; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.scheduling.annotation.EnableScheduling; + +@EnableCaching +@EnableScheduling +@SpringBootApplication +public class SimpleApplication { + + public static void main(String[] args) { + SpringApplication.run(SimpleApplication.class, args); + } +} diff --git a/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/TaskJob.java b/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/TaskJob.java new file mode 100644 index 0000000..c58141e --- /dev/null +++ b/spring-boot-cache-redis/src/main/java/com/lance/cache/redis/TaskJob.java @@ -0,0 +1,40 @@ +package com.lance.cache.redis; + +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import com.alibaba.fastjson.JSON; + +@Component +public class TaskJob { + Logger logger = LogManager.getLogger(getClass()); + @Autowired + private CityService cityService; + + /** + * Job + */ + @Scheduled(fixedDelay = 500) + public void retrieveCountry() { + int index = new Random().nextInt(list.size()); + String city = find(index); + CityInfo info = cityService.getCity(index, city); + + logger.info("city: {}", JSON.toJSONString(info)); + } + + private String find(int index) { + return list.get(index); + } + + private static final List list = Arrays.asList("北京市", "上海市", "天津市", "重庆市", "河北省", "山西省", "内蒙古自治区", "辽宁省", + "吉林省", "黑龙江", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西自治区", "海南省", "四川省", + "贵州省", "云南省", "西藏自治区", "陕西省", "甘肃省", "青海省", "宁夏自治区", "新疆自治区", "香港特别行政区", "澳门特别行政区", "台湾省"); +} diff --git a/spring-boot-cache-redis/src/main/resources/application.properties b/spring-boot-cache-redis/src/main/resources/application.properties new file mode 100644 index 0000000..5679a21 --- /dev/null +++ b/spring-boot-cache-redis/src/main/resources/application.properties @@ -0,0 +1,20 @@ +# IDENTITY (ContextIdApplicationContextInitializer) +spring.application.index=Cache.v1.1 +spring.application.name=Cache Boot + +#Server +server.port=80 + +#LOG +logging.config=classpath:log4j2.xml + +# REDIS (RedisProperties) +spring.redis.database=4 +spring.redis.host=127.0.0.1 +spring.redis.password=123456 +spring.redis.pool.max-active=8 +spring.redis.pool.max-idle=8 +spring.redis.pool.max-wait=-1 +spring.redis.pool.min-idle=0 +spring.redis.port=6379 +spring.redis.timeout=0 diff --git a/spring-boot-cache-redis/src/main/resources/log4j2.xml b/spring-boot-cache-redis/src/main/resources/log4j2.xml new file mode 100644 index 0000000..00314e0 --- /dev/null +++ b/spring-boot-cache-redis/src/main/resources/log4j2.xml @@ -0,0 +1,61 @@ + + + + + /tmp/logs + /tmp/logs/7z + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-cache-redis/src/main/webapp/WEB-INF/web.xml b/spring-boot-cache-redis/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/spring-boot-cache-redis/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/spring-boot-cache-redis/src/main/webapp/index.jsp b/spring-boot-cache-redis/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/spring-boot-cache-redis/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ +