當前位置: 首頁>>代碼示例>>Java>>正文


Java Suppliers.memoizeWithExpiration方法代碼示例

本文整理匯總了Java中com.google.common.base.Suppliers.memoizeWithExpiration方法的典型用法代碼示例。如果您正苦於以下問題:Java Suppliers.memoizeWithExpiration方法的具體用法?Java Suppliers.memoizeWithExpiration怎麽用?Java Suppliers.memoizeWithExpiration使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.google.common.base.Suppliers的用法示例。


在下文中一共展示了Suppliers.memoizeWithExpiration方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: GSuiteGroupAuthorizationFilter

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
public GSuiteGroupAuthorizationFilter(final GSuiteDirectoryService gsuiteDirService, AppConfiguration config) {
    this.config = config;
    this.externalAccountsCache = Suppliers.memoizeWithExpiration(
            () -> {
                String allowGroup = config.getExternalAccountsGroup();
                Set<String> result = Collections.emptySet();
                try {
                    GroupMembership membership = gsuiteDirService.getGroupMembers(allowGroup);
                    result = membership.getMembers() == null ? Collections.emptySet()
                    : membership.getMembers().stream().map(m -> m.getEmail()).collect(Collectors.toSet());
                } catch (ResourceNotFoundException e) {
                    log.warn("Group for external accounts {} does not exists", allowGroup);
                }
                return result;
            }, 15, TimeUnit.MINUTES);
}
 
開發者ID:hlavki,項目名稱:g-suite-identity-sync,代碼行數:17,代碼來源:GSuiteGroupAuthorizationFilter.java

示例2: main

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException {
    // {{start:memoize}}
    log.info("Memoized");
    Supplier<String> memoized = Suppliers.memoize(SuppliersExamples::helloWorldSupplier);
    log.info(memoized.get());
    log.info(memoized.get());
    // {{end:memoize}}

    // {{start:memoizeWithExpiration}}
    log.info("Memoized with Expiration");
    Supplier<String> memoizedExpiring = Suppliers.memoizeWithExpiration(
        SuppliersExamples::helloWorldSupplier, 50, TimeUnit.MILLISECONDS);
    log.info(memoizedExpiring.get());
    log.info(memoizedExpiring.get());
    log.info("sleeping");
    TimeUnit.MILLISECONDS.sleep(100);
    log.info(memoizedExpiring.get());
    log.info(memoizedExpiring.get());
    log.info("sleeping");
    TimeUnit.MILLISECONDS.sleep(100);
    log.info(memoizedExpiring.get());
    log.info(memoizedExpiring.get());
    // {{end:memoizeWithExpiration}}
}
 
開發者ID:StubbornJava,項目名稱:StubbornJava,代碼行數:25,代碼來源:SuppliersExamples.java

示例3: ConfigService

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
public ConfigService(List<ConfigurationProvider> configurationProviders, Authorizer authorizer, long maximumCacheSize, long cacheTtlMillis) {
    this.authorizer = authorizer;
    this.objectMapper = new ObjectMapper();
    if (configurationProviders == null || configurationProviders.size() == 0) {
        throw new IllegalArgumentException("Expected at least one configuration provider");
    }
    this.configurationProviderInfo = Suppliers.memoizeWithExpiration(() -> initContentTypeInfo(configurationProviders), cacheTtlMillis, TimeUnit.MILLISECONDS);
    CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    if (maximumCacheSize >= 0) {
        cacheBuilder = cacheBuilder.maximumSize(maximumCacheSize);
    }
    if (cacheTtlMillis >= 0) {
        cacheBuilder = cacheBuilder.refreshAfterWrite(cacheTtlMillis, TimeUnit.MILLISECONDS);
    }
    this.configurationCache = cacheBuilder
            .build(new CacheLoader<ConfigurationProviderKey, ConfigurationProviderValue>() {
                @Override
                public ConfigurationProviderValue load(ConfigurationProviderKey key) throws Exception {
                    return initConfigurationProviderValue(key);
                }
            });
}
 
開發者ID:apache,項目名稱:nifi-minifi,代碼行數:23,代碼來源:ConfigService.java

示例4: refresh

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
@Override
public void refresh() {
    _cache = Suppliers.memoizeWithExpiration(new Supplier<CachedInfo>() {
        @Override
        public CachedInfo get() {
            Map<String, DataCenter> dataCenters = _dataCenterDao.loadAll();
            if (!_ignoredDataCenters.isEmpty()) {
                ImmutableMap.Builder<String, DataCenter> dataCentersBuilder = ImmutableMap.builder();
                for (Map.Entry<String, DataCenter> entry : dataCenters.entrySet()) {
                    if (!_ignoredDataCenters.contains(entry.getKey())) {
                        dataCentersBuilder.put(entry);
                    } else if (_loggedIgnored.add(entry.getKey())) {
                        // Only want to log that we're ignoring the data center once
                        _log.info("Ignoring data center: {}", entry.getKey());
                    }
                }
                dataCenters = dataCentersBuilder.build();
            }
            return new CachedInfo(dataCenters);
        }
    }, 10, TimeUnit.MINUTES);
}
 
開發者ID:bazaarvoice,項目名稱:emodb,代碼行數:23,代碼來源:DefaultDataCenters.java

示例5: StandardStashReader

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
@VisibleForTesting
StandardStashReader(URI stashRoot, AmazonS3 s3, long refreshLatestMs) {
    super(stashRoot, s3);

    Supplier<String> s3LatestRootSupplier = new Supplier<String>() {
        @Override
        public String get() {
            String latest = readLatestStash();
            return String.format("%s/%s", _rootPath, latest);
        }
    };

    if (refreshLatestMs > 0) {
        // Cache the latest stash directory and periodically recheck
        _latestRootSupplier = Suppliers.memoizeWithExpiration(s3LatestRootSupplier, refreshLatestMs, TimeUnit.MILLISECONDS);
    } else {
        _latestRootSupplier = s3LatestRootSupplier;
    }
}
 
開發者ID:bazaarvoice,項目名稱:emodb,代碼行數:20,代碼來源:StandardStashReader.java

示例6: ShardSizeExpression

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
@Inject
public ShardSizeExpression(final IndexShard indexShard) {
    sizeSupplier = Suppliers.memoizeWithExpiration(new Supplier<Long>() {
        @Override
        public Long get() {
            return indexShard.storeStats().getSizeInBytes();
        }
    }, 10, TimeUnit.SECONDS);
}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:10,代碼來源:ShardSizeExpression.java

示例7: BlobShardSizeExpression

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
@Inject
public BlobShardSizeExpression(final BlobShard blobShard) {
    totalUsageSupplier = Suppliers.memoizeWithExpiration(new Supplier<Long>() {
        @Override
        public Long get() {
            return blobShard.blobStats().totalUsage();
        }
    }, 10, TimeUnit.SECONDS);
}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:10,代碼來源:BlobShardSizeExpression.java

示例8: configure

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
private void configure() {
    log.info("Configuring GSuiteDirectoryService ...");
    long tokenLifetime = config.getServiceAccountTokenLifetime();
    log.info("Token lifetime set to {}", tokenLifetime);
    this.tokenCache = Suppliers.memoizeWithExpiration(() -> getAccessToken(), tokenLifetime - 3, TimeUnit.SECONDS);
    this.membershipCache = Suppliers.memoizeWithExpiration(() -> getAllGroupMembershipInternal(), 3, TimeUnit.MINUTES);
    try {
        privateKey = config.getServiceAccountKey();
        log.info("Service account private key {}loaded", privateKey != null ? "" : "was not ");
    } catch (NoPrivateKeyException e) {
        log.error(e.getMessage(), e.getCause());
    }
}
 
開發者ID:hlavki,項目名稱:g-suite-identity-sync,代碼行數:14,代碼來源:GSuiteDirectoryServiceImpl.java

示例9: fromSupplierWithExpiration

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
public static InMemorySitemap fromSupplierWithExpiration(
        Supplier<Map<String, List<String>>> supplier,
        long duration,
        TimeUnit unit) {
    Supplier<Map<String, String>> sup = mapSupplier(supplier);
    Supplier<Map<String, String>> memoized = Suppliers.memoizeWithExpiration(sup::get, duration, unit);
    return new InMemorySitemap(memoized);
}
 
開發者ID:StubbornJava,項目名稱:StubbornJava,代碼行數:9,代碼來源:InMemorySitemap.java

示例10: ServiceConfigFetcher

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
@VisibleForTesting
ServiceConfigFetcher(Supplier<Service> supplier) {
  this.serviceSupplier = Suppliers.memoizeWithExpiration(supplier, 10, TimeUnit.MINUTES);
}
 
開發者ID:cloudendpoints,項目名稱:endpoints-management-java,代碼行數:5,代碼來源:ServiceConfigFetcher.java

示例11: DefaultJobService

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
@Inject
public DefaultJobService(LifeCycleRegistry lifeCycleRegistry,
                         QueueService queueService,
                         @JobQueueName String queueName,
                         JobHandlerRegistryInternal jobHandlerRegistry,
                         JobStatusDAO jobStatusDAO,
                         @JobZooKeeper CuratorFramework curator,
                         @JobConcurrencyLevel Integer concurrencyLevel,
                         @QueueRefreshTime Duration queueRefreshTime,
                         final @QueuePeekLimit Integer queuePeekLimit,
                         @NotOwnerRetryDelay Duration notOwnerRetryDelay) {
    _queueService = checkNotNull(queueService, "queueService");
    _queueName = checkNotNull(queueName, "queueName");
    _jobHandlerRegistry = checkNotNull(jobHandlerRegistry, "jobHandlerRegistry");
    _jobStatusDAO = checkNotNull(jobStatusDAO, "jobStatusDAO");
    _curator = checkNotNull(curator, "curator");
    _concurrencyLevel = checkNotNull(concurrencyLevel, "concurrencyLevel");
    checkArgument(_concurrencyLevel >= 0, "Concurrency level cannot be negative");
    _notOwnerRetryDelay = checkNotNull(notOwnerRetryDelay, "notOwnerRetryDelay");
    checkNotNull(queuePeekLimit, "queuePeekLimit");
    checkNotNull(lifeCycleRegistry, "lifecycleRegistry");

    _recentNotOwnerDelays = CacheBuilder.newBuilder()
            .expireAfterWrite(notOwnerRetryDelay.getMillis(), TimeUnit.MILLISECONDS)
            .build();

    Supplier<Queue<Message>> sourceMessageSupplier = new Supplier<Queue<Message>>() {
        @Override
        public Queue<Message> get() {
            return Queues.synchronizedQueue(Queues.newArrayDeque(_queueService.peek(_queueName, queuePeekLimit)));
        }
    };

    checkNotNull(queueRefreshTime, "queueRefreshTime");
    if (queueRefreshTime.getMillis() == 0) {
        _messageSupplier = sourceMessageSupplier;
    } else {
        _messageSupplier = Suppliers.memoizeWithExpiration(
                sourceMessageSupplier, queueRefreshTime.getMillis(), TimeUnit.MILLISECONDS);
    }

    lifeCycleRegistry.manage(this);
}
 
開發者ID:bazaarvoice,項目名稱:emodb,代碼行數:44,代碼來源:DefaultJobService.java

示例12: memoizeWithRandomExpiration

import com.google.common.base.Suppliers; //導入方法依賴的package包/類
/**
 * Memoization similar to {@link Suppliers#memoizeWithExpiration(Supplier, long, TimeUnit)} except the expiration
 * is randomly selected to be a value within provided bounds.  For example:
 *
 * <code>
 *     Supplier&lt;Object&t; supplier = MoreSuppliers.memoizeWithRandomExpiration(delegate, 5, 10, TimeUnit.SECONDS);
 * </code>
 *
 * returns a Supplier that will memoize the delegate's response for a random number of nanoseconds between 5
 * (inclusive) to 10 (exclusive) seconds.
 *
 * This is useful when numerous memoized values are typically computed independently but used within the same operation.
 * If all values were computed and memoized together then each call that refreshes from delegate could take longer
 * than desirable.  Similarly, if each value were memomized independently using the same expiration then herding
 * behavior would result in the same long calls at expiration time.  A random expiration allows each value to be
 * cached but spread out the refresh cycle so that it is unlikely that any single call will refresh the entire value
 * set, so long as the call frequency is significantly below <code>minDuration</code>.
 */
public static <T> Supplier<T> memoizeWithRandomExpiration(Supplier<T> delegate, long minDuration, long maxDuration, TimeUnit units) {
    if (minDuration == maxDuration) {
        // This case resolves to standard expiration
        return Suppliers.memoizeWithExpiration(delegate, minDuration, units);
    }
    return new RandomExpirationSupplier<T>(delegate, minDuration, maxDuration, units);
}
 
開發者ID:bazaarvoice,項目名稱:emodb,代碼行數:26,代碼來源:MoreSuppliers.java


注:本文中的com.google.common.base.Suppliers.memoizeWithExpiration方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。