io.github.sinri.keel.cache.impl.KeelCacheVet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Keel Show documentation
Show all versions of Keel Show documentation
A website framework with VERT.X for ex-PHP-ers, exactly Ark Framework Users.
The newest version!
package io.github.sinri.keel.cache.impl;
import io.github.sinri.keel.cache.KeelEverlastingCacheInterface;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @since 2.9
*/
public class KeelCacheVet implements KeelEverlastingCacheInterface {
private final Lock lock;
private final Map map;
public KeelCacheVet() {
lock = new ReentrantLock();
map = new HashMap<>();
}
@Override
public void save(@Nonnull K k, V v) {
lock.lock();
try {
map.put(k, v);
} finally {
lock.unlock();
}
}
@Override
public void save(@Nonnull Map appendEntries) {
lock.lock();
try {
map.putAll(appendEntries);
} finally {
lock.unlock();
}
}
@Override
public V read(@Nonnull K k, V v) {
V r;
lock.lock();
try {
r = map.getOrDefault(k, v);
} finally {
lock.unlock();
}
return r;
}
@Override
public void remove(@Nonnull K key) {
lock.lock();
try {
map.remove(key);
} finally {
lock.unlock();
}
}
@Override
public void remove(@Nonnull Collection keys) {
lock.lock();
try {
keys.forEach(map::remove);
} finally {
lock.unlock();
}
}
@Override
public void removeAll() {
lock.lock();
try {
map.clear();
} finally {
lock.unlock();
}
}
/**
* @param newEntries new map of entries
* @since 2.9.4 no longer implemented by replace map
*/
@Override
public void replaceAll(@Nonnull Map newEntries) {
lock.lock();
try {
Set ks = newEntries.keySet();
map.putAll(newEntries);
map.keySet().forEach(k -> {
if (!ks.contains(k)) {
map.remove(k);
}
});
} finally {
lock.unlock();
}
}
@Override
@Nonnull
public Map getSnapshotMap() {
return Collections.unmodifiableMap(map);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy