Esempio n. 1
0
 public void setAutoLoadTO(AutoLoadTO autoLoadTO) {
   if (null == autoLoadMap) {
     return;
   }
   if (autoLoadTO.getExpire() >= 120 && autoLoadMap.size() <= this.config.getMaxElement()) {
     autoLoadMap.put(autoLoadTO.getCacheKey(), autoLoadTO);
   }
 }
Esempio n. 2
0
 /**
  * 重置自动加载时间
  *
  * @param cacheKey 缓存Key
  */
 public void resetAutoLoadLastLoadTime(String cacheKey) {
   AutoLoadTO autoLoadTO = autoLoadMap.get(cacheKey);
   if (null != autoLoadTO && !autoLoadTO.isLoading()) {
     autoLoadTO.setLastLoadTime(1L);
   }
 }
Esempio n. 3
0
 private void loadCache(AutoLoadTO autoLoadTO) {
   if (null == autoLoadTO) {
     return;
   }
   long now = System.currentTimeMillis();
   if (autoLoadTO.getLastRequestTime() <= 0 || autoLoadTO.getLastLoadTime() <= 0) {
     return;
   }
   if (autoLoadTO.getRequestTimeout() > 0
       && (now - autoLoadTO.getLastRequestTime())
           >= autoLoadTO.getRequestTimeout() * 1000) { // 如果超过一定时间没有请求数据,则从队列中删除
     autoLoadMap.remove(autoLoadTO.getCacheKey());
     return;
   }
   if (autoLoadTO.getLoadCnt() > 100
       && autoLoadTO.getAverageUseTime() < 10) { // 如果效率比较高的请求,就没必要使用自动加载了。
     autoLoadMap.remove(autoLoadTO.getCacheKey());
     return;
   }
   // 对于使用频率很低的数据,也可以考虑不用自动加载
   long difFirstRequestTime = now - autoLoadTO.getFirstRequestTime();
   long oneHourSecs = 3600000L;
   if (difFirstRequestTime > oneHourSecs
       && autoLoadTO.getAverageUseTime() < 1000
       && (autoLoadTO.getRequestTimes() / (difFirstRequestTime / oneHourSecs))
           < 60) { // 使用率比较低的数据,没有必要使用自动加载。
     autoLoadMap.remove(autoLoadTO.getCacheKey());
     return;
   }
   int diff;
   if (autoLoadTO.getExpire() >= 600) {
     diff = 120;
   } else {
     diff = 60;
   }
   if (!autoLoadTO.isLoading()
       && (now - autoLoadTO.getLastLoadTime()) >= (autoLoadTO.getExpire() - diff) * 1000) {
     if (config.isCheckFromCacheBeforeLoad()) {
       CacheWrapper<T> result = cacheManager.get(autoLoadTO.getCacheKey());
       if (null != result
           && result.getLastLoadTime() > autoLoadTO.getLastLoadTime()
           && (now - result.getLastLoadTime()) < (autoLoadTO.getExpire() - diff) * 1000) {
         autoLoadTO.setLastLoadTime(result.getLastLoadTime());
         return;
       }
     }
     try {
       autoLoadTO.setLoading(true);
       ProceedingJoinPoint pjp = autoLoadTO.getJoinPoint();
       String className = pjp.getTarget().getClass().getName();
       String methodName = pjp.getSignature().getName();
       long startTime = System.currentTimeMillis();
       @SuppressWarnings("unchecked")
       T result = (T) pjp.proceed(autoLoadTO.getArgs());
       long useTime = System.currentTimeMillis() - startTime;
       if (config.isPrintSlowLog() && useTime >= config.getSlowLoadTime()) {
         logger.error(className + "." + methodName + ", use time:" + useTime + "ms");
       }
       CacheWrapper<T> tmp = new CacheWrapper<T>();
       tmp.setCacheObject(result);
       tmp.setLastLoadTime(System.currentTimeMillis());
       cacheManager.setCache(autoLoadTO.getCacheKey(), tmp, autoLoadTO.getExpire());
       autoLoadTO.setLastLoadTime(System.currentTimeMillis());
       autoLoadTO.addUseTotalTime(useTime);
     } catch (Exception e) {
       logger.error(e.getMessage(), e);
     } catch (Throwable e) {
       logger.error(e.getMessage(), e);
     } finally {
       autoLoadTO.setLoading(false);
     }
   }
 }