@Override
  public <K, V, C extends Configuration<K, V>> Cache<K, V> newCache(
      final String cacheName, final C configuration) {
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();

    if (configuration instanceof CompleteConfiguration) {
      configureCacheBuilder((CompleteConfiguration) configuration, cacheBuilder);
    }

    return new WrappedCache<K, V>(cacheBuilder.<K, V>build()) {
      @Override
      public String getName() {
        return cacheName;
      }

      @Override
      public CacheManager getCacheManager() {
        return GuavaCacheManager.this;
      }

      @Override
      public void close() {
        if (!isClosed()) {
          super.close();
          destroyCache(cacheName);
        }
      }

      @Override
      public <T extends Configuration<K, V>> T getConfiguration(Class<T> clazz) {
        return Constants.unwrap(configuration, clazz);
      }
    };
  }
 protected InMemorySchemaSourceCache(
     final SchemaSourceRegistry consumer,
     final Class<T> representation,
     final CacheBuilder<Object, Object> builder) {
   super(consumer, representation, Costs.IMMEDIATE);
   cache = builder.build();
 }
 private static Cache<String, CacheObject> buildCache(
     /*int reloadAfterAccess, */ int reloadAfterWrite) {
   CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().softValues();
   // if(reloadAfterAccess > 0)
   //     builder.expireAfterAccess(reloadAfterAccess, TimeUnit.SECONDS);
   if (reloadAfterWrite > 0) builder.expireAfterWrite(reloadAfterWrite, TimeUnit.SECONDS);
   return builder.build();
 }
  public HashMapCacheRepository(long validFor) {
    if (validFor > 0) setExpiry(validFor);

    CacheBuilder builder = CacheBuilder.newBuilder();

    if (validFor > 0) builder.expireAfterAccess(validFor, TimeUnit.MINUTES);

    cache = builder.build();
  }
Exemple #5
0
 @GwtIncompatible("maximumWeight")
 public void testMaximumWeight_withoutWeigher() {
   CacheBuilder<Object, Object> builder = new CacheBuilder<Object, Object>().maximumWeight(1);
   try {
     builder.build(identityLoader());
     fail();
   } catch (IllegalStateException expected) {
   }
 }
 /**
  * @throws IllegalStateException If neither {@link #evictAfterAccess()} nor {@link
  *     #evictAfterWrite()} has been called, or if no periods have been added with {@link
  *     #period(long, TimeUnit, int)}.
  */
 public Cache<K, V> build() {
   if (this.periods.isEmpty()) {
     throw new IllegalStateException("no periods defined");
   } else if (this.periods.size() == 1) {
     final CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
     configureBuilder(builder, this.periods.get(0));
     return builder.build();
   } else {
     assert this.periods.size() > 1;
     final Map<Period, Cache<K, V>> caches = new IdentityHashMap<>();
     for (final Period period : this.periods) {
       final CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
       configureBuilder(builder, period);
       final Cache<K, V> cache = builder.build();
       caches.put(period, cache);
     }
     return new MultiCache<K, V>(this.elementKeyClass, caches, new PeriodSelector<>(this.periods));
   }
 }
 public static final Cache<String, BlockReader> buildCache(CacheBuilder builder) {
   if (singleCache != null) {
     singleCache.cleanUp();
   }
   if (enableStats) {
     // todo: when we upgrade to a newer guava version we can use this
     // builder.recordStats();
   }
   singleCache = builder.build();
   return singleCache;
 }
Exemple #8
0
 public <K, V> LoadingCache<K, V> build(CacheLoader<K, V> loader) {
   LoadingCache<K, V> cache = null;
   if (isCaffeine()) {
     cache = isAsync() ? caffeine.buildAsync(loader).synchronous() : caffeine.build(loader);
   } else {
     cache =
         new GuavaLoadingCache<>(
             guava.build(
                 com.google.common.cache.CacheLoader.asyncReloading(
                     new SingleLoader<>(loader), executor.delegate())),
             ticker(),
             isRecordingStats());
   }
   this.cache = cache;
   return cache;
 }
  private void buildCache() {
    long sizeInBytes =
        MemorySizeValue.parseBytesSizeValueOrHeapRatio(size, INDICES_CACHE_QUERY_SIZE).bytes();

    CacheBuilder<Key, Value> cacheBuilder =
        CacheBuilder.newBuilder()
            .maximumWeight(sizeInBytes)
            .weigher(new QueryCacheWeigher())
            .removalListener(this);
    cacheBuilder.concurrencyLevel(concurrencyLevel);

    if (expire != null) {
      cacheBuilder.expireAfterAccess(expire.millis(), TimeUnit.MILLISECONDS);
    }

    cache = cacheBuilder.build();
  }
Exemple #10
0
  /**
   * Gets the section cache of a world.
   *
   * @param world The world of the cache.
   * @return The section cache.
   */
  private Cache<Long, Section> getSectionCache(final MWorld world) {
    Cache<Long, Section> cache = sections.get(world.getName());
    if (cache == null) {
      CacheBuilder builder = CacheBuilder.newBuilder();

      builder.maximumSize(10000).expireAfterWrite(10, TimeUnit.MINUTES);

      cache =
          builder.build(
              new CacheLoader<Long, Section>() {

                @Override
                public Section load(Long key) throws Exception {
                  int x = getXFromKey(key);
                  int y = getYFromKey(key);
                  int z = getZFromKey(key);
                  return createSection(world, x, y, z);
                }
              });
    }
    sections.put(world.getName(), cache);
    return cache;
  }
 public <K1, V1> LoadingCache<K1, V1> build(CacheLoader<? super K1, V1> loader) {
   return builder.build(loader);
 }
 public <K1, V1> Cache<K1, V1> build() {
   return builder.build();
 }