Skip to content

Instantly share code, notes, and snippets.

@ova2
Created July 27, 2021 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ova2/c174173abef34d0aebabbec589cfcae2 to your computer and use it in GitHub Desktop.
Save ova2/c174173abef34d0aebabbec589cfcae2 to your computer and use it in GitHub Desktop.
/**
* Gets cached values as Java Stream. Returned stream is not sorted.
*/
public Stream<OVALUE> getValues() {
final Lock readLock = lock.readLock();
readLock.lock();
try {
return cache.values().stream().flatMap(cachedValue -> cachedValue.getValue().stream());
} finally {
readLock.unlock();
}
}
/**
* Gets cached value as Java Optional.
*/
public Optional<OVALUE> getValue(KEY key) {
final Lock readLock = lock.readLock();
readLock.lock();
try {
return Optional.ofNullable(cache.get(key)).flatMap(CacheMonoValue::getValue);
} finally {
readLock.unlock();
}
}
/**
* Removes the mapping for a key from this map if it is present.
*/
public void remove(KEY key) {
final Lock writeLock = lock.writeLock();
writeLock.lock();
try {
cache.remove(key);
} finally {
writeLock.unlock();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment