CMS 给 Category 栏目添加缓存

This commit is contained in:
thinkgem
2025-10-10 16:45:13 +08:00
parent d981e643f4
commit 07b24e3685
5 changed files with 102 additions and 83 deletions

View File

@@ -31,8 +31,6 @@ public class CategoryService extends TreeService<CategoryDao, Category> {
/**
* 获取单条数据
* @param category
* @return
*/
@Override
public Category get(Category category) {
@@ -50,8 +48,6 @@ public class CategoryService extends TreeService<CategoryDao, Category> {
/**
* 查询列表数据
* @param category
* @return
*/
@Override
public List<Category> findList(Category category) {
@@ -60,19 +56,15 @@ public class CategoryService extends TreeService<CategoryDao, Category> {
/**
* 保存数据(插入或更新)
* @param category
*/
@Override
@Transactional
public void save(Category category) {
super.save(category);
CmsUtils.removeCache("mainNavList_"+category.getSite().getId());
// 保存上传图片
FileUploadUtils.saveFileUpload(category, category.getId(), "category_image");
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(category);
}
// 清理栏目缓存
clearCache(category);
}
/**
@@ -87,27 +79,37 @@ public class CategoryService extends TreeService<CategoryDao, Category> {
/**
* 更新状态
* @param category
*/
@Override
@Transactional
public void updateStatus(Category category) {
super.updateStatus(category);
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(category);
}
// 清理栏目缓存
clearCache(category);
}
/**
* 删除数据
* @param category
*/
@Override
@Transactional
public void delete(Category category) {
category.sqlMap().markIdDelete();
super.delete(category);
// 清理栏目缓存
clearCache(category);
}
/**
* 清理栏目缓存
*/
public void clearCache(Category category) {
// 清理栏目缓存
CmsUtils.removeCache("category_" + category.getId());
// 清理栏目列表缓存
CmsUtils.removeCacheByKeyPrefix("categoryList_" + category.getSite().getId() + "_" + category.getParentCode() + "_");
// 清理主导航缓存
CmsUtils.removeCache("mainNavList_" + category.getSite().getId());
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(category);

View File

@@ -32,7 +32,6 @@ public class SiteService extends CrudService<SiteDao, Site> {
/**
* 获取单条数据
* @param site
* @return
*/
@Override
public Site get(Site site) {
@@ -43,7 +42,6 @@ public class SiteService extends CrudService<SiteDao, Site> {
* 查询分页数据
* @param site 查询条件
* @param site page 分页对象
* @return
*/
@Override
public Page<Site> findPage(Site site) {
@@ -52,19 +50,14 @@ public class SiteService extends CrudService<SiteDao, Site> {
/**
* 保存数据(插入或更新)
* @param site
*/
@Override
@Transactional
public void save(Site site) {
super.save(site);
CmsUtils.removeCache("siteList");
// 保存logo
FileUploadUtils.saveFileUpload(site, site.getId(), "site_logo");
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(site);
}
// 清理站点缓存
clearCache(site);
}
/**
@@ -75,11 +68,8 @@ public class SiteService extends CrudService<SiteDao, Site> {
@Transactional
public void updateStatus(Site site) {
super.updateStatus(site);
CmsUtils.removeCache("siteList");
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(site);
}
// 清理站点缓存
clearCache(site);
}
/**
@@ -91,11 +81,8 @@ public class SiteService extends CrudService<SiteDao, Site> {
public void delete(Site site) {
site.sqlMap().markIdDelete();
super.delete(site);
CmsUtils.removeCache("siteList");
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(site);
}
// 清理站点缓存
clearCache(site);
}
// /**
@@ -110,6 +97,26 @@ public class SiteService extends CrudService<SiteDao, Site> {
// CmsUtils.removeCache("siteList");
// }
/**
* 清理站点缓存
*/
public void clearCache(Site site) {
// 清理栏目缓存
CmsUtils.removeCacheByKeyPrefix("category_");
// 清理栏目列表缓存
CmsUtils.removeCacheByKeyPrefix("categoryList_" + site.getId() + "_");
// 清理主导航缓存
CmsUtils.removeCache("mainNavList_" + site.getId());
// 清理站点缓存
CmsUtils.removeCache("site_" + site.getId());
// 清理站点列表缓存
CmsUtils.removeCache("siteList");
// 清理首页、栏目和文章页面缓存
if (pageCacheService != null) {
pageCacheService.clearCache(site);
}
}
/**
* 重建索引
* @author ThinkGem

View File

@@ -5,6 +5,7 @@
package com.jeesite.modules.cms.utils;
import com.jeesite.common.cache.CacheUtils;
import com.jeesite.common.codec.Md5Utils;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
@@ -52,17 +53,14 @@ public class CmsUtils {
* @param siteCode 站点编号
*/
public static Site getSite(String siteCode) {
String code = Site.MAIN_SITE_CODE;
if (StringUtils.isNotBlank(siteCode)) {
code = siteCode;
}
// 根据编码获取站点
for (Site site : getSiteList()) {
if (site.getSiteCode().equals(code)) {
return site;
String code = StringUtils.isNotBlank(siteCode) ? siteCode : Site.MAIN_SITE_CODE;
return CmsUtils.computeIfAbsentCache("site_" + code, k -> {
Site site = Static.siteService.get(code);
if (site == null) {
site = new Site(code);
}
}
return new Site(code);
return site;
});
}
/**
@@ -92,7 +90,8 @@ public class CmsUtils {
* @param categoryCode 栏目编号
*/
public static Category getCategory(String categoryCode) {
return Static.categoryService.get(categoryCode);
return CmsUtils.computeIfAbsentCache("category_" + categoryCode, k ->
Static.categoryService.get(categoryCode));
}
/**
@@ -113,46 +112,49 @@ public class CmsUtils {
if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(parentCode)) {
return ListUtils.newArrayList();
}
Page<Category> page = new Page<>(1, number, -1);
Category category = new Category();
category.setSite(new Site(siteCode));
category.setParentCode(parentCode);
Boolean isChildList = false; // 是否进行childList转换
if (StringUtils.isNotBlank(params)) {
@SuppressWarnings({ "rawtypes" })
Map map = JsonMapper.fromJson("{" + params.trim() + "}", Map.class);
String key = "categoryList_" + siteCode + "_" + parentCode + "_" + Md5Utils.md5(number + "_" + params);
return CmsUtils.computeIfAbsentCache(key, k -> {
Page<Category> page = new Page<>(1, number, -1);
Category category = new Category();
category.setSite(new Site(siteCode));
category.setParentCode(parentCode);
boolean isChildList = false; // 是否进行childList转换
if (StringUtils.isNotBlank(params)) {
@SuppressWarnings({ "rawtypes" })
Map map = JsonMapper.fromJson("{" + params.trim() + "}", Map.class);
// 获取的层级级别
String sortGrades = ObjectUtils.toString(map.get("sortGrades"));
if (StringUtils.isNotBlank(sortGrades)) {
// 获取的层级级别
String sortGrades = ObjectUtils.toString(map.get("sortGrades"));
if (StringUtils.isNotBlank(sortGrades)) {
// 如果设置了级别则清理ParentCode并使用ParentCodes进行查询
category.setParentCode(null);
// 如果设置了级别则清理ParentCode并使用ParentCodes进行查询
category.setParentCode(null);
// 如果是跟节点则不加入条件,代表查询全部,不是跟节点的时候获取指定节点的所有下级
if (!Category.ROOT_CODE.equals(parentCode)) {
category.setParentCodes("%," + parentCode + ",%");
// 如果是跟节点则不加入条件,代表查询全部,不是跟节点的时候获取指定节点的所有下级
if (!Category.ROOT_CODE.equals(parentCode)) {
category.setParentCodes("%," + parentCode + ",%");
}
// 增加获取层次级别条件
List<Integer> sortGradeList = ListUtils.newArrayList();
for (String s : StringUtils.splitComma(sortGrades)) {
sortGradeList.add(ObjectUtils.toInteger(s));
}
category.setSortGradeList(sortGradeList);
}
// 增加获取层次级别条件
List<Integer> sortGradeList = ListUtils.newArrayList();
for (String s : StringUtils.splitComma(sortGrades)) {
sortGradeList.add(ObjectUtils.toInteger(s));
}
category.setSortGradeList(sortGradeList);
// 是否进行childList转换
isChildList = ObjectUtils.toBoolean(map.get("isChildList"));
}
// 是否进行childList转换
isChildList = ObjectUtils.toBoolean(map.get("isChildList"));
}
category.setPage(page);
page = Static.categoryService.findPage(category);
// 进行childList转换
if (isChildList) {
List<Category> sourceList = page.getList();
List<Category> targetList = Static.categoryService.convertTreeList(sourceList, parentCode);
page.setList(targetList);
}
return page.getList();
category.setPage(page);
page = Static.categoryService.findPage(category);
// 进行childList转换
if (isChildList) {
List<Category> sourceList = page.getList();
List<Category> targetList = Static.categoryService.convertTreeList(sourceList, parentCode);
page.setList(targetList);
}
return page.getList();
});
}
/**
@@ -518,4 +520,12 @@ public class CmsUtils {
CacheUtils.remove(CMS_CACHE, key);
}
/**
* 根据key前缀从缓存中移除
* @param key 缓存键
*/
public static void removeCacheByKeyPrefix(String key) {
CacheUtils.removeByKeyPrefix(CMS_CACHE, key);
}
}

View File

@@ -126,7 +126,7 @@ public class ArticleController extends BaseController {
categoryParam.setSite(new Site(Site.getCurrentSiteCode()));
categoryParam.setParentCode(article.getCategory().getCategoryCode());
List<Category> list = categoryService.findList(categoryParam);
if (list.size() > 0) {
if (!list.isEmpty()) {
article.setCategory(null); // 不允许在父节点上添加文章
} else {
article.setCategory(CmsUtils.getCategory(article.getCategory().getCategoryCode()));

View File

@@ -40,7 +40,7 @@
<label class="control-label col-sm-4" title="">
<span class="required hide">*</span> ${text('站点域名')}<i class="fa icon-question hide"></i></label>
<div class="col-sm-8">
<#form:input path="domain" maxlength="500" class="form-control" placeholder="www.jeesite.com"/>
<#form:input path="domain" maxlength="500" class="form-control" placeholder="https://jeesite.com"/>
</div>
</div>
</div>