Forráskód Böngészése

优化接口使用缓存

xiewenjie 3 éve
szülő
commit
10cdc7fc6c

+ 7 - 0
sxz-core/src/main/java/com/fdkk/sxz/webApi/service/IRenovationPartsColorService.java

@@ -7,4 +7,11 @@ import com.fdkk.sxz.entity.RenovationPartsColorEntity;
  * Created by Hb_zzZ on 2020/11/12.
  */
 public interface IRenovationPartsColorService extends IBaseService<RenovationPartsColorEntity> {
+    
+    RenovationPartsColorEntity findById(Long id);
+
+    @Override
+    boolean updateById(RenovationPartsColorEntity entity);
+
+    boolean removeById(Long id);
 }

+ 39 - 2
sxz-core/src/main/java/com/fdkk/sxz/webApi/service/impl/RenovationPartsColorServiceImpl.java

@@ -1,9 +1,13 @@
 package com.fdkk.sxz.webApi.service.impl;
 
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.ObjectUtil;
 import com.fdkk.sxz.base.impl.BaseServiceImpl;
 import com.fdkk.sxz.entity.RenovationPartsColorEntity;
 import com.fdkk.sxz.webApi.mapper.IRenovationPartsColorMapper;
 import com.fdkk.sxz.webApi.service.IRenovationPartsColorService;
+import net.oschina.j2cache.CacheChannel;
+import net.oschina.j2cache.CacheObject;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -13,9 +17,42 @@ import org.springframework.transaction.annotation.Transactional;
  */
 @Service
 @Transactional
-public class RenovationPartsColorServiceImpl extends BaseServiceImpl<IRenovationPartsColorMapper , RenovationPartsColorEntity> implements IRenovationPartsColorService{
+public class RenovationPartsColorServiceImpl extends BaseServiceImpl<IRenovationPartsColorMapper, RenovationPartsColorEntity> implements IRenovationPartsColorService {
 
+    private String region = "RenovationPartsColorEntity";
     @Autowired
-    private IRenovationPartsColorMapper mapper;
+    private CacheChannel cacheChannel;
 
+    @Override
+    public RenovationPartsColorEntity findById(Long id) {
+        RenovationPartsColorEntity entity = null;
+        if (cacheChannel.exists(region, "id::" + id)) {
+            CacheObject cache = cacheChannel.get(region, "id::" + id);
+            entity = BeanUtil.toBean(cache.getValue(), RenovationPartsColorEntity.class);
+        } else {
+            entity = super.findById(entity);
+            if (ObjectUtil.isNotNull(entity)) {
+                cacheChannel.set(region, "id::" + entity.getId(), entity, 60 * 30);
+            }
+        }
+        return entity;
+    }
+
+    @Override
+    public boolean updateById(RenovationPartsColorEntity entity) {
+        boolean updateById = super.updateById(entity);
+        if (updateById) {
+            cacheChannel.set(region, "id::" + entity.getId(), entity, 60 * 30);
+        }
+        return updateById;
+    }
+
+    @Override
+    public boolean removeById(Long id) {
+        boolean removeById = super.removeById(id);
+        if (removeById) {
+            cacheChannel.evict(region, "id::" + id);
+        }
+        return removeById;
+    }
 }