|
@@ -0,0 +1,74 @@
|
|
|
+package com.qmth.exam.reserve.cache.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.qmth.exam.reserve.bean.category.CategoryCacheBean;
|
|
|
+import com.qmth.exam.reserve.cache.CacheConstants;
|
|
|
+import com.qmth.exam.reserve.cache.RedisClient;
|
|
|
+import com.qmth.exam.reserve.entity.CategoryEntity;
|
|
|
+import com.qmth.exam.reserve.service.CategoryService;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class CategoryCacheService implements CacheConstants {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(CategoryCacheService.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisClient redisClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CategoryService categoryService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某个分类缓存
|
|
|
+ */
|
|
|
+ public CategoryCacheBean getCategoryById(Long categoryId) {
|
|
|
+ String cacheKey = String.format(CACHE_CATEGORY, categoryId);
|
|
|
+ CategoryCacheBean value = redisClient.get(cacheKey, CategoryCacheBean.class);
|
|
|
+ if (value != null) {
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ value = this.findInfoById(categoryId);
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ redisClient.set(cacheKey, value, 5, TimeUnit.MINUTES);
|
|
|
+ log.info("{} = id:{} name:{}", cacheKey, value.getId(), value.getName());
|
|
|
+
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void clearCategoryByIdCache(Long orgId) {
|
|
|
+ String cacheKey = String.format(CACHE_CATEGORY, orgId);
|
|
|
+ redisClient.delete(cacheKey);
|
|
|
+ log.warn("清理缓存!cacheKey:{}", cacheKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ private CategoryCacheBean findInfoById(Long categoryId) {
|
|
|
+ LambdaQueryWrapper<CategoryEntity> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.select(CategoryEntity::getId, CategoryEntity::getCode, CategoryEntity::getName,
|
|
|
+ CategoryEntity::getLevel, CategoryEntity::getEnable, CategoryEntity::getParentId);
|
|
|
+ wrapper.eq(CategoryEntity::getId, categoryId);
|
|
|
+ CategoryEntity category = categoryService.getOne(wrapper);
|
|
|
+ if (category == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ CategoryCacheBean bean = new CategoryCacheBean();
|
|
|
+ bean.setId(category.getId());
|
|
|
+ bean.setCode(category.getCode());
|
|
|
+ bean.setName(category.getName());
|
|
|
+ bean.setLevel(category.getLevel());
|
|
|
+ bean.setEnable(category.getEnable());
|
|
|
+ bean.setParentId(category.getParentId());
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|