示例#1
0
 @Override
 public String getName() {
   if (refreshAheadConfig.getName() != null) {
     return refreshAheadConfig.getName();
   }
   return super.getName();
 }
示例#2
0
  private void initSupportCache() {
    // create the support cache
    // make this cache clustered in the same way as the underlying cache,
    this.supportConfig = new CacheConfiguration();
    supportConfig.name(
        underlyingCache.getName() + "_" + getClass().getName() + "_refreshAheadSupport");
    supportConfig =
        supportConfig.persistence(new PersistenceConfiguration().strategy(Strategy.NONE));
    int activeSize =
        2 * refreshAheadConfig.getBatchSize() * refreshAheadConfig.getNumberOfThreads();
    supportConfig = supportConfig.maxEntriesLocalHeap(activeSize);
    supportConfig = supportConfig.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU);
    supportConfig = supportConfig.timeToLiveSeconds(DEFAULT_SUPPORT_TTL_SECONDS);

    // TC stuff
    if (underlyingCache.getCacheConfiguration().isTerracottaClustered()) {
      supportConfig =
          supportConfig.persistence(new PersistenceConfiguration().strategy(Strategy.DISTRIBUTED));

      TerracottaConfiguration newTerracottaConfig = new TerracottaConfiguration().clustered(true);

      newTerracottaConfig.consistency(Consistency.STRONG);

      supportConfig.addTerracotta(newTerracottaConfig);
    } else {
      supportConfig.setMaxElementsOnDisk(activeSize);
    }

    // here we try to create the support cache.
    this.supportCache = new Cache(supportConfig);

    Ehcache prior = underlyingCache.getCacheManager().addCacheIfAbsent(supportCache);
    if (prior != supportCache) {
      throw new IllegalStateException(
          "Unable to add refresh ahead support cache due to name collision: "
              + refreshAheadConfig.getName());
    }

    // wipe it on startup. might wobble in a clustered case, but clears out orphans.
    prior.removeAll();

    // catch the dispose. not sure this is the best way to do it at all.
    // we could register a listener alternatively
    underlyingCache.registerCacheExtension(
        new CacheExtension() {

          @Override
          public void init() {}

          @Override
          public Status getStatus() {
            return underlyingCache.getStatus();
          }

          @Override
          public void dispose() throws CacheException {
            RefreshAheadCache.this.localDispose();
          }

          @Override
          public CacheExtension clone(Ehcache cache) throws CloneNotSupportedException {
            throw new CloneNotSupportedException();
          }
        });
  }