下面列出了com.google.common.cache.CacheBuilder#from ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
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;
}
static
private CacheBuilder<Object, Object> newCacheBuilder(){
CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.from(CacheUtil.cacheBuilderSpec);
return cacheBuilder;
}
/**
* 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));
}
/**
* 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));
}