peng
2026-03-24 73d124ce71e60685b19cc74a44e21dc080f7bfb6
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
package com.tievd.jyz.cache;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.tievd.cube.modules.system.entity.SysDepart;
import com.tievd.cube.modules.system.mapper.SysDepartMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.TimeUnit;
 
/**
 * 机构信息缓存
 * @author timi
 */
@Slf4j
@Component
public class DepartCache {
 
    private static SysDepartMapper sysDepartMapper;
 
    @Autowired
    public void setSysDepartMapper(SysDepartMapper sysDepartMapper){
        DepartCache.sysDepartMapper = sysDepartMapper;
    }
 
    private final static LoadingCache<String, SysDepart> DEPART_MAP = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES)
            .build(new CacheLoader<String, SysDepart>() {
                @Override
                public SysDepart load(String orgCode) throws Exception {
                    log.info("重新从数据库获取机构信息,orgCode:{}",orgCode);
                    return sysDepartMapper.selectOne(new QueryWrapper<SysDepart>().eq("org_code",orgCode));
                }
            });
 
 
    /**
     * 获取机构信息
     * @param orgCode
     * @return
     */
    public static SysDepart getDepartByOrgCode(String orgCode){
        try{
            return DEPART_MAP.get(orgCode);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }
 
}