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 Timer.Context time = redisSetRequestTimer.time();
|
||||||
final String trimmedKey = StringUtils.trimToNull(key.toString());
|
final String trimmedKey = StringUtils.trimToNull(key.toString());
|
||||||
try {
|
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")) {
|
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();
|
redisSetRequestErrors.mark();
|
||||||
return LookupResult.empty();
|
return LookupResult.empty();
|
||||||
}
|
}
|
||||||
return LookupResult.single(value.toString());
|
return LookupResult.single(value.toString());
|
||||||
} 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 SET(EX) key <{}> to value <{}> with TTL <{}> returned an exception: {}", key, value, ttlSec, e);
|
||||||
redisSetRequestErrors.mark();
|
redisSetRequestErrors.mark();
|
||||||
return LookupResult.withError();
|
return LookupResult.withError();
|
||||||
} finally {
|
} 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) {
|
public LookupResult assignTtl(Object key, Long ttlSec) {
|
||||||
final Timer.Context time = redisSetRequestTimer.time();
|
final Timer.Context time = redisSetRequestTimer.time();
|
||||||
final String trimmedKey = StringUtils.trimToNull(key.toString());
|
final String trimmedKey = StringUtils.trimToNull(key.toString());
|
||||||
try {
|
try {
|
||||||
final Boolean result = this.commands.expire(trimmedKey, ttlSec);
|
if (ttlSec > 0) {
|
||||||
if (!result) {
|
return setExpire(trimmedKey, ttlSec);
|
||||||
LOG.warn("Redis EXPIRE key <{}> to <{}> returned {}", key, ttlSec, result);
|
} else {
|
||||||
redisSetRequestErrors.mark();
|
return setPersist(trimmedKey);
|
||||||
return LookupResult.withError();
|
|
||||||
}
|
}
|
||||||
final String value = this.commands.get(trimmedKey);
|
|
||||||
return LookupResult.single(value.toString());
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.error("Redis EXPIRE key <{}> to <{}> returned {}", key, ttlSec, e);
|
LOG.error("assignTtl <{}> to key <{}> returned {}", ttlSec, trimmedKey, e);
|
||||||
redisSetRequestErrors.mark();
|
redisSetRequestErrors.mark();
|
||||||
return LookupResult.withError();
|
return LookupResult.withError();
|
||||||
} finally {
|
} 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> {
|
public interface Factory extends LookupDataAdapter.Factory2<RedisLookupDataAdapter> {
|
||||||
@Override
|
@Override
|
||||||
RedisLookupDataAdapter create(@Assisted("dto") DataAdapterDto dto);
|
RedisLookupDataAdapter create(@Assisted("dto") DataAdapterDto dto);
|
||||||
@ -241,7 +279,7 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
|
|||||||
.redisHost("127.0.0.1")
|
.redisHost("127.0.0.1")
|
||||||
.redisPort(6379)
|
.redisPort(6379)
|
||||||
.redisDB(0)
|
.redisDB(0)
|
||||||
.redisKeyTTL(86400)
|
.redisKeyTTL(-)
|
||||||
.redisUsername("")
|
.redisUsername("")
|
||||||
.redisPassword("")
|
.redisPassword("")
|
||||||
.build();
|
.build();
|
||||||
|
@ -82,7 +82,7 @@ class RedisLookupAdapterFieldSet extends React.Component {
|
|||||||
label="Redis key TTL"
|
label="Redis key TTL"
|
||||||
required
|
required
|
||||||
onChange={this.props.handleFormEvent}
|
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')}
|
bsStyle={this.props.validationState('redis_ttl')}
|
||||||
value={config.redis_ttl}
|
value={config.redis_ttl}
|
||||||
labelClassName="col-sm-3"
|
labelClassName="col-sm-3"
|
||||||
|
Loading…
Reference in New Issue
Block a user