最近做的一个论坛项目发现,热门榜单文章信息和最新发布的文章信息经常需要访问,这样的话,就把它们存储在redis里吧。
先写个redisUitls,这样就可以直接存储java对象,很方便。
redisUitls.java
package com.protal.community.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
public Object get(final String key) {
if (StringUtils.isBlank(key)) {
return null;
}
try {
return redisTemplate.opsForValue().get(key);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean set(final String key, Object value) {
if (StringUtils.isBlank(key)) {
return false;
}
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
RedisConfig配置类
package com.protal.community.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
// 设置序列化
Jackson2JsonRedisSerializer
再写个定时器事件类,设置每隔5分钟更新一次redis中的数据
@Component
public class TimerTask {
@Autowired
private ArticalInformationService articalInformationService;
@Autowired
private RedisUtils redisUtils;
@Async
@Scheduled(fixedRate = 5 * 1000 * 60)
public void updateNewArtical() throws IOException {
List newArtical = articalInformationService.getNewArtical();
redisUtils.set("newArtical",newArtical);
for (ArticalInformation articalInformation : newArtical){
Integer id = articalInformation.getId();
String encoding = "UTF-8";
File file = new File("F:\site_data\artical\" + id.toString() +".txt");
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(filecontent);
String s = new String(filecontent, encoding);
redisUtils.set("newArtical" + id.toString(),s);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
}
}
@Async
@Scheduled(fixedRate = 5 * 1000 * 60)
public void updateHotArtical() throws IOException {
List hotArtical1 = articalInformationService.getHotArtical();
List hotArtical = new ArrayList<>();
for(ArticalVisit articalVisit:hotArtical1){
Integer id = articalVisit.getAid();
ArticalInformation articalInformation = articalInformationService.selectArticalInformationById(id);
hotArtical.add(articalInformation);
String encoding = "UTF-8";
File file = new File("F:\site_data\artical\" + id.toString() +".txt");
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(filecontent);
String s = new String(filecontent, encoding);
redisUtils.set("hotArtical" + id.toString(),s);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
}
redisUtils.set("hotArtical",hotArtical);
//此处欠缺每次获取热榜后对表的操作,写完后边再补全
}
}
好的,大功告成。



