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 DEPART_MAP = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES) .build(new CacheLoader() { @Override public SysDepart load(String orgCode) throws Exception { log.info("重新从数据库获取机构信息,orgCode:{}",orgCode); return sysDepartMapper.selectOne(new QueryWrapper().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; } }