Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
2cf5dff011 | |||
30e1bf70d9 | |||
f1c53077a4 | |||
5a4b3cb38a | |||
5487e2dd71 |
10
README.md
10
README.md
@ -30,8 +30,14 @@ Usage
|
||||
-----
|
||||
|
||||
* Create data adapter, cache (or not), lookup table
|
||||
* Use 'lookup_set_value(lookup_table, key, value)' to create or update key in redis
|
||||
* Use 'lookup(lookup_table, key)' to get key
|
||||
* Use 'lookup_set_value(lookup_table, key, value, [ttl])' to create or update key in redis
|
||||
* Use 'lookup_value(lookup_table, key)' to get key value
|
||||
* Use 'lookup_clear_key(lookup_table, key)' to remove key
|
||||
* Use 'lookup_has_value(lookup_table, key)' to test key existence
|
||||
* Use 'lookup_assign_ttl(lookup_table, key, ttl)' to change TTL of existing key
|
||||
|
||||
By default keys will be created in Redis with the default TTL defined at data adapter creation time
|
||||
|
||||
|
||||
Getting started
|
||||
---------------
|
||||
|
@ -10,7 +10,7 @@
|
||||
<groupId>in.nosd.redis</groupId>
|
||||
<artifactId>graylog-plugin-redis-lookup</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
<description>Graylog ${project.artifactId} plugin.</description>
|
||||
<url>https://www.graylog.org</url>
|
||||
<developers>
|
||||
|
2
pom.xml
2
pom.xml
@ -31,7 +31,7 @@
|
||||
|
||||
<groupId>in.nosd.redis</groupId>
|
||||
<artifactId>graylog-plugin-redis-lookup</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
|
@ -68,6 +68,7 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
|
||||
private final Config config;
|
||||
private final RedisClient client;
|
||||
private RedisCommands<String, String> commands;
|
||||
private StatefulRedisConnection<String, String> connection;
|
||||
private final Timer redisGetRequestTimer;
|
||||
private final Meter redisGetRequestErrors;
|
||||
private final Timer redisSetRequestTimer;
|
||||
@ -99,19 +100,19 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
|
||||
this.redisSetRequestErrors = metricRegistry.meter(MetricRegistry.name(getClass(), "redisSetRequestErrors"));
|
||||
}
|
||||
|
||||
// Add code to initialise Redis connection
|
||||
@Override
|
||||
protected void doStart() throws Exception {
|
||||
StatefulRedisConnection<String, String> connection = this.client.connect();
|
||||
connection = this.client.connect();
|
||||
this.commands = connection.sync();
|
||||
}
|
||||
|
||||
// Add code to close Redis connection
|
||||
@Override
|
||||
protected void doStop() throws Exception {
|
||||
|
||||
connection.close();
|
||||
client.close();
|
||||
}
|
||||
|
||||
// Returns the refresh interval for this data adapter. Use {@link Duration#ZERO} if refresh should be disabled.
|
||||
@Override
|
||||
public Duration refreshInterval() {
|
||||
return REFRESH_INTERVAL_DURATION;
|
||||
@ -134,7 +135,7 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
|
||||
try {
|
||||
final String value = this.commands.get(trimmedKey);
|
||||
if (value == null) {
|
||||
LOG.warn("Redis GET request for key <{}> returned null, key do not exists.", trimmedKey);
|
||||
LOG.debug("Redis GET request for key <{}> returned null, key do not exists.", trimmedKey);
|
||||
redisGetRequestErrors.mark();
|
||||
return LookupResult.empty();
|
||||
}
|
||||
@ -149,26 +150,72 @@ public class RedisLookupDataAdapter extends LookupDataAdapter {
|
||||
}
|
||||
|
||||
// This is deprecated, see setValue
|
||||
@Override
|
||||
@Deprecated
|
||||
public void set(Object key, Object value) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LookupResult setValue(Object key, Object value) {
|
||||
return setValueWithTtl(key, value, this.config.redisKeyTTL());
|
||||
}
|
||||
|
||||
public LookupResult setValueWithTtl(Object key, Object value, Long ttlSec) {
|
||||
final Timer.Context time = redisSetRequestTimer.time();
|
||||
final String trimmedKey = StringUtils.trimToNull(key.toString());
|
||||
try {
|
||||
final String result = this.commands.setex(key.toString(), this.config.redisKeyTTL(), value.toString());
|
||||
final String result = this.commands.setex(trimmedKey, ttlSec, value.toString());
|
||||
if (!result.equals("OK")) {
|
||||
LOG.warn("Redis SET key <{}> to value <{}> returned {}", key, value, result);
|
||||
LOG.warn("Redis SETEX 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 SET key <{}> to value <{}> returned an exception: {}", key, value, e);
|
||||
LOG.error("Redis SETEX key <{}> to value <{}> with TTL <{}> returned an exception: {}", key, value, ttlSec, e);
|
||||
redisSetRequestErrors.mark();
|
||||
return LookupResult.empty();
|
||||
return LookupResult.withError();
|
||||
} 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.debug("Redis DEL key <{}> returned {}", key, result);
|
||||
redisSetRequestErrors.mark();
|
||||
}
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
LOG.error("Redis DEL key <{}> returned {}", key, e);
|
||||
redisSetRequestErrors.mark();
|
||||
return;
|
||||
} finally {
|
||||
time.stop();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
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 LookupResult.withError();
|
||||
} finally {
|
||||
time.stop();
|
||||
}
|
||||
|
Reference in New Issue
Block a user