package com.qianwen.smartman.common.cache.cps; import java.util.List; import java.util.Map; import org.apache.commons.compress.utils.Lists; import com.qianwen.smartman.common.constant.ExtCacheConstant; import com.qianwen.core.cache.utils.CacheUtil; import com.qianwen.core.redis.cache.BladeRedis; import com.qianwen.core.tool.utils.Func; import com.qianwen.core.tool.utils.SpringUtil; import com.qianwen.core.tool.utils.StringUtil; import com.qianwen.smartman.modules.smis.convert.EmployeeConvert; import com.qianwen.smartman.modules.smis.dto.EmployeeDTO; import com.qianwen.smartman.modules.smis.entity.Employee; import com.qianwen.smartman.modules.smis.service.IEmployeeService; public class EmployeeCache { private static final BladeRedis BLADE_REDIS = (BladeRedis) SpringUtil.getBean(BladeRedis.class); private static final IEmployeeService EMPLOYEE_SERVICE = (IEmployeeService) SpringUtil.getBean(IEmployeeService.class); private static final String EMPLOYEE_ALL = "employee:all"; private static final String WORK = "workstation:id:{}:employee"; private static final long EMPLOYEE_EXPIRE = 86400; public static Map getEmployees() { String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(EMPLOYEE_ALL); Map map = BLADE_REDIS.hGetAll(redisKey); if (Func.isEmpty(map)) { return setEmployees(); } return map; } private static Map setEmployees() { List list = EMPLOYEE_SERVICE.list(); String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(EMPLOYEE_ALL); if (Func.isNotEmpty(list)) { list.forEach(employee -> { EmployeeDTO employeeDTO = EmployeeConvert.INSTANCE.convertToDTO(employee); BLADE_REDIS.hSet(redisKey, employeeDTO.getId(), employeeDTO); }); BLADE_REDIS.expire(redisKey, 86400L); } return BLADE_REDIS.hGetAll(redisKey); } public static Boolean clearEmployeeCache() { WorkstationCache.clearWorkstationCache(); CacheUtil.clear("blade:sys"); CacheUtil.clear("blade:sys", Boolean.FALSE); return true; } public static void clearPerfCache(List workstationIds) { List keys = Lists.newArrayList(); workstationIds.forEach(wId -> { String redisKey = StringUtil.format(ExtCacheConstant.COLLECT_WORKSTATION.concat("::").concat(WORK), new Object[]{wId}); keys.add(redisKey); }); BLADE_REDIS.del(keys); } }