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

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

Introduction

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

Prototype

public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(CacheLoader<? super K1, V1> loader) 

Source Link

Document

Builds a cache, which either returns an already-loaded value for a given key or atomically computes or retrieves it using the supplied CacheLoader .

Usage

From source file:org.eluder.jersey.mustache.ReloadingMustacheFactory.java

@Override
protected LoadingCache<String, Mustache> createMustacheCache() {
    int templateExpiryMillis = getTemplateExpiryMillis();
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    if (templateExpiryMillis > ETERNAL_CACHE_VALUE) {
        builder.expireAfterWrite(templateExpiryMillis, TimeUnit.MILLISECONDS);
    }/*from ww  w. j a  v  a 2 s .c  om*/
    return builder.build(createMustacheCacheLoader());
}

From source file:org.centralperf.service.RunStatisticsService.java

private LoadingCache<Long, SummaryGraph> getSummaryGraphCache() {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    builder.expireAfterWrite(cacheRefreshDelay, TimeUnit.SECONDS);
    return builder.build(summaryGraphLoader);
}

From source file:org.centralperf.service.RunStatisticsService.java

private LoadingCache<Long, ResponseTimeGraph> getResponseTimeGraphCache() {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    builder.expireAfterWrite(cacheRefreshDelay, TimeUnit.SECONDS);
    return builder.build(responseTimeGraphLoader);
}

From source file:org.centralperf.service.RunStatisticsService.java

private LoadingCache<Long, ResponseSizeGraph> getResponseSizeGraphCache() {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    builder.expireAfterWrite(cacheRefreshDelay, TimeUnit.SECONDS);
    return builder.build(responseSizeGraphLoader);
}

From source file:org.centralperf.service.RunStatisticsService.java

private LoadingCache<Long, ErrorRateGraph> getErrorRateGraphCache() {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    builder.expireAfterWrite(cacheRefreshDelay, TimeUnit.SECONDS);
    return builder.build(errorRateGraphLoader);
}

From source file:org.centralperf.service.RunStatisticsService.java

public LoadingCache<Long, RunStats> getRunStatsCache() {
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    builder.expireAfterWrite(cacheRefreshDelay, TimeUnit.SECONDS);
    return builder.build(runStatsLoader);
}

From source file:com.hazelcast.client.GuavaNearCacheImpl.java

public GuavaNearCacheImpl(NearCacheConfig nc, final MapClientProxy<K, V> map) {
    this.map = map;
    CacheBuilder cacheBuilder = CacheBuilder.newBuilder().maximumSize(nc.getMaxSize());
    if (nc.getTimeToLiveSeconds() > 0)
        cacheBuilder.expireAfterWrite(nc.getTimeToLiveSeconds(), TimeUnit.SECONDS);
    if (nc.getMaxIdleSeconds() > 0)
        cacheBuilder.expireAfterAccess(nc.getMaxIdleSeconds(), TimeUnit.SECONDS);
    cache = cacheBuilder.build(new CacheLoader() {
        @Override/*  www  .  jav a  2s .  c om*/
        public Object load(Object o) throws Exception {
            try {
                return map.get0(o);
            } catch (Exception e) {
                throw new ExecutionException(e);
            }
        }
    });
}

From source file:com.streamsets.pipeline.stage.processor.jdbctee.JdbcTeeProcessor.java

public JdbcTeeProcessor(String schema, String tableNameTemplate,
        List<JdbcFieldColumnParamMapping> customMappings, List<JdbcFieldColumnMapping> generatedColumnMappings,
        boolean caseSensitive, boolean rollbackOnError, boolean useMultiRowOp, int maxPrepStmtParameters,
        int maxPrepStmtCache, ChangeLogFormat changeLogFormat, HikariPoolConfigBean hikariConfigBean,
        JDBCOperationType defaultOp, UnsupportedOperationAction unsupportedAction) {
    this.jdbcUtil = UtilsProvider.getJdbcUtil();
    this.schema = schema;
    this.tableNameTemplate = tableNameTemplate;
    this.customMappings = customMappings;
    this.generatedColumnMappings = generatedColumnMappings;
    this.caseSensitive = caseSensitive;
    this.rollbackOnError = rollbackOnError;
    this.useMultiRowOp = useMultiRowOp;
    this.maxPrepStmtParameters = maxPrepStmtParameters;
    this.maxPrepStmtCache = maxPrepStmtCache;
    this.changeLogFormat = changeLogFormat;
    this.hikariConfigBean = hikariConfigBean;
    this.defaultOperation = defaultOp;
    this.unsupportedAction = unsupportedAction;
    this.dynamicTableName = this.jdbcUtil.isElString(tableNameTemplate);

    CacheBuilder cacheBuilder = CacheBuilder.newBuilder().maximumSize(500).expireAfterAccess(1, TimeUnit.HOURS);

    if (LOG.isDebugEnabled()) {
        cacheBuilder.recordStats();//from   ww  w  .java2  s .  com
    }

    this.recordWriters = cacheBuilder.build(new RecordWriterLoader());

    cacheCleaner = new CacheCleaner(this.recordWriters, "JdbcTeeProcessor", 10 * 60 * 1000);
}

From source file:com.enitalk.configs.DateCache.java

@Bean(name = "skipCache")
public LoadingCache<String, ConcurrentSkipListSet<DateTime>> datesMap() {
    CacheBuilder<Object, Object> ccc = CacheBuilder.newBuilder();
    ccc.expireAfterWrite(2, TimeUnit.MINUTES);

    LoadingCache<String, ConcurrentSkipListSet<DateTime>> cache = ccc
            .build(new CacheLoader<String, ConcurrentSkipListSet<DateTime>>() {

                @Override//from   w  w  w . j a va  2s .  c o  m
                public ConcurrentSkipListSet<DateTime> load(String key) throws Exception {
                    try {
                        HashMap teachers = mongo.findOne(Query.query(Criteria.where("i").is(key)),
                                HashMap.class, "teachers");
                        ObjectNode teacherJson = jackson.convertValue(teachers, ObjectNode.class);
                        String timeZone = teacherJson.at("/calendar/timeZone").asText();

                        NavigableSet<DateTime> set = days(teacherJson.path("schedule"), timeZone, teacherJson);

                        DateTimeZone dzz = DateTimeZone.forID(timeZone);
                        DateTimeFormatter df = ISODateTimeFormat.dateTimeNoMillis().withZone(dzz);

                        byte[] events = calendar.busyEvents(jackson.createObjectNode().put("id", key));
                        JsonNode evs = jackson.readTree(events);
                        Iterator<JsonNode> its = evs.iterator();
                        TreeSet<DateTime> dates = new TreeSet<>();
                        while (its.hasNext()) {
                            String date = its.next().asText();
                            DateTime av = df.parseDateTime(date).toDateTime(DateTimeZone.UTC);
                            dates.add(av);
                        }

                        set.removeAll(dates);

                        logger.info("Dates for i {} {}", key, set);

                        return new ConcurrentSkipListSet<>(set);

                    } catch (Exception e) {
                        logger.error(ExceptionUtils.getFullStackTrace(e));
                    }
                    return null;
                }

            });

    return cache;
}

From source file:org.opennms.plugins.elasticsearch.rest.NodeCacheImpl.java

public void init() {
    if (cache == null) {
        LOG.info("initializing node data cache (TTL=" + MAX_TTL + "m, MAX_SIZE=" + MAX_SIZE + ")");
        CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
        if (MAX_TTL > 0) {
            cacheBuilder.expireAfterWrite(MAX_TTL, TimeUnit.MINUTES);
        }//from   w w w. j a va 2s  . c  o  m
        if (MAX_SIZE > 0) {
            cacheBuilder.maximumSize(MAX_SIZE);
        }

        cache = cacheBuilder.build(new CacheLoader<Long, Map<String, String>>() {
            @Override
            public Map<String, String> load(Long key) throws Exception {
                return getNodeAndCategoryInfo(key);
            }
        });
    }
}