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
| public class StrategyLocal { private LoadingCache<String, String> transactionSessionMapping = CacheBuilder.newBuilder() .expireAfterAccess(3, TimeUnit.SECONDS).build(new CacheLoader<String, String>() { @Override public String load(String key) throws Exception { return "========="; } }); }
@Service public class StrategyLocal { private static final Logger LOG = LoggerFactory.getLogger(StrategyLocal.class); @Autowired private RouteInfoDAO routeInfoDAO; private StrategyInfoDAO strategyInfoDAO; private LoadingCache<Integer, Optional<StrategyInfo>> cache = CacheBuilder.newBuilder().maximumSize(500) .refreshAfterWrite(ReassignConstant.REFRESH_AFTER_WRITE, TimeUnit.SECONDS) .build(new CacheLoader<Integer, Optional<StrategyInfo>>() { @Override public Optional<StrategyInfo> load(Integer cityId) throws Exception { Integer strategyId = routeInfoDAO.getStrategyId(cityId); StrategyInfo strategyInfo = strategyInfoDAO.getStrategy(strategyId); return Optional.ofNullable(strategyInfo); } }); public StrategyInfo getStrategyInfo(int cityId) { try { Optional<StrategyInfo> op = cache.get(cityId); return op.isPresent() ? op.get() : null; } catch (Exception e) { LOG.error("缓存获取改派规则错误!!! cityId={}", cityId, e); } return null; } }
|