Example usage for com.google.common.cache CacheBuilder newBuilder

List of usage examples for com.google.common.cache CacheBuilder newBuilder

Introduction

In this page you can find the example usage for com.google.common.cache CacheBuilder newBuilder.

Prototype

public static CacheBuilder<Object, Object> newBuilder() 

Source Link

Document

Constructs a new CacheBuilder instance with default settings, including strong keys, strong values, and no automatic eviction of any kind.

Usage

From source file:com.streamsets.pipeline.lib.parser.net.syslog.SyslogDecoder.java

public static LoadingCache<String, Long> buildTimestampCache(DateTimeFormatter timeParser) {
    return CacheBuilder.newBuilder().maximumSize(1000).build(new CacheLoader<String, Long>() {
        @Override/*www .  j  av a2  s.c  o  m*/
        public Long load(String key) {
            return LocalDateTime.from(timeParser.parse(key)).toInstant(ZoneOffset.UTC).toEpochMilli();
        }
    });
}

From source file:org.elasticsearch.index.cache.field.data.soft.SoftFieldDataCache.java

@Override
protected Cache<String, FieldData> buildFieldDataMap() {
    CacheBuilder<String, FieldData> cacheBuilder = CacheBuilder.newBuilder().softValues().removalListener(this);
    return cacheBuilder.build();
}

From source file:com.facebook.buck.rules.keys.InputCountingRuleKeyFactory.java

public InputCountingRuleKeyFactory(int seed, FileHashLoader hashLoader, SourcePathResolver pathResolver) {
    super(seed);//from w  w w .j a  va 2s  .c o m
    this.resultCache = CacheBuilder.newBuilder().weakKeys()
            .build(new CacheLoader<RuleKeyAppendable, InputCountingRuleKeyFactory.Result>() {
                @Override
                public InputCountingRuleKeyFactory.Result load(@Nonnull RuleKeyAppendable appendable)
                        throws Exception {
                    RuleKeyBuilder<InputCountingRuleKeyFactory.Result> subKeyBuilder = newBuilder();
                    appendable.appendToRuleKey(subKeyBuilder);
                    return subKeyBuilder.build();
                }
            });
    this.hashLoader = hashLoader;
    this.pathResolver = pathResolver;
}

From source file:com.haulmont.cuba.core.sys.entitycache.StandardQueryCache.java

@PostConstruct
protected void init() {
    data = CacheBuilder.newBuilder().maximumSize(queryCacheConfig.getQueryCacheMaxSize()).build();
}

From source file:com.codebullets.sagalib.processing.invocation.ReflectionInvoker.java

/**
 * Generates a new instance of ReflectionInvoker.
 *//*from   w ww . j  a  v a  2 s  . c om*/
@Inject
public ReflectionInvoker(final SagaAnalyzer analyzer) {
    invocationMethods = CacheBuilder.newBuilder().build(new MethodSearcher(analyzer.scanHandledMessageTypes()));
}

From source file:com.oneops.opamp.service.CachedEnvPropsProcessor.java

@Override
public void setCmProcessor(CmsCmProcessor cmProcessor) {
    super.setCmProcessor(cmProcessor);
    variables = CacheBuilder.newBuilder().maximumSize(maxSize).expireAfterWrite(ttlInSeconds, TimeUnit.SECONDS)
            .build(new CacheLoader<String, Boolean>() {
                public Boolean load(String key) {
                    CmsVar repairStatus = cmProcessor.getCmSimpleVar(key);
                    return repairStatus != null && Boolean.TRUE.toString().equals(repairStatus.getValue());
                }//from   ww  w.  ja va 2s .  co  m
            });

    this.varCache = CacheBuilder.newBuilder().maximumSize(maxSize).expireAfterWrite(ttlInSeconds, SECONDS)
            .initialCapacity(1).build(new CacheLoader<String, Long>() {
                @Override
                public Long load(String key) throws Exception {
                    CmsVar cacheStatus = cmProcessor.getCmSimpleVar(key);
                    if (cacheStatus != null) {
                        return parseLong(cacheStatus.getValue());
                    }
                    return 0L;
                }
            });
}

From source file:com.bennavetta.vetinari.template.TemplateLoader.java

@Inject
public TemplateLoader(VetinariContext context, Set<TemplateEngine> templateEngines) {
    this.context = context;
    this.templateEngines = templateEngines;
    this.defaultTemplateEngine = Iterables.find(this.templateEngines,
            t -> t.getName().equals(this.context.getSiteConfig().getString("defaultTemplateEngine")));

    this.templateCache = CacheBuilder.newBuilder().build(new CacheLoader<String, Template>() {
        @Override//from  w  w w.j  a va2s. c  o  m
        public Template load(String templateName) throws Exception {
            final Path templatePath = context.getTemplateRoot().resolve(templateName);
            final String extension = getFileExtension(templateName);
            final TemplateEngine engine = Iterables
                    .tryFind(templateEngines, t -> Iterables.contains(t.getFileExtensions(), extension))
                    .or(defaultTemplateEngine);
            log.debug("Compiling template from {} with {}", templatePath, engine);
            String source = new String(Files.readAllBytes(templatePath), context.getContentEncoding());
            log.trace("Read template source \"{}\"", source);
            return engine.compile(source);
        }
    });
}

From source file:org.apache.beam.runners.direct.SideInputContainer.java

/**
 * Create a new {@link SideInputContainer} with the provided views and the provided
 * context.//from   w w w.j a  v  a  2s  .  c o  m
 */
public static SideInputContainer create(final EvaluationContext context,
        Collection<PCollectionView<?>> containedViews) {
    LoadingCache<PCollectionViewWindow<?>, AtomicReference<Iterable<? extends WindowedValue<?>>>> viewByWindows = CacheBuilder
            .newBuilder().build(new CallbackSchedulingLoader(context));
    return new SideInputContainer(containedViews, viewByWindows);
}

From source file:com.stegosaurus.crypt.DefaultPermutationProvider.java

/**
 * CTOR./*from www. j  ava2 s .  c o m*/
 */
public DefaultPermutationProvider() {
    /* TODO Every last thing about this is awful. */
    /* TODO Make this nicer on the eye... */
    /* TODO Externalize the damn parameters */
    permutations = CacheBuilder.newBuilder().maximumSize(5)
            .build(new CacheLoader<Integer, LoadingCache<Long, Permutation>>() {
                public LoadingCache<Long, Permutation> load(final Integer size) {
                    return CacheBuilder.newBuilder().maximumSize(50)
                            .build(new CacheLoader<Long, Permutation>() {
                                public Permutation load(Long seed) {
                                    Permutation retval = new Permutation(size, seed);
                                    retval.init();
                                    return retval;
                                }
                            });
                }
            });
}

From source file:io.crate.operation.projectors.IndexNameResolver.java

public static Supplier<String> forPartition(final TableIdent tableIdent,
        final List<Input<?>> partitionedByInputs) {
    assert partitionedByInputs.size() > 0 : "must have at least 1 partitionedByInput";
    final LoadingCache<List<BytesRef>, String> cache = CacheBuilder.newBuilder().initialCapacity(10)
            .maximumSize(20).build(new CacheLoader<List<BytesRef>, String>() {
                @Override/*from   www. j  a va 2  s. com*/
                public String load(@Nonnull List<BytesRef> key) throws Exception {
                    return PartitionName.indexName(tableIdent, PartitionName.encodeIdent(key));
                }
            });
    return new Supplier<String>() {

        @Override
        public String get() {
            // copy because transform returns a view and the values of the inputs are mutable
            List<BytesRef> partitions = Collections.unmodifiableList(
                    Lists.newArrayList(Lists.transform(partitionedByInputs, Inputs.TO_BYTES_REF)));
            try {
                return cache.get(partitions);
            } catch (ExecutionException e) {
                throw Throwables.propagate(e);
            }
        }
    };
}