209 lines
6.0 KiB
Java
209 lines
6.0 KiB
Java
|
|
package com.nis.web.service.configuration;
|
|||
|
|
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.concurrent.ExecutorService;
|
|||
|
|
import java.util.concurrent.Executors;
|
|||
|
|
|
|||
|
|
import javax.annotation.Resource;
|
|||
|
|
|
|||
|
|
import org.apache.log4j.Logger;
|
|||
|
|
import org.crazycake.shiro.RedisManager;
|
|||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
import org.springframework.stereotype.Service;
|
|||
|
|
import com.nis.domain.configuration.HelpInfo;
|
|||
|
|
import com.nis.util.StringUtils;
|
|||
|
|
import com.nis.util.redis.RedisPoolHelper;
|
|||
|
|
import com.nis.util.redis.SaveHelpInfoThread;
|
|||
|
|
import com.nis.web.dao.configuration.HelpInfoDao;
|
|||
|
|
import com.nis.web.service.SpringContextHolder;
|
|||
|
|
|
|||
|
|
import redis.clients.jedis.Jedis;
|
|||
|
|
import redis.clients.jedis.JedisPool;
|
|||
|
|
import redis.clients.jedis.JedisPoolConfig;
|
|||
|
|
|
|||
|
|
@Service
|
|||
|
|
public class HelpInfoService{
|
|||
|
|
@Autowired
|
|||
|
|
private HelpInfoDao dao;
|
|||
|
|
private JedisPool jedisPool;
|
|||
|
|
private Logger logger=Logger.getLogger(HelpInfoService.class);
|
|||
|
|
private ExecutorService pool = Executors.newFixedThreadPool(5);
|
|||
|
|
private final String HELP_CACHE_PRE="help:cache:";//帮助文档redis key的前缀
|
|||
|
|
private final String BACK_HELP_CACHE_PRE="help:cache:back:"; //帮助文档上一版redis key的前缀
|
|||
|
|
private final String HELP_CACHE_FLAG="help:cache:cacheFlag";//是否已经缓存的标识
|
|||
|
|
public HelpInfoService(){
|
|||
|
|
jedisPool=new RedisPoolHelper().getJedisPool();
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* 通过文件名称查询文件内容
|
|||
|
|
* @param fileName
|
|||
|
|
* @return
|
|||
|
|
* @throws Exception
|
|||
|
|
*/
|
|||
|
|
public HelpInfo findHelpCommentByName(String fileName){
|
|||
|
|
HelpInfo helpInfo = getCacheHelpInfo(fileName);
|
|||
|
|
if(helpInfo==null){
|
|||
|
|
List<HelpInfo> list = dao.findComment(fileName);
|
|||
|
|
if(list!=null&&list.size()>0){
|
|||
|
|
helpInfo= list.get(0);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return helpInfo;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 查询所有的帮助文档信息
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public List<HelpInfo> findHelpComments(){
|
|||
|
|
List<HelpInfo> list = dao.findComment(null);
|
|||
|
|
return list;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 查询某个文档的备份内容
|
|||
|
|
* @param fileName
|
|||
|
|
* @return
|
|||
|
|
* @throws Exception
|
|||
|
|
*/
|
|||
|
|
public HelpInfo findHelpBakCommentByName(String fileName){
|
|||
|
|
HelpInfo helpInfo = getCacheBakHelpInfo(fileName);
|
|||
|
|
if(helpInfo==null){
|
|||
|
|
List<HelpInfo> list = dao.findBakComment(fileName);
|
|||
|
|
if(list!=null&&list.size()>0){
|
|||
|
|
helpInfo= list.get(0);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return helpInfo;
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* 查询所有文档的备份内容
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public List<HelpInfo> findHelpBakComments(){
|
|||
|
|
List<HelpInfo> list = dao.findBakComment(null);
|
|||
|
|
return list;
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* 从数据库获取所有的帮助文档信息
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public List<HelpInfo> findAllHelpComments(){
|
|||
|
|
List<HelpInfo> list = dao.findAllComment();
|
|||
|
|
return list;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void saveHelpInfo(HelpInfo helpInfo){
|
|||
|
|
|
|||
|
|
Jedis jedis=jedisPool.getResource();
|
|||
|
|
try {
|
|||
|
|
if(jedis!=null){
|
|||
|
|
String fileName=helpInfo.getFileName();
|
|||
|
|
String oldComment=jedis.get(HELP_CACHE_PRE+fileName);
|
|||
|
|
if(StringUtils.strIsBlank(oldComment)){//redis宕机等意外原因导致获取不到信息,从数据库中读取
|
|||
|
|
List<HelpInfo> list = dao.findComment(fileName);
|
|||
|
|
if(list!=null&&list.size()>0){
|
|||
|
|
oldComment=list.get(0).getFileComment();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
pool.execute(new SaveHelpInfoThread(jedisPool,HELP_CACHE_PRE+fileName, helpInfo.getFileComment(), -1));
|
|||
|
|
pool.execute(new SaveHelpInfoThread(jedisPool,BACK_HELP_CACHE_PRE+fileName, oldComment, -1));
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
logger.error("save help info to cache error",e);
|
|||
|
|
}finally{
|
|||
|
|
jedis.close();
|
|||
|
|
}
|
|||
|
|
dao.saveHelpInfo(helpInfo);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 缓存帮助文档
|
|||
|
|
* @throws Exception
|
|||
|
|
*/
|
|||
|
|
public void cacheAllHelpInfo() {
|
|||
|
|
Jedis jedis=jedisPool.getResource();
|
|||
|
|
if(jedis==null) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
try {
|
|||
|
|
String cacheFlag = jedis.get(HELP_CACHE_FLAG);
|
|||
|
|
if(StringUtils.strIsBlank(cacheFlag)||!cacheFlag.equals("true")){
|
|||
|
|
logger.info("start cache help document");
|
|||
|
|
long startTime=System.currentTimeMillis();
|
|||
|
|
List<HelpInfo> helpInfos = findAllHelpComments();
|
|||
|
|
for (HelpInfo helpInfo : helpInfos) {
|
|||
|
|
String helpFileName=helpInfo.getFileName();
|
|||
|
|
String key=HELP_CACHE_PRE+helpFileName;
|
|||
|
|
String backKey=BACK_HELP_CACHE_PRE+helpFileName;
|
|||
|
|
String fileComment = helpInfo.getFileComment();
|
|||
|
|
String backFileComment = helpInfo.getBackFileComment();
|
|||
|
|
if(!StringUtils.strIsBlank(fileComment)){
|
|||
|
|
pool.execute(new SaveHelpInfoThread(jedisPool,key,fileComment , -1));
|
|||
|
|
}
|
|||
|
|
if(!StringUtils.strIsBlank(backFileComment)){
|
|||
|
|
pool.execute(new SaveHelpInfoThread(jedisPool,backKey, helpInfo.getBackFileComment(), -1));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
pool.execute(new SaveHelpInfoThread(jedisPool,HELP_CACHE_FLAG, "true", -1));
|
|||
|
|
long endTime=System.currentTimeMillis();
|
|||
|
|
logger.info("cache help info complate! Time--> "+(endTime-startTime)+"ms");
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
logger.error("cache Help Info error",e);
|
|||
|
|
return ;
|
|||
|
|
}finally{
|
|||
|
|
jedis.close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 从redis缓存中获取帮助文档
|
|||
|
|
* @param fileName
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public HelpInfo getCacheHelpInfo(String fileName){
|
|||
|
|
Jedis jedis=jedisPool.getResource();
|
|||
|
|
if(jedis==null){
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
try {
|
|||
|
|
String comment = jedis.get(HELP_CACHE_PRE+fileName);
|
|||
|
|
HelpInfo helpInfo = new HelpInfo();
|
|||
|
|
helpInfo.setFileComment(comment);
|
|||
|
|
return helpInfo;
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
logger.error("get Help Info error",e);
|
|||
|
|
return null;
|
|||
|
|
}finally{
|
|||
|
|
jedis.close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* 从redis缓存中获取帮助文档备份
|
|||
|
|
* @param fileName
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public HelpInfo getCacheBakHelpInfo(String fileName) {
|
|||
|
|
Jedis jedis=jedisPool.getResource();
|
|||
|
|
if(jedis==null){
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
try {
|
|||
|
|
String backComment = jedis.get(BACK_HELP_CACHE_PRE+fileName);
|
|||
|
|
HelpInfo helpInfo = new HelpInfo();
|
|||
|
|
helpInfo.setBackFileComment(backComment);
|
|||
|
|
return helpInfo;
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
logger.error("get back Help Info error",e);
|
|||
|
|
return null;
|
|||
|
|
}finally{
|
|||
|
|
jedis.close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|