feat: 缓存主设备,区域,类型,单位

dev
huangfeng 1 year ago
parent 3f0a92bed4
commit 044c4ae881

@ -6,6 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
@ -15,6 +16,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication @SpringBootApplication
@EnableCaching @EnableCaching
@EnableAsync @EnableAsync
@EnableScheduling
public class CacBackendApplication { public class CacBackendApplication {
@Bean(name = "passwordEncoder") @Bean(name = "passwordEncoder")

@ -8,6 +8,7 @@ import com.xydl.cac.repository.BdzRepository;
import com.xydl.cac.repository.JgRepository; import com.xydl.cac.repository.JgRepository;
import com.xydl.cac.repository.ZsbRepository; import com.xydl.cac.repository.ZsbRepository;
import com.xydl.cac.service.JgService; import com.xydl.cac.service.JgService;
import com.xydl.cac.task.CacheTask;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -32,9 +33,11 @@ public class JgServiceImpl implements JgService {
@Override @Override
public List<Jg> listAll(Integer bdzid) { public List<Jg> listAll(Integer bdzid) {
if (bdzid == null) { if (bdzid == null) {
List<Jg> result = repository.findAll(); if (CacheTask.jg_Cache == null) {
this.fillBdzName(result); CacheTask.jg_Cache = repository.findAll();
return result; this.fillBdzName(CacheTask.jg_Cache);
}
return CacheTask.jg_Cache;
} else { } else {
List<Jg> result = repository.findByBdzid(bdzid); List<Jg> result = repository.findByBdzid(bdzid);
Optional<Bdz> optional = bdzRepository.findById(bdzid); Optional<Bdz> optional = bdzRepository.findById(bdzid);
@ -72,6 +75,7 @@ public class JgServiceImpl implements JgService {
if (!CollectionUtils.isEmpty(list)) { if (!CollectionUtils.isEmpty(list)) {
throw new BusinessException("该名称已被使用"); throw new BusinessException("该名称已被使用");
} }
CacheTask.jg_Cache = null;
return repository.save(item); return repository.save(item);
} }
@ -86,6 +90,7 @@ public class JgServiceImpl implements JgService {
throw new BusinessException("该名称已被使用"); throw new BusinessException("该名称已被使用");
} }
repository.save(item); repository.save(item);
CacheTask.jg_Cache = null;
} }
@Override @Override
@ -95,6 +100,7 @@ public class JgServiceImpl implements JgService {
throw new BusinessException("已被主设备使用不能删除"); throw new BusinessException("已被主设备使用不能删除");
} }
repository.deleteById(id); repository.deleteById(id);
CacheTask.jg_Cache = null;
} }
@Override @Override

@ -7,6 +7,7 @@ import com.xydl.cac.repository.ModevTypePointRepository;
import com.xydl.cac.repository.ModevTypeRepository; import com.xydl.cac.repository.ModevTypeRepository;
import com.xydl.cac.repository.NSensorRepository; import com.xydl.cac.repository.NSensorRepository;
import com.xydl.cac.service.ModevTypeService; import com.xydl.cac.service.ModevTypeService;
import com.xydl.cac.task.CacheTask;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -30,12 +31,15 @@ public class ModevTypeServiceImpl implements ModevTypeService {
@Override @Override
public List<ModevType> listAll() { public List<ModevType> listAll() {
return repository.findAll(); if (CacheTask.modevType_Cache == null) {
CacheTask.modevType_Cache = repository.findAll();
}
return CacheTask.modevType_Cache;
} }
@Override @Override
public List<ModevType> listAllCount() { public List<ModevType> listAllCount() {
List<ModevType> list = repository.findAll(); List<ModevType> list = this.listAll();
for (ModevType modevType : list) { for (ModevType modevType : list) {
long count = sensorRepository.countByTypeId(modevType.getId()); long count = sensorRepository.countByTypeId(modevType.getId());
modevType.setSensorCount(count); modevType.setSensorCount(count);
@ -50,6 +54,7 @@ public class ModevTypeServiceImpl implements ModevTypeService {
if (!CollectionUtils.isEmpty(byMc)) { if (!CollectionUtils.isEmpty(byMc)) {
throw new BusinessException("该监测装置类型已存在"); throw new BusinessException("该监测装置类型已存在");
} }
CacheTask.modevType_Cache = null;
return repository.save(item); return repository.save(item);
} }
@ -64,6 +69,7 @@ public class ModevTypeServiceImpl implements ModevTypeService {
} }
} }
repository.save(item); repository.save(item);
CacheTask.modevType_Cache = null;
} }
@Override @Override
@ -74,6 +80,7 @@ public class ModevTypeServiceImpl implements ModevTypeService {
} }
repository.deleteById(id); repository.deleteById(id);
pointRepository.deleteByModevtypeId(id); pointRepository.deleteByModevtypeId(id);
CacheTask.modevType_Cache = null;
} }
@Override @Override

@ -4,13 +4,13 @@ import com.xydl.cac.entity.Unit;
import com.xydl.cac.exception.BusinessException; import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.repository.UnitRepository; import com.xydl.cac.repository.UnitRepository;
import com.xydl.cac.service.UnitService; import com.xydl.cac.service.UnitService;
import com.xydl.cac.task.CacheTask;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List; import java.util.List;
@Service @Service
@ -21,8 +21,6 @@ public class UnitServiceImpl implements UnitService {
@Resource @Resource
UnitRepository repository; UnitRepository repository;
HashMap<String, String> cache = new HashMap<>();
@Override @Override
public List<Unit> listAll() { public List<Unit> listAll() {
return repository.findAll(); return repository.findAll();
@ -35,30 +33,30 @@ public class UnitServiceImpl implements UnitService {
if (!CollectionUtils.isEmpty(list)) { if (!CollectionUtils.isEmpty(list)) {
throw new BusinessException("该字段已存在"); throw new BusinessException("该字段已存在");
} }
cache.clear(); CacheTask.unit_Cache.clear();
return repository.save(item); return repository.save(item);
} }
@Override @Override
public void update(Unit item) throws Exception { public void update(Unit item) throws Exception {
repository.save(item); repository.save(item);
cache.clear(); CacheTask.unit_Cache.clear();
} }
@Override @Override
public void delete(Integer id) { public void delete(Integer id) {
repository.deleteById(id); repository.deleteById(id);
cache.clear(); CacheTask.unit_Cache.clear();
} }
@Override @Override
public String findUnitInCache(String field) { public String findUnitInCache(String field) {
if (cache.size() < 1) { if (CacheTask.unit_Cache.size() < 1) {
List<Unit> list = repository.findAll(); List<Unit> list = repository.findAll();
for (Unit item : list) { for (Unit item : list) {
cache.put(item.getField().toLowerCase(), item.getUnit()); CacheTask.unit_Cache.put(item.getField().toLowerCase(), item.getUnit());
} }
} }
return cache.get(field.toLowerCase()); return CacheTask.unit_Cache.get(field.toLowerCase());
} }
} }

@ -10,6 +10,7 @@ import com.xydl.cac.repository.ModevRepository;
import com.xydl.cac.repository.ZsbRepository; import com.xydl.cac.repository.ZsbRepository;
import com.xydl.cac.service.JgService; import com.xydl.cac.service.JgService;
import com.xydl.cac.service.ZsbService; import com.xydl.cac.service.ZsbService;
import com.xydl.cac.task.CacheTask;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -36,10 +37,12 @@ public class ZsbServiceImpl implements ZsbService {
@Override @Override
public List<Zsb> listAll(Integer jgid) throws Exception { public List<Zsb> listAll(Integer jgid) throws Exception {
if (jgid == null) { if (jgid == null) {
List<Zsb> result = repository.findAll(); if (CacheTask.zsb_Cache == null) {
this.fillJgBdzName(result); CacheTask.zsb_Cache = repository.findAll();
this.fillLxName(result); this.fillJgBdzName(CacheTask.zsb_Cache);
return result; this.fillLxName(CacheTask.zsb_Cache);
}
return CacheTask.zsb_Cache;
} else { } else {
List<Zsb> result = repository.findByJgid(jgid); List<Zsb> result = repository.findByJgid(jgid);
Jg jg = jgService.detail(jgid); Jg jg = jgService.detail(jgid);
@ -96,6 +99,7 @@ public class ZsbServiceImpl implements ZsbService {
if (!CollectionUtils.isEmpty(list)) { if (!CollectionUtils.isEmpty(list)) {
throw new BusinessException("该名称已被使用"); throw new BusinessException("该名称已被使用");
} }
CacheTask.zsb_Cache = null;
return repository.save(item); return repository.save(item);
} }
@ -112,6 +116,7 @@ public class ZsbServiceImpl implements ZsbService {
throw new BusinessException("该名称已被使用"); throw new BusinessException("该名称已被使用");
} }
repository.save(item); repository.save(item);
CacheTask.zsb_Cache = null;
} }
@Override @Override
@ -121,6 +126,7 @@ public class ZsbServiceImpl implements ZsbService {
throw new BusinessException("已被监测装置使用不能删除"); throw new BusinessException("已被监测装置使用不能删除");
} }
repository.deleteById(id); repository.deleteById(id);
CacheTask.zsb_Cache = null;
} }
@Override @Override

@ -0,0 +1,29 @@
package com.xydl.cac.task;
import com.xydl.cac.entity.Jg;
import com.xydl.cac.entity.ModevType;
import com.xydl.cac.entity.Zsb;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
@Service
@Slf4j
public class CacheTask {
public static HashMap<String, String> unit_Cache = new HashMap<>();
public static List<ModevType> modevType_Cache = null;
public static List<Jg> jg_Cache = null;
public static List<Zsb> zsb_Cache = null;
@Scheduled(cron = "0 1 * * * ?")
private void clearCache() {
unit_Cache.clear();
modevType_Cache = null;
jg_Cache = null;
zsb_Cache = null;
}
}
Loading…
Cancel
Save