1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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<String, EmployeeDTO> getEmployees() {
        String redisKey = ExtCacheConstant.CPS_CACHE.concat("::").concat(EMPLOYEE_ALL);
        Map<String, EmployeeDTO> map = BLADE_REDIS.hGetAll(redisKey);
        if (Func.isEmpty(map)) {
            return setEmployees();
        }
        return map;
    }
 
    private static Map<String, EmployeeDTO> setEmployees() {
        List<Employee> 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<Long> workstationIds) {
        List<String> 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);
    }
}