mirror of https://github.com/halo-dev/halo
🎨 取消自动备份,新增定时更新文章访问量到数据库
parent
cc92bc62de
commit
82ce86311f
25
src/main/java/cc/ryanc/halo/config/StartupConfig.java → src/main/java/cc/ryanc/halo/listener/StartedListener.java
Executable file → Normal file
25
src/main/java/cc/ryanc/halo/config/StartupConfig.java → src/main/java/cc/ryanc/halo/listener/StartedListener.java
Executable file → Normal 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表情
|
||||
*/
|
|
@ -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<>();
|
||||
}
|
||||
|
|
|
@ -275,4 +275,11 @@ public interface PostService {
|
|||
* @return String
|
||||
*/
|
||||
String buildSiteMap(List<Post> posts);
|
||||
|
||||
/**
|
||||
* 缓存阅读数
|
||||
*
|
||||
* @param postId postId
|
||||
*/
|
||||
void cacheViews(Long postId);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过class获取Bean
|
||||
*
|
||||
* @param clazz clazz
|
||||
* @param <T> <T>
|
||||
* @return T
|
||||
*/
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return getApplicationContext().getBean(clazz);
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
|
||||
/**
|
||||
* 将备份发送到邮箱
|
||||
*
|
||||
|
|
|
@ -78,7 +78,7 @@ public class FrontArchiveController extends BaseController {
|
|||
if (null == posts) {
|
||||
return this.renderNotFound();
|
||||
}
|
||||
model.addAttribute("is_archives",true);
|
||||
model.addAttribute("is_archives", true);
|
||||
model.addAttribute("posts", posts);
|
||||
return this.render("archives");
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ public class FrontArchiveController extends BaseController {
|
|||
if (null == posts) {
|
||||
return this.renderNotFound();
|
||||
}
|
||||
model.addAttribute("is_archives",true);
|
||||
model.addAttribute("is_archives", true);
|
||||
model.addAttribute("posts", posts);
|
||||
return this.render("archives");
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ public class FrontArchiveController extends BaseController {
|
|||
*/
|
||||
@GetMapping(value = "{postUrl}")
|
||||
public String getPost(@PathVariable String postUrl,
|
||||
@RequestParam(value = "cp",defaultValue = "1") Integer cp,
|
||||
@RequestParam(value = "cp", defaultValue = "1") Integer cp,
|
||||
Model model) {
|
||||
Post post = postService.findByPostUrl(postUrl, PostTypeEnum.POST_TYPE_POST.getDesc());
|
||||
if (null == post || !post.getPostStatus().equals(PostStatusEnum.PUBLISHED.getCode())) {
|
||||
|
@ -153,15 +153,15 @@ public class FrontArchiveController extends BaseController {
|
|||
size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()));
|
||||
}
|
||||
//评论分页
|
||||
ListPage<Comment> commentsPage = new ListPage<Comment>(CommentUtil.getComments(comments),cp, size);
|
||||
ListPage<Comment> commentsPage = new ListPage<Comment>(CommentUtil.getComments(comments), cp, size);
|
||||
int[] rainbow = PageUtil.rainbow(cp, commentsPage.getTotalPage(), 3);
|
||||
model.addAttribute("is_post",true);
|
||||
model.addAttribute("is_post", true);
|
||||
model.addAttribute("post", post);
|
||||
model.addAttribute("comments", commentsPage);
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,7 +76,7 @@ public class FrontPageController extends BaseController {
|
|||
*/
|
||||
@GetMapping(value = "/p/{postUrl}")
|
||||
public String getPage(@PathVariable(value = "postUrl") String postUrl,
|
||||
@RequestParam(value = "cp",defaultValue = "1") Integer cp,
|
||||
@RequestParam(value = "cp", defaultValue = "1") Integer cp,
|
||||
Model model) {
|
||||
Post post = postService.findByPostUrl(postUrl, PostTypeEnum.POST_TYPE_PAGE.getDesc());
|
||||
if (null == post) {
|
||||
|
@ -95,17 +95,17 @@ public class FrontPageController extends BaseController {
|
|||
size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_COMMENTS.getProp()));
|
||||
}
|
||||
//评论分页
|
||||
ListPage<Comment> commentsPage = new ListPage<Comment>(CommentUtil.getComments(comments),cp, size);
|
||||
ListPage<Comment> commentsPage = new ListPage<Comment>(CommentUtil.getComments(comments), cp, size);
|
||||
int[] rainbow = PageUtil.rainbow(cp, commentsPage.getTotalPage(), 3);
|
||||
model.addAttribute("is_page",true);
|
||||
model.addAttribute("is_page", true);
|
||||
model.addAttribute("post", post);
|
||||
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())){
|
||||
if (StrUtil.isNotEmpty(post.getCustomTpl())) {
|
||||
return this.render(post.getCustomTpl());
|
||||
}
|
||||
return this.render("page");
|
||||
|
|
|
@ -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 * * * ?
|
|
@ -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">
|
||||
|
|
Loading…
Reference in New Issue