示例#1
0
  private void initWorkQueue() {
    BatchWorker<Object> batchWorker =
        new BatchWorker<Object>() {

          @Override
          public void process(Collection<? extends Object> collection) {

            // only fetch this once for each process() call
            long accessTime = System.currentTimeMillis();

            HashSet<Object> keysToProcess = new HashSet<Object>();
            for (Object key : collection) {

              // check if it was loaded by someone else in the meantime -- does it still qualify for
              // refresh ahead?
              Element quickTest = underlyingCache.getQuiet(key);
              if (quickTest == null
                  || checkForRefresh(
                      quickTest, accessTime, refreshAheadConfig.getTimeToRefreshMillis())) {
                final Element ersatz = new Element(key, REFRESH_VALUE);

                if (supportCache.putIfAbsent(ersatz) == null) {
                  // work, work, work
                  keysToProcess.add(key);
                }
              }
            }
            try {
              // iterate through the loaders
              for (CacheLoader loader : underlyingCache.getRegisteredCacheLoaders()) {
                // if we are out of keys, punt
                if (keysToProcess.isEmpty()) {
                  break;
                }

                // try and load them all
                Map<? extends Object, ? extends Object> values = loader.loadAll(keysToProcess);
                // subtract the ones that were loaded
                keysToProcess.removeAll(values.keySet());
                try {
                  for (Map.Entry<? extends Object, ? extends Object> entry : values.entrySet()) {
                    Element newElement = new Element(entry.getKey(), entry.getValue());
                    underlyingCache.put(newElement);
                    refreshSuccessCount.incrementAndGet();
                  }
                } finally {
                  // subtract from the support cache
                  supportCache.removeAll(values.keySet());
                }
              }
              // assume we got here ok, now evict any that don't evict
              if (refreshAheadConfig.isEvictOnLoadMiss() && !keysToProcess.isEmpty()) {
                underlyingCache.removeAll(keysToProcess);
              }
            } finally {
              // this is utterly paranoid. but still.
              supportCache.removeAll(keysToProcess);
            }
          }
        };

    this.refreshWorkQueue =
        new ThreadedWorkQueue<Object>(
            batchWorker,
            refreshAheadConfig.getNumberOfThreads(),
            new ThreadFactory() {

              @Override
              public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                return t;
              }
            },
            refreshAheadConfig.getMaximumRefreshBacklogItems(),
            refreshAheadConfig.getBatchSize());
  }