Skip to content

Commit

Permalink
add spring-boot-cache-redis project
Browse files Browse the repository at this point in the history
add spring-boot-cache-redis project
  • Loading branch information
leelance committed Jul 2, 2016
1 parent ee29a3d commit 1fe832d
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> list = Arrays.asList("北京市", "上海市", "天津市", "重庆市", "河北省", "山西省", "内蒙古自治区", "辽宁省",
"吉林省", "黑龙江", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西自治区", "海南省", "四川省",
"贵州省", "云南省", "西藏自治区", "陕西省", "甘肃省", "青海省", "宁夏自治区", "新疆自治区", "香港特别行政区", "澳门特别行政区", "台湾省");
}
20 changes: 20 additions & 0 deletions spring-boot-cache-redis/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions spring-boot-cache-redis/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="off">
<!-- 日志文件目录和压缩文件 -->
<Properties>
<Property name="fileName">/tmp/logs</Property>
<Property name="fileGz">/tmp/logs/7z</Property>
</Properties>
<Appenders>
<!--这个输出控制台的配置 -->
<Console name="console" target="SYSTEM_OUT">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="NEUTRAL" />
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="NEUTRAL" />
<!--输出日志的格式 -->
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} %L %M - %msg%xEx%n" />
</Console>

<!--这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
<RollingRandomAccessFile name="rollingInfoFile" fileName="${fileName}/springboot-info.log" immediateFlush="false"
filePattern="${fileGz}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.springboot-info.gz">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} [%t] %-5level %logger{36} %L %M - %msg%xEx%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<Filters>
<!-- 只记录info级别信息 -->
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
</Filters>
<!-- 指定每天的最大压缩包个数,默认7个,超过了会覆盖之前的 -->
<DefaultRolloverStrategy max="50"/>
</RollingRandomAccessFile>


<RollingRandomAccessFile name="rollingErrorFile" fileName="${fileName}springboot-error.log" immediateFlush="false"
filePattern="${fileGz}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.springboot-error.gz">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} [%t] %-5level %logger{36} %L %M - %msg%xEx%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<Filters>
<!-- 只记录error级别信息 -->
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />
</Filters>
<!-- 指定每天的最大压缩包个数,默认7个,超过了会覆盖之前的 -->
<DefaultRolloverStrategy max="50"/>
</RollingRandomAccessFile>

</Appenders>

<Loggers>
<!-- 全局配置,默认所有的Logger都继承此配置 -->
<AsyncRoot level="info" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="rollingInfoFile"/>
<AppenderRef ref="rollingErrorFile"/>
</AsyncRoot>
</Loggers>
</Configuration>
7 changes: 7 additions & 0 deletions spring-boot-cache-redis/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
5 changes: 5 additions & 0 deletions spring-boot-cache-redis/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

0 comments on commit 1fe832d

Please sign in to comment.