Handle TTL to -1 = no key expiration
This commit is contained in:
parent
8febd14eb6
commit
0a0748172e
@ -164,15 +164,20 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
|
||||
final Timer.Context time = redisSetRequestTimer.time();
|
||||
final String trimmedKey = StringUtils.trimToNull(key.toString());
|
||||
try {
|
||||
final String result = this.commands.setex(trimmedKey, ttlSec, value.toString());
|
||||
final String result;
|
||||
if (ttlSec > 0 ) {
|
||||
result = this.commands.setex(trimmedKey, ttlSec, value.toString());
|
||||
} else {
|
||||
result = this.commands.set(trimmedKey, value.toString());
|
||||
}
|
||||
if (!result.equals("OK")) {
|
||||
LOG.warn("Redis SETEX key <{}> to value <{}> with TTL <{}> returned {}", key, value, ttlSec, result);
|
||||
LOG.warn("Redis SET(EX) key <{}> to value <{}> with TTL <{}> returned {}", key, value, ttlSec, result);
|
||||
redisSetRequestErrors.mark();
|
||||
return LookupResult.empty();
|
||||
}
|
||||
return LookupResult.single(value.toString());
|
||||
} catch (Exception e) {
|
||||
LOG.error("Redis SETEX key <{}> to value <{}> with TTL <{}> returned an exception: {}", key, value, ttlSec, e);
|
||||
LOG.error("Redis SET(EX) key <{}> to value <{}> with TTL <{}> returned an exception: {}", key, value, ttlSec, e);
|
||||
redisSetRequestErrors.mark();
|
||||
return LookupResult.withError();
|
||||
} finally {
|
||||
@ -200,20 +205,48 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
private LookupResult setExpire(String key, Long ttl) {
|
||||
try {
|
||||
final Boolean result = this.commands.expire(key, ttl);
|
||||
if (!result) {
|
||||
LOG.warn("Redis EXPIRE key <{}> to <{}> returned {}", key, ttl, result);
|
||||
return LookupResult.withError();
|
||||
}
|
||||
final String value = this.commands.get(key);
|
||||
return LookupResult.single(value.toString());
|
||||
} catch (Exception e) {
|
||||
LOG.error("Redis EXPIRE key <{}> to <{}> returned {}", key, ttl, e);
|
||||
return LookupResult.withError(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private LookupResult setPersist(String key) {
|
||||
try {
|
||||
final Boolean result = this.commands.persist(key);
|
||||
if (!result) {
|
||||
LOG.warn("Redis PERSIST key <{}> returned {}", key, result);
|
||||
return LookupResult.withError();
|
||||
}
|
||||
final String value = this.commands.get(key);
|
||||
return LookupResult.single(value.toString());
|
||||
} catch (Exception e) {
|
||||
LOG.error("Redis PERSIST key <{}> returned {}", key, e);
|
||||
return LookupResult.withError(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// TTL -1 = never expire
|
||||
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 LookupResult.withError();
|
||||
if (ttlSec > 0) {
|
||||
return setExpire(trimmedKey, ttlSec);
|
||||
} else {
|
||||
return setPersist(trimmedKey);
|
||||
}
|
||||
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);
|
||||
LOG.error("assignTtl <{}> to key <{}> returned {}", ttlSec, trimmedKey, e);
|
||||
redisSetRequestErrors.mark();
|
||||
return LookupResult.withError();
|
||||
} finally {
|
||||
@ -221,6 +254,11 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LookupResult setStringList(Object key, List<String> listValue) {
|
||||
return LookupResult.empty();
|
||||
}
|
||||
|
||||
public interface Factory extends LookupDataAdapter.Factory2<RedisLookupDataAdapter> {
|
||||
@Override
|
||||
RedisLookupDataAdapter create(@Assisted("dto") DataAdapterDto dto);
|
||||
@ -241,7 +279,7 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
|
||||
.redisHost("127.0.0.1")
|
||||
.redisPort(6379)
|
||||
.redisDB(0)
|
||||
.redisKeyTTL(86400)
|
||||
.redisKeyTTL(-)
|
||||
.redisUsername("")
|
||||
.redisPassword("")
|
||||
.build();
|
||||
|
@ -82,7 +82,7 @@ class RedisLookupAdapterFieldSet extends React.Component {
|
||||
label="Redis key TTL"
|
||||
required
|
||||
onChange={this.props.handleFormEvent}
|
||||
help={this.props.validationMessage('redis_ttl', 'Redis key TTL in seconds')}
|
||||
help={this.props.validationMessage('redis_ttl', 'Redis key TTL in seconds. Set -1 to not expire keys')}
|
||||
bsStyle={this.props.validationState('redis_ttl')}
|
||||
value={config.redis_ttl}
|
||||
labelClassName="col-sm-3"
|
||||
|
Loading…
Reference in New Issue
Block a user