@Override public void record(long key) { Object value = cache.get(key); if (value == null) { if (cache.size() == cache.getMaxSize()) { policyStats.recordEviction(); } cache.put(key, key); policyStats.recordMiss(); } else { policyStats.recordHit(); } }
@Override public byte[] get(String key) { Objects.requireNonNull(key); byte[] value = map.get(key); return clone(value); }
@Override public List<Message> getByApplication(String applicationId) throws TException { checkThat(applicationId) .throwing(InvalidArgumentException.class) .is(validApplicationId()); return messagesByApplication.getOrDefault(applicationId, Sets.emptySet()) .stream() .map(id -> messages.get(id)) .filter(Objects::nonNull) .collect(toList()); }
private void scanEntries() { for (String key : cache.keySet()) { CachedValueWrapper wrapper = cache.get(key); if (wrapper.getRedisCacheAnn().redisFor() == StoreValue) continue; long expiration = RedisCacheUtils.redisExpirationSeconds(key, appContext); long cacheExpiration = cache.getExpiration(key); if (expiration == cacheExpiration) continue; CachedValueWrapper removed = cache.remove(key); if (removed == null) continue; Logger logger = wrapper.getLogger(); logger.debug("invalidate cache {} because of redis refresh seconds changed to {} ", key, expiration); } }
@Override public boolean setTTL(String key, long ttl) { Objects.requireNonNull(key); byte[] value = map.get(key); if (value == null) { return false; } doSetTTL(key, ttl); return true; }
@Override public List<Message> getByApplication(String applicationId) throws TException { checkThat(applicationId) .throwing(InvalidArgumentException.class) .is(validApplicationId()); return messagesByApplication.getOrDefault(applicationId, Sets.emptySet()) .stream() .map(id -> messages.get(id)) .filter(Objects::nonNull) .collect(toList()); }
@Override public void record(long key) { Object value = cache.get(key); if (value == null) { if (cache.size() == cache.getMaxSize()) { policyStats.recordEviction(); } cache.put(key, key); policyStats.recordMiss(); } else { policyStats.recordHit(); } }
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; }); }
@Override public List<Message> getByTitle(String applicationId, String title) throws TException { checkThat(applicationId) .throwing(InvalidArgumentException.class) .is(validApplicationId()); checkThat(title) .throwing(InvalidArgumentException.class) .is(nonEmptyString()); return messagesByApplication.getOrDefault(applicationId, Sets.emptySet()) .stream() .map(id -> messages.get(id)) .filter(Objects::nonNull) .filter(m -> Objects.equals(m.title, title)) .collect(toList()); }
Object getLocalCache() { CachedValueWrapper cachedValue = localCache.get(valueKey); if (cachedValue != null) { long expectedExpiration = getExpectedExpirationSeconds(); logger.debug("got local {} = {} with expiration {} seconds", valueKey, cachedValue.getValue(), expectedExpiration); if (expectedExpiration >= 0) this.value = cachedValue.getValue(); else localCache.remove(valueKey); } else { logger.debug("got local {} = null", valueKey); } return this.value; }
@Override public List<Message> getByTitle(String applicationId, String title) throws TException { checkThat(applicationId) .throwing(InvalidArgumentException.class) .is(validApplicationId()); checkThat(title) .throwing(InvalidArgumentException.class) .is(nonEmptyString()); return messagesByApplication.getOrDefault(applicationId, Sets.emptySet()) .stream() .map(id -> messages.get(id)) .filter(Objects::nonNull) .filter(m -> Objects.equals(m.title, title)) .collect(toList()); }
@Override public boolean compareAndSet(String key, byte[] expected, byte[] value, long ttl) { Objects.requireNonNull(key); // clone is not needed if the comparison fails // but we are optimistic and prefer to do the clone outside the lock value = clone(value); // we don't use ExpiringMap.replace because it deals with null differently writeLock.lock(); try { byte[] current = map.get(key); boolean equal = Arrays.equals(expected, current); if (equal) { if (value == null) { map.remove(key); } else { map.put(key, value); doSetTTL(key, ttl); } } return equal; } finally { writeLock.unlock(); } }
private MappedAccessToken mapToSelfToken(String clientAt){ MappedAccessToken token=tokenMappings.get(clientAt); if(token != null){ if(token.isExpired()){ AccessToken at=refreshAccessToken(token); token=new MappedAccessToken(clientAt, at); tokenMappings.put(clientAt, token); } return token; } token=newAccessToken(clientAt); if(token!=null){ tokenMappings.put(clientAt, token); } return token; }
@Override public AuthenticationToken getToken(String tokenId) throws TException, InvalidTokenException { checkThat(tokenId) .throwing(InvalidArgumentException.class) .usingMessage("missing tokenId") .is(nonEmptyString()) .throwing(InvalidTokenException.class) .usingMessage("token does not exists") .is(keyInMap(tokens)); synchronized (tokens) { return tokens.get(tokenId); } }
@Override public AuthenticationToken getToken(String tokenId) throws TException, InvalidTokenException { checkThat(tokenId) .throwing(InvalidArgumentException.class) .usingMessage("missing tokenId") .is(nonEmptyString()) .throwing(InvalidTokenException.class) .usingMessage("token does not exists") .is(keyInMap(tokens)); synchronized (tokens) { return tokens.get(tokenId); } }
@Override public List<UserExtSource> consolidateIdentityUsingToken(PerunSession sess, String token) throws PerunException { Map<String, Object> originalIdentity = requestCache.get(token);
@Override public Message getMessage(String applicationId, String messageId) throws TException { checkThat(applicationId, messageId) .throwing(InvalidArgumentException.class) .are(nonEmptyString()); checkThat(messageId) .throwing(MessageDoesNotExistException.class) .is(keyInMap(messages)); Message message = messages.get(messageId); checkThat(message.applicationId) .throwing(MessageDoesNotExistException.class) .usingMessage("Message does not correspond to the given App ID") .is(equalTo(applicationId)); return message; }
@Override public Message getMessage(String applicationId, String messageId) throws TException { checkThat(applicationId, messageId) .throwing(InvalidArgumentException.class) .are(nonEmptyString()); checkThat(messageId) .throwing(MessageDoesNotExistException.class) .is(keyInMap(messages)); Message message = messages.get(messageId); checkThat(message.applicationId) .throwing(MessageDoesNotExistException.class) .usingMessage("Message does not correspond to the given App ID") .is(equalTo(applicationId)); return message; }