Skip to content

Commit 4e1a953

Browse files
committed
redis
1 parent 137ba65 commit 4e1a953

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-9
lines changed

redis/src/main/java/info/xiaomo/redis/RedisMain.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
import org.springframework.context.annotation.ComponentScan;
99
import org.springframework.context.annotation.Configuration;
1010
import org.springframework.data.redis.cache.RedisCacheManager;
11-
import org.springframework.data.redis.connection.RedisConnectionFactory;
1211
import org.springframework.data.redis.core.RedisTemplate;
13-
import org.springframework.data.redis.core.StringRedisTemplate;
14-
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
12+
import org.springframework.scheduling.annotation.EnableScheduling;
1513

1614
/**
1715
* 把今天最好的表现当作明天最新的起点..~
@@ -31,6 +29,7 @@
3129
@EnableAutoConfiguration
3230
@ComponentScan("info.xiaomo")
3331
@EnableCaching
32+
@EnableScheduling
3433
public class RedisMain {
3534
public static void main(String[] args) throws Exception {
3635
SpringApplication.run(RedisMain.class, args);
@@ -41,11 +40,11 @@ public CacheManager cacheManager(RedisTemplate redisTemplate) {
4140
return new RedisCacheManager(redisTemplate);
4241
}
4342

44-
@Bean
45-
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
46-
final StringRedisTemplate template = new StringRedisTemplate(factory);
47-
template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); //请注意这里
48-
return template;
49-
}
43+
// @Bean
44+
// public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
45+
// final StringRedisTemplate template = new StringRedisTemplate(factory);
46+
// template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); //请注意这里
47+
// return template;
48+
// }
5049

5150
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package info.xiaomo.redis.job;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import info.xiaomo.redis.model.CityInfo;
5+
import info.xiaomo.redis.service.CityService;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.scheduling.annotation.Scheduled;
10+
import org.springframework.stereotype.Component;
11+
12+
import java.util.Arrays;
13+
import java.util.List;
14+
import java.util.Random;
15+
16+
@Component
17+
public class TaskJob {
18+
19+
private static final Logger LOGGER = LoggerFactory.getLogger(TaskJob.class);
20+
private static final List<String> list = Arrays.asList("北京市", "上海市", "天津市", "重庆市", "河北省", "山西省", "内蒙古自治区", "辽宁省",
21+
"吉林省", "黑龙江", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西自治区", "海南省", "四川省",
22+
"贵州省", "云南省", "西藏自治区", "陕西省", "甘肃省", "青海省", "宁夏自治区", "新疆自治区", "香港特别行政区", "澳门特别行政区", "台湾省");
23+
private final CityService cityService;
24+
25+
@Autowired
26+
public TaskJob(CityService cityService) {
27+
this.cityService = cityService;
28+
}
29+
30+
/**
31+
* Job
32+
*/
33+
@Scheduled(fixedDelay = 500)
34+
public void retrieveCountry() {
35+
int index = new Random().nextInt(list.size());
36+
String city = find(index);
37+
CityInfo info = cityService.getCity(index, city);
38+
LOGGER.debug("{}", JSON.toJSONString(info));
39+
}
40+
41+
private String find(int index) {
42+
return list.get(index);
43+
}
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package info.xiaomo.redis.model;
2+
3+
import java.io.Serializable;
4+
5+
public class CityInfo implements Serializable{
6+
private static final long serialVersionUID = 2845294956907027149L;
7+
8+
private int id;
9+
private String city;
10+
11+
public CityInfo() {
12+
13+
}
14+
15+
public CityInfo(int id, String city) {
16+
this.id = id;
17+
this.city = city;
18+
}
19+
20+
public int getId() {
21+
return id;
22+
}
23+
public void setId(int id) {
24+
this.id = id;
25+
}
26+
public String getCity() {
27+
return city;
28+
}
29+
public void setCity(String city) {
30+
this.city = city;
31+
}
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package info.xiaomo.redis.service;
2+
3+
import info.xiaomo.redis.model.CityInfo;
4+
import org.springframework.cache.annotation.CacheConfig;
5+
import org.springframework.cache.annotation.Cacheable;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
@CacheConfig(cacheNames="CityService")
10+
public class CityService {
11+
12+
@Cacheable
13+
public CityInfo getCity(int id, String city) {
14+
return new CityInfo(id, city);
15+
}
16+
}

0 commit comments

Comments
 (0)