frostfs-sdk-java/client/src/main/java/info/frostfs/sdk/pool/SessionCache.java
Ori Bruk e9e9480701
All checks were successful
DCO / DCO (pull_request) Successful in 25s
Verify code phase / Verify code (pull_request) Successful in 1m49s
[#32] Provide a pool of clients to grpc
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
2025-01-17 16:57:59 +03:00

46 lines
1.1 KiB
Java

package info.frostfs.sdk.pool;
import info.frostfs.sdk.dto.session.SessionToken;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class SessionCache {
private final ConcurrentMap<String, SessionToken> cache = new ConcurrentHashMap<>();
private final long tokenDuration;
private long currentEpoch;
public SessionCache(long sessionExpirationDuration) {
this.tokenDuration = sessionExpirationDuration;
}
public boolean contains(String key) {
return cache.containsKey(key);
}
public boolean tryGetValue(String key, SessionToken[] value) {
if (key == null) {
value[0] = null;
return false;
}
SessionToken token = cache.get(key);
if (token != null) {
value[0] = token;
return true;
}
return false;
}
public void setValue(String key, SessionToken value) {
if (key != null) {
cache.put(key, value);
}
}
public void deleteByPrefix(String prefix) {
cache.keySet().removeIf(key -> key.startsWith(prefix));
}
}