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

The following examples show how to use com.google.common.cache.CacheBuilder#from() . 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: IrisSettings.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static CacheBuilder<Object,Object> configurableCacheBuilder(String base, CacheBuilder<Object,Object> builder) {
   // If there is a full cache specification then that overrides everything
   String spec = IrisSettings.getStringProperty(base + ".spec", "");
   if (!spec.isEmpty()) {
      return CacheBuilder.from(spec);
   }

   CacheBuilder<Object,Object> bld = builder;
   int concurrency = IrisSettings.getIntegerProperty(base + ".concurrency", -1);
   if (concurrency > 0) {
      bld = bld.concurrencyLevel(concurrency);
   }

   long write = IrisSettings.getLongProperty(base + ".expire.write", -1L);
   if (write > 0) {
      bld = bld.expireAfterWrite(write, TimeUnit.MILLISECONDS);
   }

   long access = IrisSettings.getLongProperty(base + ".expire.access", -1L);
   if (access > 0) {
      bld = bld.expireAfterAccess(access, TimeUnit.MILLISECONDS);
   }

   long refresh = IrisSettings.getLongProperty(base + ".refresh.write", -1L);
   if (refresh > 0) {
      bld = bld.refreshAfterWrite(refresh, TimeUnit.MILLISECONDS);
   }

   int initsz = IrisSettings.getIntegerProperty(base + ".initial.capacity", -1);
   if (initsz > 0) {
      bld = bld.initialCapacity(initsz);
   }

   int maxsz = IrisSettings.getIntegerProperty(base + ".max.size", -1);
   if (maxsz > 0) {
      bld = bld.maximumSize(maxsz);
   }

   boolean soft = IrisSettings.getBooleanProperty(base + ".soft.values", false);
   if (soft) {
      bld = bld.softValues();
   }

   return bld;
}
 
Example 3
Source File: CacheUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private CacheBuilder<Object, Object> newCacheBuilder(){
	CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.from(CacheUtil.cacheBuilderSpec);

	return cacheBuilder;
}
 
Example 4
Source File: CachingJwtAuthenticator.java    From dropwizard-auth-jwt with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new cached authenticator.
 *
 * @param metricRegistry the application's registry of metrics
 * @param authenticator  the underlying authenticator
 * @param cacheSpec      a {@link CacheBuilderSpec}
 */
public CachingJwtAuthenticator(final MetricRegistry metricRegistry,
                               final Authenticator<JwtContext, P> authenticator,
                               final CacheBuilderSpec cacheSpec) {
    this(metricRegistry, authenticator, CacheBuilder.from(cacheSpec));
}
 
Example 5
Source File: CachingAuthenticator.java    From dropwizard-java8 with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new cached authenticator.
 *
 * @param metricRegistry the application's registry of metrics
 * @param authenticator  the underlying authenticator
 * @param cacheSpec      a {@link CacheBuilderSpec}
 */
public CachingAuthenticator(final MetricRegistry metricRegistry,
                            final Authenticator<C, P> authenticator,
                            final CacheBuilderSpec cacheSpec) {
    this(metricRegistry, authenticator, CacheBuilder.from(cacheSpec));
}