Java Code Examples for com.google.common.cache.CacheBuilder#weigher()

The following examples show how to use com.google.common.cache.CacheBuilder#weigher() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AssetServlet.java    From dropwizard-configurable-assets-bundle with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@code AssetServlet} that serves static assets loaded from {@code resourceURL}
 * (typically a file: or jar: URL). The assets are served at URIs rooted at {@code uriPath}. For
 * example, given a {@code resourceURL} of {@code "file:/data/assets"} and a {@code uriPath} of
 * {@code "/js"}, an {@code AssetServlet} would serve the contents of {@code
 * /data/assets/example.js} in response to a request for {@code /js/example.js}. If a directory
 * is requested and {@code indexFile} is defined, then {@code AssetServlet} will attempt to
 * serve a file with that name in that directory. If a directory is requested and {@code
 * indexFile} is null, it will serve a 404.
 *
 * @param resourcePathToUriPathMapping A mapping from base URL's from which assets are loaded to
 *                                     the URI path fragment in which the requests for that asset
 *                                     are rooted
 * @param indexFile                    the filename to use when directories are requested, or null
 *                                     to serve no indexes
 * @param defaultCharset               the default character set
 * @param spec                         the CacheBuilderSpec to use
 * @param overrides                    the path overrides
 * @param mimeTypes                    the mimeType overrides
 */
public AssetServlet(Iterable<Map.Entry<String, String>> resourcePathToUriPathMapping,
                    String indexFile,
                    Charset defaultCharset,
                    CacheBuilderSpec spec,
                    Iterable<Map.Entry<String, String>> overrides,
                    Iterable<Map.Entry<String, String>> mimeTypes) {
  this.defaultCharset = defaultCharset;
  AssetLoader loader = new AssetLoader(resourcePathToUriPathMapping, indexFile, overrides);

  CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.from(spec);
  // Don't add the weigher if we are using maximumSize instead of maximumWeight.
  if (spec.toParsableString().contains("maximumWeight=")) {
    cacheBuilder.weigher(new AssetSizeWeigher());
  }
  this.cache = cacheBuilder.build(loader);

  this.cacheSpec = spec;
  this.mimeTypes = new MimeTypes();
  this.setMimeTypes(mimeTypes);
}
 
Example 2
Source File: MapCache.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
MapCache(final String name, final CacheConfiguration config) {
  this.name = name;

  CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
  if (config.isStatisticsEnabled()) {
    cacheBuilder.recordStats();
  }
  if (config.isSoftValuesEnabled()) {
    cacheBuilder.softValues();
  }
  if (config.getInitialCapacity() >= 0) {
    cacheBuilder.initialCapacity(config.getInitialCapacity());
  }
  if (config.getMaximumSize() >= 0) {
    if (config.isArraySizeEnabled()) {
      cacheBuilder.maximumWeight(config.getMaximumSize());
      cacheBuilder.weigher(new Weigher<K, V>() {
        @Override
        public int weigh(final K key, final V value) {
          if (value instanceof byte[]) {
            return ((byte[]) value).length;
          }
          throw new IllegalStateException("Using array size is only supported for byte arrays"); //$NON-NLS-1$
        }
      });
    } else {
      cacheBuilder.maximumSize(config.getMaximumSize());
    }
  }

  backend = cacheBuilder.build();
}