Add clearKey and assignTtl

This commit is contained in:
yo 2024-02-03 22:38:36 +01:00
parent 5487e2dd71
commit 5a4b3cb38a

View File

@ -162,8 +162,9 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
@Override @Override
public LookupResult setValueWithTtl(Object key, Object value, Long ttlSec) { public LookupResult setValueWithTtl(Object key, Object value, Long ttlSec) {
final Timer.Context time = redisSetRequestTimer.time(); final Timer.Context time = redisSetRequestTimer.time();
final String trimmedKey = StringUtils.trimToNull(key.toString());
try { try {
final String result = this.commands.setex(key.toString(), ttlSec, value.toString()); final String result = this.commands.setex(trimmedKey, ttlSec, value.toString());
if (!result.equals("OK")) { if (!result.equals("OK")) {
LOG.warn("Redis SETEX key <{}> to value <{}> with TTL <{}> returned {}", key, value, ttlSec, result); LOG.warn("Redis SETEX key <{}> to value <{}> with TTL <{}> returned {}", key, value, ttlSec, result);
redisSetRequestErrors.mark(); redisSetRequestErrors.mark();
@ -173,7 +174,50 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
} catch (Exception e) { } catch (Exception e) {
LOG.error("Redis SETEX key <{}> to value <{}> with TTL <{}> returned an exception: {}", key, value, ttlSec, e); LOG.error("Redis SETEX key <{}> to value <{}> with TTL <{}> returned an exception: {}", key, value, ttlSec, e);
redisSetRequestErrors.mark(); redisSetRequestErrors.mark();
return LookupResult.empty(); return resultWithError;
} finally {
time.stop();
}
}
@Override
public void clearKey(Object key) {
final Timer.Context time = redisSetRequestTimer.time();
final String trimmedKey = StringUtils.trimToNull(key.toString());
try {
final Long result = this.commands.del(key.toString());
if (result != 1) {
LOG.warn("Redis DEL key <{}> returned {}", key, result);
redisSetRequestErrors.mark();
return resultWithError;
}
return;
} catch (Exception e) {
LOG.error("Redis DEL key <{}> returned {}", key, e);
redisSetRequestErrors.mark();
return;
} finally {
time.stop();
}
}
@Override
public LookupResult assignTtl(Object key, Long ttlSec) {
final Timer.Context time = redisSetRequestTimer.time();
final String trimmedKey = StringUtils.trimToNull(key.toString());
try {
final Boolean result = this.commands.expire(trimmedKey, ttlSec);
if (!result) {
LOG.warn("Redis EXPIRE key <{}> to <{}> returned {}", key, ttlSec, result);
redisSetRequestErrors.mark();
return resultWithError;
}
final String value = this.commands.get(trimmedKey);
return LookupResult.single(value.toString());
} catch (Exception e) {
LOG.error("Redis EXPIRE key <{}> to <{}> returned {}", key, ttlSec, e);
redisSetRequestErrors.mark();
return resultWithError;
} finally { } finally {
time.stop(); time.stop();
} }
@ -186,7 +230,7 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
@Override @Override
Descriptor getDescriptor(); Descriptor getDescriptor();
} }
public static class Descriptor extends LookupDataAdapter.Descriptor<Config> { public static class Descriptor extends LookupDataAdapter.Descriptor<Config> {
public Descriptor() { public Descriptor() {
super(NAME, Config.class); super(NAME, Config.class);