🎨 取消自动备份,新增定时更新文章访问量到数据库

pull/69/head
ruibaby 2018-12-05 11:26:43 +08:00
parent cc92bc62de
commit 82ce86311f
11 changed files with 136 additions and 107 deletions

View File

@ -1,9 +1,8 @@
package cc.ryanc.halo.config;
package cc.ryanc.halo.listener;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.Theme;
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
import cc.ryanc.halo.model.enums.TrueFalseEnum;
import cc.ryanc.halo.service.OptionsService;
import cc.ryanc.halo.utils.HaloUtils;
import cc.ryanc.halo.web.controller.core.BaseController;
@ -22,15 +21,15 @@ import java.util.Map;
/**
* <pre>
*
*
* </pre>
*
* @author : RYAN0UP
* @date : 2017/12/22
* @date : 2018/12/5
*/
@Slf4j
@Configuration
public class StartupConfig implements ApplicationListener<ApplicationStartedEvent> {
public class StartedListener implements ApplicationListener<ApplicationStartedEvent> {
@Autowired
private OptionsService optionsService;
@ -48,7 +47,9 @@ public class StartupConfig implements ApplicationListener<ApplicationStartedEven
this.loadOptions();
this.loadThemes();
this.loadOwo();
this.autoBackup();
//启动定时任务
CronUtil.start();
log.info("The scheduled task starts successfully!");
}
/**
@ -86,18 +87,6 @@ public class StartupConfig implements ApplicationListener<ApplicationStartedEven
}
}
/**
*
*/
private void autoBackup() {
String autoBackup = optionsService.findOneOption(BlogPropertiesEnum.AUTO_BACKUP.getProp());
if (StrUtil.isNotEmpty(autoBackup) && StrUtil.equals(autoBackup, TrueFalseEnum.TRUE.getDesc())) {
//启动定时任务
CronUtil.start();
log.info("The scheduled task starts successfully!");
}
}
/**
* OwO
*/

View File

@ -34,4 +34,9 @@ public class HaloConst {
* user_session
*/
public static String USER_SESSION_KEY = "user_session";
/**
*
*/
public static Map<Long,Long> POSTS_VIEWS = new HashMap<>();
}

View File

@ -275,4 +275,11 @@ public interface PostService {
* @return String
*/
String buildSiteMap(List<Post> posts);
/**
*
*
* @param postId postId
*/
void cacheViews(Long postId);
}

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.model.domain.Category;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.enums.PostStatusEnum;
import cc.ryanc.halo.model.enums.PostTypeEnum;
import cc.ryanc.halo.repository.PostRepository;
@ -464,4 +465,18 @@ public class PostServiceImpl implements PostService {
public String buildSiteMap(List<Post> posts) {
return HaloUtils.getSiteMap(posts);
}
/**
*
*
* @param postId postId
*/
@Override
public void cacheViews(Long postId) {
if (null != HaloConst.POSTS_VIEWS.get(postId)) {
HaloConst.POSTS_VIEWS.put(postId, HaloConst.POSTS_VIEWS.get(postId) + 1);
} else {
HaloConst.POSTS_VIEWS.put(postId, 1L);
}
}
}

View File

@ -0,0 +1,34 @@
package cc.ryanc.halo.task;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
/**
* @author : RYAN0UP
* @date : 2018/12/5
*/
@Slf4j
public class PostSyncTask {
/**
*
*/
public void postSync() {
PostService postService = SpringUtil.getBean(PostService.class);
Post post = null;
int count = 0;
for (Long key : HaloConst.POSTS_VIEWS.keySet()) {
post = postService.findByPostId(key).orElse(null);
if (null != post) {
post.setPostViews(post.getPostViews() + HaloConst.POSTS_VIEWS.get(key));
postService.saveByPost(post);
count++;
}
}
log.info("The number of visits to {} posts has been updated", count);
HaloConst.POSTS_VIEWS.clear();
}
}

View File

@ -0,0 +1,52 @@
package cc.ryanc.halo.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @author : RYAN0UP
* @date : 2018/12/5
*/
@Component
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* applicationContext
*
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
}
/**
* name Bean.
*
* @param name name
* @return Object
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* classBean
*
* @param clazz clazz
* @param <T> <T>
* @return T
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
}

View File

@ -7,7 +7,6 @@ import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.*;
import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.service.OptionsService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.HaloUtils;
import cc.ryanc.halo.utils.LocaleMessageUtil;
@ -15,15 +14,15 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.cron.CronUtil;
import freemarker.template.Configuration;
import freemarker.template.TemplateModelException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.io.File;
@ -54,12 +53,6 @@ public class BackupController {
@Autowired
private LocaleMessageUtil localeMessageUtil;
@Autowired
private OptionsService optionsService;
@Autowired
private Configuration configuration;
/**
*
*
@ -197,33 +190,6 @@ public class BackupController {
}
}
/**
*
*
* @param autoBackup autoBackup
* @return /admin/backup
*/
@PostMapping(value = "backupOption")
public String backupOption(@RequestParam("auto_backup") String autoBackup) throws TemplateModelException {
if (StrUtil.equals(autoBackup, TrueFalseEnum.TRUE.getDesc())) {
if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.AUTO_BACKUP.getProp()), TrueFalseEnum.FALSE.getDesc())) {
CronUtil.start();
log.info("The scheduled task starts successfully!");
}
optionsService.saveOption("auto_backup", TrueFalseEnum.TRUE.getDesc());
} else {
if (StrUtil.equals(HaloConst.OPTIONS.get(BlogPropertiesEnum.AUTO_BACKUP.getProp()), TrueFalseEnum.TRUE.getDesc())) {
CronUtil.stop();
log.info("The scheduled task stops successfully!");
}
optionsService.saveOption("auto_backup", TrueFalseEnum.FALSE.getDesc());
}
configuration.setSharedVariable("options", optionsService.findAllOptions());
HaloConst.OPTIONS.clear();
HaloConst.OPTIONS = optionsService.findAllOptions();
return "redirect:/admin/backup";
}
/**
*
*

View File

@ -161,7 +161,7 @@ public class FrontArchiveController extends BaseController {
model.addAttribute("commentsCount", comments.size());
model.addAttribute("rainbow", rainbow);
model.addAttribute("tagWords", CollUtil.join(tagWords, ","));
postService.updatePostView(post);
postService.cacheViews(post.getPostId());
return this.render("post");
}
}

View File

@ -102,7 +102,7 @@ public class FrontPageController extends BaseController {
model.addAttribute("comments", commentsPage);
model.addAttribute("commentsCount", comments.size());
model.addAttribute("rainbow", rainbow);
postService.updatePostView(post);
postService.cacheViews(post.getPostId());
//如果设置了自定义模板,则渲染自定义模板
if (StrUtil.isNotEmpty(post.getCustomTpl())) {

View File

@ -1,4 +1 @@
[cc.ryanc.halo.web.controller.admin]
BackupController.backupResources = 0 0 1 * * ?
BackupController.backupDatabase = 0 0 2 * * ?
BackupController.backupPosts = 0 0 3 * * ?
cc.ryanc.halo.task.PostSyncTask.postSync = 0 0 * * * ?

View File

@ -7,9 +7,6 @@
</style>
<section class="content-header" id="animated-header">
<h1 style="display: inline-block;"><@spring.message code='admin.backup.title' /></h1>
<a class="btn-header" id="btnBackupOption" href="#">
<@spring.message code='admin.backup.text.setting' />
</a>
<ol class="breadcrumb">
<li>
<a href="/admin"><i class="fa fa-dashboard"></i> <@spring.message code='admin.index.bread.index' /></a>
@ -20,39 +17,6 @@
</section>
<section class="content container-fluid" id="animated-content">
<div class="row">
<div class="col-lg-12 col-xs-12" id="backupOptionsPanel" style="display: none">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title"><@spring.message code='admin.backup.text.setting' /></h3>
</div>
<form class="form-horizontal" id="backupOption" method="post" action="/admin/backup/backupOption">
<div class="box-body">
<div class="col-sm-6 col-xs-6">
<div class="form-group">
<label for="autoBackup" class="col-sm-4 control-label"><@spring.message code='admin.backup.form.auto-backup' /></label>
<div class="col-sm-8 control-radio">
<div class="pretty p-default p-round">
<input type="radio" name="auto_backup" id="autoBackup" value="true" ${((options.auto_backup!)=='true')?string('checked','')}>
<div class="state p-primary">
<label><@spring.message code='common.radio.enable' /></label>
</div>
</div>
<div class="pretty p-default p-round">
<input type="radio" name="auto_backup" id="autoBackup" value="false" ${((options.auto_backup!'false')=='false')?string('checked','')}>
<div class="state p-primary">
<label><@spring.message code='common.radio.disable' /></label>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary pull-right" ><@spring.message code='common.btn.save' /></button>
</div>
</form>
</div>
</div>
<div class="col-xs-12">
<ul style="list-style: none;padding-left: 0">
<li class="resourceType">