本文整理汇总了Java中net.jodah.expiringmap.ExpirationPolicy类的典型用法代码示例。如果您正苦于以下问题:Java ExpirationPolicy类的具体用法?Java ExpirationPolicy怎么用?Java ExpirationPolicy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ExpirationPolicy类属于net.jodah.expiringmap包,在下文中一共展示了ExpirationPolicy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getChatters
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
/**
* Gets all user's present in the twitch chat of a channel.
*
* @param channelName Channel to fetch the information for.
* @return All chatters in a channel, separated into groups like admins, moderators and viewers.
*/
public Chatter getChatters(String channelName) {
// Endpoint
String requestUrl = String.format("%s/group/user/%s/chatters", Endpoints.TMI.getURL(), channelName);
RestTemplate restTemplate = getTwitchClient().getRestClient().getRestTemplate();
// REST Request
try {
if (!restObjectCache.containsKey(requestUrl)) {
Logger.trace(this, "Rest Request to [%s]", requestUrl);
ChatterResult responseObject = restTemplate.getForObject(requestUrl, ChatterResult.class);
restObjectCache.put(requestUrl, responseObject, ExpirationPolicy.CREATED, 60, TimeUnit.SECONDS);
}
return ((ChatterResult) restObjectCache.get(requestUrl)).getChatters();
} catch (RestException restException) {
Logger.error(this, "RestException: " + restException.getRestError().toString());
} catch (Exception ex) {
Logger.error(this, "Request failed: " + ex.getMessage());
Logger.trace(this, ExceptionUtils.getStackTrace(ex));
}
// OnError: Return empty result
return new Chatter();
}
示例2: get
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
/**
* @param cacheKey Cache key
* @param underlyingSource Underlying token source to invoke on cache miss
* @return Token from either cache or underlying source
* @throws IOException Exception from underlying source
*/
@Override
public String get(String cacheKey, TokenSource underlyingSource) throws IOException {
String value = cacheMap.get(cacheKey);
if (value == null) {
lock.lock();
try {
value = cacheMap.get(cacheKey);
if (value == null) {
ExpiringToken token = underlyingSource.get();
value = token.getToken();
if (token.getExpiresInSeconds() > 0) {
cacheMap.put(cacheKey, value, ExpirationPolicy.CREATED, token.getExpiresInSeconds(), TimeUnit.SECONDS);
} else {
throw new IOException("Authorization server does not provide token expiration information. Consider using NoCache or custom cache implementation to avoid performance penalty caused by locking.");
}
}
} finally {
lock.unlock();
}
}
return value;
}
示例3: refresh
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
synchronized void refresh(boolean force) {
String token = tokenCache.get(TOKEN_KEY);
if (force || token == null) {
AccessToken accessToken;
if (secret != null) {
accessToken = identityClient.getToken(secret);
} else {
accessToken = identityClient.getToken(user, password);
}
token = accessToken.getToken();
int exp = (int) (accessToken.getExpiresIn() * TOKEN_EXPIRATION_FACTOR);
LOGGER.info("Token has been renewed and expires in {} seconds", exp);
tokenCache.put(TOKEN_KEY, accessToken.getToken(), ExpirationPolicy.CREATED, exp, TimeUnit.SECONDS);
renewEndpoints(token);
}
}
示例4: refresh
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
private synchronized void refresh() {
String token = tokenCache.get(TOKEN_KEY);
if (token == null) {
AccessToken accessToken;
if (secret != null) {
accessToken = identityClient.getToken(secret);
} else {
accessToken = identityClient.getToken(user, password);
}
token = accessToken.getToken();
int exp = (int) (accessToken.getExpiresIn() * TOKEN_EXPIRATION_FACTOR);
LOGGER.info("Token has been renewed and expires in {} seconds", exp);
tokenCache.put(TOKEN_KEY, accessToken.getToken(), ExpirationPolicy.CREATED, exp, TimeUnit.SECONDS);
renewEndpoints(token);
}
}
示例5: HistoricalSearches
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
HistoricalSearches() {
this.historicalMusic = ExpiringMap.builder()
.expiration(10, TimeUnit.MINUTES)
.expirationPolicy(ExpirationPolicy.CREATED)
.build();
historicalThemeSearchResults = ExpiringMap.builder()
.expiration(10, TimeUnit.MINUTES)
.expirationPolicy(ExpirationPolicy.CREATED)
.build();
}
示例6: getMap
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
private ConcurrentMap<String, Long> getMap(String key, int longestDuration) {
// Currently unable to putIfAbsent when using jodah's expiry map so will wrap in a lock
return lockManager.executeLocked(key, () -> {
ConcurrentMap<String, Long> keyMap = expiringKeyMap.get(key);
if (keyMap == null) {
keyMap = new ConcurrentHashMap<>();
expiringKeyMap.put(key, keyMap, ExpirationPolicy.CREATED, longestDuration, TimeUnit.SECONDS);
}
return keyMap;
});
}
示例7: checkForMissingRunningUnits
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
private void checkForMissingRunningUnits() {
checkins.forEach((c, m) -> {
numberOfServersLast15Minutes.putIfAbsent(c, ExpiringMap.builder().maxSize(15).expirationPolicy(ExpirationPolicy.CREATED).build());
int lowestNumberOfServersLast15Minutes = numberOfServersLast15Minutes.get(c).entrySet().stream().mapToInt(Map.Entry::getValue).min().orElse(0);
int numberOfServersNow = m.size();
if (numberOfServersNow < lowestNumberOfServersLast15Minutes) {
slackClient.indicateFewerRunningUnits(c, lowestNumberOfServersLast15Minutes, numberOfServersNow);
pagerdutyClient.indicateFewerRunningUnits(c, lowestNumberOfServersLast15Minutes, numberOfServersNow);
} else if (numberOfServersNow > lowestNumberOfServersLast15Minutes) {
pagerdutyClient.indicateMoreRunningUnits(c, lowestNumberOfServersLast15Minutes, numberOfServersNow);
}
numberOfServersLast15Minutes.get(c).put(LocalDateTime.now(), numberOfServersNow);
});
}
示例8: HistoricalSearches
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
HistoricalSearches() {
this.historicalAnime = ExpiringMap.builder()
.expiration(15, TimeUnit.MINUTES)
.expirationPolicy(ExpirationPolicy.CREATED)
.build();
this.historicalMusic = ExpiringMap.builder()
.expiration(15, TimeUnit.MINUTES)
.expirationPolicy(ExpirationPolicy.CREATED)
.build();
historicalThemeSearchResults = ExpiringMap.builder()
.expiration(15, TimeUnit.MINUTES)
.expirationPolicy(ExpirationPolicy.CREATED)
.build();
}
示例9: load
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
@Override
public AsyncFuture<QueryResult> load(
FullQuery.Request request, Supplier<AsyncFuture<QueryResult>> loader
) {
final AggregationInstance aggregation = request.getAggregation();
/* can't be cached :( */
if (aggregation.cadence() <= 0) {
return loader.get();
}
final AsyncFuture<QueryResult> result = cache.get(request);
if (result != null) {
return result;
}
synchronized (lock) {
final AsyncFuture<QueryResult> candidate = cache.get(request);
if (candidate != null) {
return candidate;
}
final AsyncFuture<QueryResult> next = loader.get();
cache.put(request, next, ExpirationPolicy.ACCESSED, aggregation.cadence(),
TimeUnit.MILLISECONDS);
return next;
}
}
示例10: ExpiringMapCache
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
public ExpiringMapCache() {
this.cache =
ExpiringMap
.builder()
.expiration(10, TimeUnit.MINUTES)
.expirationPolicy(ExpirationPolicy.CREATED) // time-to-live
.variableExpiration() // allows the keys to have individual expirations
.build();
}
示例11: TimeoutList
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
/**
* @param timeout the timeout after which a device is assumed to be dead
*/
public TimeoutList(long timeout) {
this.devicesMap = ExpiringMap.builder()
.expiration(timeout, TimeUnit.MILLISECONDS)
.expirationPolicy(ExpirationPolicy.CREATED)
.build();
}
示例12: create
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
public void create() {
log.debug("Starting InMemoryCacheProvider ...");
try {
map = ExpiringMap.builder()
.expirationPolicy(ExpirationPolicy.CREATED)
.variableExpiration()
.build();
log.debug("InMemoryCacheProvider started.");
} catch (Exception e) {
throw new IllegalStateException("Error starting InMemoryCacheProvider", e);
}
}
示例13: put
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
@Override // it is so weird but we use as workaround "region" field to pass "expiration" for put operation
public void put(String expirationInSeconds, String key, Object object) {
// if key already exists and hash is the same for value then expiration time is not updated
// net.jodah.expiringmap.ExpiringMap.putInternal()
// therefore we first remove entry and then put it
map.remove(key);
map.put(key, object, ExpirationPolicy.CREATED, putExpiration(expirationInSeconds), TimeUnit.SECONDS);
}
示例14: checkin
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
public void checkin(RunningUnit unit) {
ExpiringMap<String, LocalDateTime> servers = checkins.computeIfAbsent(Component.fromRunningUnit(unit), component -> ExpiringMap.builder().expiration(5, TimeUnit.MINUTES).expirationPolicy(ExpirationPolicy.CREATED).build());
servers.putIfAbsent(unit.getServer(), LocalDateTime.now());
servers.resetExpiration(unit.getServer());
}
示例15: configTokenCache
import net.jodah.expiringmap.ExpirationPolicy; //导入依赖的package包/类
private ExpiringMap<String, String> configTokenCache() {
return ExpiringMap.builder().variableExpiration().expirationPolicy(ExpirationPolicy.CREATED).build();
}