io.github.sinri.keel.cache.impl.KeelCacheGimel 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.KeelAsyncEverlastingCacheInterface;
import io.vertx.core.Future;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BooleanSupplier;
/**
* @param
* @param
* @since 2.9
*/
public class KeelCacheGimel implements KeelAsyncEverlastingCacheInterface {
private final Lock lock;
private final Map map;
private long lockWaitMs = 100;
public KeelCacheGimel() {
lock = new ReentrantLock();
map = new HashMap<>();
}
public long getLockWaitMs() {
return lockWaitMs;
}
public KeelCacheGimel setLockWaitMs(long lockWaitMs) {
this.lockWaitMs = lockWaitMs;
return this;
}
private Future actionInLock(BooleanSupplier resultSupplier) {
return Future.succeededFuture()
.compose(ready -> {
try {
var locked = lock.tryLock(getLockWaitMs(), TimeUnit.MILLISECONDS);
if (locked) {
return Future.failedFuture("locked");
}
} catch (InterruptedException e) {
return Future.failedFuture(e);
}
boolean result;
try {
result = resultSupplier.getAsBoolean();
} catch (Throwable throwable) {
result = false;
} finally {
lock.unlock();
}
if (result) {
return Future.succeededFuture();
} else {
return Future.failedFuture("action failed");
}
});
}
@Override
public Future save(@Nonnull K k, V v) {
return actionInLock(() -> {
map.put(k, v);
return true;
});
}
@Override
public Future save(@Nonnull Map appendEntries) {
return actionInLock(() -> {
map.putAll(appendEntries);
return true;
});
}
@Override
public Future read(@Nonnull K k, V v) {
AtomicReference vRef = new AtomicReference<>();
return actionInLock(() -> {
var x = map.get(k);
if (x != null) {
vRef.set(x);
} else {
vRef.set(v);
}
return true;
})
.compose(unlocked -> Future.succeededFuture(vRef.get()));
}
@Override
public Future remove(@Nonnull K key) {
return actionInLock(() -> {
map.remove(key);
return true;
});
}
@Override
public Future remove(@Nonnull Collection keys) {
return actionInLock(() -> {
keys.forEach(key -> map.remove(key));
return true;
});
}
@Override
public Future removeAll() {
return actionInLock(() -> {
map.clear();
return true;
});
}
/**
* @param newEntries new map of entries
* @since 2.9.4 no longer implemented by replace map
*/
@Override
public Future replaceAll(@Nonnull Map newEntries) {
return actionInLock(() -> {
Set ks = newEntries.keySet();
map.putAll(newEntries);
map.keySet().forEach(k -> {
if (!ks.contains(k)) {
map.remove(k);
}
});
return true;
});
}
@Override
@Nonnull
public Map getSnapshotMap() {
return Collections.unmodifiableMap(map);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy