CMS增加页面高速缓存网页静态化缓存

This commit is contained in:
thinkgem
2023-04-10 10:27:13 +08:00
parent 42ee036804
commit 183a4c3694
7 changed files with 142 additions and 24 deletions

View File

@@ -20,4 +20,6 @@ public interface ArticleDao extends CrudDao<Article> {
long updateHitsAddOne(String id);
long getHits(String id);
}

View File

@@ -41,6 +41,8 @@ public class ArticleService extends CrudService<ArticleDao, Article> {
@Autowired
private ArticleDataDao articleDataDao;
@Autowired(required = false)
private PageCacheService pageCacheService;
private static ExecutorService updateExpiredWeightThreadPool = new ThreadPoolExecutor(5, 20,
60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
@@ -158,8 +160,10 @@ public class ArticleService extends CrudService<ArticleDao, Article> {
}
// 保存上传图片
FileUploadUtils.saveFileUpload(article, article.getId(), "article_image");
// // 保存上传附件
// FileUploadUtils.saveFileUpload(article, article.getId(), "article_file");
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(article);
}
}
/**
@@ -170,16 +174,27 @@ public class ArticleService extends CrudService<ArticleDao, Article> {
@Transactional
public void updateStatus(Article article) {
super.updateStatus(article);
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(article);
}
}
/**
* 获取文章获取文章并点击数加一
* 文章点击次数数加一
*/
@Transactional
public void updateHitsAddOne(String id) {
dao.updateHitsAddOne(id);
}
/**
* 获取文章点击次数
*/
public long getHits(String id) {
return dao.getHits(id);
}
/**
* 删除数据
* @param article
@@ -188,6 +203,10 @@ public class ArticleService extends CrudService<ArticleDao, Article> {
@Transactional
public void delete(Article article) {
super.delete(article);
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(article);
}
}
/**

View File

@@ -9,6 +9,7 @@ import com.jeesite.modules.cms.dao.CategoryDao;
import com.jeesite.modules.cms.entity.Category;
import com.jeesite.modules.cms.utils.CmsUtils;
import com.jeesite.modules.file.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -22,6 +23,9 @@ import java.util.List;
@Service
public class CategoryService extends TreeService<CategoryDao, Category> {
@Autowired(required = false)
private PageCacheService pageCacheService;
/**
* 获取单条数据
* @param category
@@ -62,6 +66,10 @@ public class CategoryService extends TreeService<CategoryDao, Category> {
CmsUtils.removeCache("mainNavList_"+category.getSite().getId());
// 保存上传图片
FileUploadUtils.saveFileUpload(category, category.getId(), "category_image");
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(category);
}
}
/**
@@ -82,6 +90,10 @@ public class CategoryService extends TreeService<CategoryDao, Category> {
@Transactional
public void updateStatus(Category category) {
super.updateStatus(category);
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(category);
}
}
/**
@@ -92,6 +104,10 @@ public class CategoryService extends TreeService<CategoryDao, Category> {
@Transactional
public void delete(Category category) {
super.delete(category);
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(category);
}
}
}

View File

@@ -0,0 +1,36 @@
/**
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
* No deletion without permission, or be held responsible to law.
*/
package com.jeesite.modules.cms.service;
import com.jeesite.modules.cms.entity.Article;
import com.jeesite.modules.cms.entity.Category;
import com.jeesite.modules.cms.entity.Site;
/**
* 页面缓存服务接口
* @author ThinkGem
* @version 2023-4-7
*/
public interface PageCacheService {
/**
* 根据文章清理页面缓存
* @author ThinkGem
*/
void clearCache(Article article);
/**
* 根据栏目清理页面缓存
* @author ThinkGem
*/
void clearCache(Category category);
/**
* 根据栏目清理页面缓存
* @author ThinkGem
*/
void clearCache(Site site);
}

View File

@@ -4,6 +4,7 @@
*/
package com.jeesite.modules.cms.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -21,7 +22,10 @@ import com.jeesite.modules.file.utils.FileUploadUtils;
*/
@Service
public class SiteService extends CrudService<SiteDao, Site> {
@Autowired(required = false)
private PageCacheService pageCacheService;
/**
* 获取单条数据
* @param site
@@ -54,6 +58,10 @@ public class SiteService extends CrudService<SiteDao, Site> {
CmsUtils.removeCache("siteList");
// 保存logo
FileUploadUtils.saveFileUpload(site, site.getId(), "site_logo");
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(site);
}
}
/**
@@ -64,6 +72,10 @@ public class SiteService extends CrudService<SiteDao, Site> {
@Transactional
public void updateStatus(Site site) {
super.updateStatus(site);
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(site);
}
}
/**
@@ -74,6 +86,10 @@ public class SiteService extends CrudService<SiteDao, Site> {
@Transactional
public void delete(Site site) {
super.delete(site);
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(site);
}
}
/**

View File

@@ -32,6 +32,8 @@ import java.util.Map;
*/
public class CmsUtils {
private static final String CMS_CACHE = "cmsCache";
private static final class Static {
private static SiteService siteService = SpringUtils.getBean(SiteService.class);
private static CategoryService categoryService = SpringUtils.getBean(CategoryService.class);
@@ -39,8 +41,6 @@ public class CmsUtils {
private static ServletContext context = SpringUtils.getBean(ServletContext.class);
}
private static final String CMS_CACHE = "cmsCache";
/**
* 获得当前站点信息
*/
@@ -124,7 +124,7 @@ public class CmsUtils {
if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(parentCode)) {
return ListUtils.newArrayList();
}
Page<Category> page = new Page<Category>(1, number, -1);
Page<Category> page = new Page<>(1, number, -1);
Category category = new Category();
category.setSite(new Site(siteCode));
category.setParentCode(parentCode);
@@ -236,23 +236,6 @@ public class CmsUtils {
return page.getList();
}
public static <V> V getCache(String key) {
return CacheUtils.get(CMS_CACHE, key);
}
public static <V> V getCache(String key, V defaultValue) {
V value = CacheUtils.get(CMS_CACHE, key);
return value != null ? value : defaultValue;
}
public static void putCache(String key, Object value) {
CacheUtils.put(CMS_CACHE, key, value);
}
public static void removeCache(String key) {
CacheUtils.remove(CMS_CACHE, key);
}
/**
* 获得文章动态URL地址
* @param article
@@ -499,4 +482,37 @@ public class CmsUtils {
}
}
public static SiteService getSiteService() {
return Static.siteService;
}
public static CategoryService getCategoryService() {
return Static.categoryService;
}
public static ArticleService getArticleService() {
return Static.articleService;
}
public static ServletContext getServletContext() {
return Static.context;
}
public static <V> V getCache(String key) {
return CacheUtils.get(CMS_CACHE, key);
}
public static <V> V getCache(String key, V defaultValue) {
V value = CacheUtils.get(CMS_CACHE, key);
return value != null ? value : defaultValue;
}
public static void putCache(String key, Object value) {
CacheUtils.put(CMS_CACHE, key, value);
}
public static void removeCache(String key) {
CacheUtils.remove(CMS_CACHE, key);
}
}

View File

@@ -0,0 +1,13 @@
# 温馨提示不建议直接修改此文件为了平台升级方便建议将需要修改的参数值复制到application.yml里进行覆盖该参数值。
cms:
pageCache:
enabled: true
cacheName: cmsPageCache
urlPatterns: ${frontPath}/*
urlSuffixes: .html
j2cache:
caffeine:
region:
cmsPageCache: 100000, 7d