diff --git a/config.dev.yml b/config.dev.yml index 48002851..e2ddf666 100644 --- a/config.dev.yml +++ b/config.dev.yml @@ -88,6 +88,7 @@ server: adminConnectors: - type: http port: 8084 + logging: level: INFO loggers: @@ -103,4 +104,16 @@ logging: archive: true archivedLogFilenamePattern: log/commafeed-%d.log archivedFileCount: 5 - timeZone: UTC \ No newline at end of file + timeZone: UTC + +# Redis pool configuration +# (only used if app.cache is 'redis') +# ----------------------------------- +redis: + host: localhost + port: 6379 + password: + timeout: 2000 + database: 0 + maxTotal: 500 + \ No newline at end of file diff --git a/config.yml.example b/config.yml.example index ad46640a..cff78a1a 100644 --- a/config.yml.example +++ b/config.yml.example @@ -88,6 +88,7 @@ server: adminConnectors: - type: http port: 8084 + logging: level: WARN loggers: @@ -102,4 +103,16 @@ logging: archive: true archivedLogFilenamePattern: log/commafeed-%d.log archivedFileCount: 5 - timeZone: UTC \ No newline at end of file + timeZone: UTC + +# Redis pool configuration +# (only used if app.cache is 'redis') +# ----------------------------------- +redis: + host: localhost + port: 6379 + password: + timeout: 2000 + database: 0 + maxTotal: 500 + \ No newline at end of file diff --git a/src/main/java/com/commafeed/CommaFeedConfiguration.java b/src/main/java/com/commafeed/CommaFeedConfiguration.java index c7874172..c9566389 100644 --- a/src/main/java/com/commafeed/CommaFeedConfiguration.java +++ b/src/main/java/com/commafeed/CommaFeedConfiguration.java @@ -15,6 +15,7 @@ import lombok.Getter; import org.apache.commons.lang.time.DateUtils; import org.hibernate.validator.constraints.NotBlank; +import com.commafeed.backend.cache.RedisPoolFactory; import com.commafeed.frontend.SessionManagerFactory; import com.fasterxml.jackson.annotation.JsonProperty; @@ -35,7 +36,12 @@ public class CommaFeedConfiguration extends Configuration { @NotNull @JsonProperty("database") private DataSourceFactory database = new DataSourceFactory(); - + + @Valid + @NotNull + @JsonProperty("redis") + private RedisPoolFactory redisPoolFactory = new RedisPoolFactory(); + @Valid @NotNull @JsonProperty("session") diff --git a/src/main/java/com/commafeed/CommaFeedModule.java b/src/main/java/com/commafeed/CommaFeedModule.java index f3fedb57..1524865b 100644 --- a/src/main/java/com/commafeed/CommaFeedModule.java +++ b/src/main/java/com/commafeed/CommaFeedModule.java @@ -30,7 +30,7 @@ public class CommaFeedModule extends AbstractModule { @Override protected void configure() { CacheService cacheService = config.getApplicationSettings().getCache() == CacheType.NOOP ? new NoopCacheService() - : new RedisCacheService(); + : new RedisCacheService(config.getRedisPoolFactory().build()); log.info("using cache {}", cacheService.getClass()); bind(CacheService.class).toInstance(cacheService); } diff --git a/src/main/java/com/commafeed/backend/cache/RedisCacheService.java b/src/main/java/com/commafeed/backend/cache/RedisCacheService.java index c34e4dca..ac305d24 100644 --- a/src/main/java/com/commafeed/backend/cache/RedisCacheService.java +++ b/src/main/java/com/commafeed/backend/cache/RedisCacheService.java @@ -4,10 +4,10 @@ import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.Pipeline; import com.commafeed.backend.model.Feed; @@ -21,17 +21,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Lists; @Slf4j +@RequiredArgsConstructor public class RedisCacheService extends CacheService { - private static ObjectMapper mapper = new ObjectMapper(); + private static ObjectMapper MAPPER = new ObjectMapper(); - private JedisPool pool; - - public RedisCacheService() { - JedisPoolConfig config = new JedisPoolConfig(); - config.setMaxTotal(500); - pool = new JedisPool(config, "localhost"); - } + private final JedisPool pool; @Override public List getLastEntries(Feed feed) { @@ -75,7 +70,7 @@ public class RedisCacheService extends CacheService { String key = buildRedisUserRootCategoryKey(user); String json = jedis.get(key); if (json != null) { - cat = mapper.readValue(json, Category.class); + cat = MAPPER.readValue(json, Category.class); } } catch (Exception e) { log.error(e.getMessage(), e); @@ -93,7 +88,7 @@ public class RedisCacheService extends CacheService { Pipeline pipe = jedis.pipelined(); pipe.del(key); - pipe.set(key, mapper.writeValueAsString(category)); + pipe.set(key, MAPPER.writeValueAsString(category)); pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30)); pipe.sync(); } catch (JsonProcessingException e) { @@ -111,7 +106,7 @@ public class RedisCacheService extends CacheService { String key = buildRedisUnreadCountKey(sub); String json = jedis.get(key); if (json != null) { - count = mapper.readValue(json, UnreadCount.class); + count = MAPPER.readValue(json, UnreadCount.class); } } catch (Exception e) { log.error(e.getMessage(), e); @@ -129,7 +124,7 @@ public class RedisCacheService extends CacheService { Pipeline pipe = jedis.pipelined(); pipe.del(key); - pipe.set(key, mapper.writeValueAsString(count)); + pipe.set(key, MAPPER.writeValueAsString(count)); pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30)); pipe.sync(); } catch (Exception e) { diff --git a/src/main/java/com/commafeed/backend/cache/RedisPoolFactory.java b/src/main/java/com/commafeed/backend/cache/RedisPoolFactory.java new file mode 100644 index 00000000..302a2793 --- /dev/null +++ b/src/main/java/com/commafeed/backend/cache/RedisPoolFactory.java @@ -0,0 +1,28 @@ +package com.commafeed.backend.cache; + +import lombok.Getter; + +import org.apache.commons.lang.StringUtils; + +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; +import redis.clients.jedis.Protocol; + +@Getter +public class RedisPoolFactory { + private String host = "localhost"; + private int port = Protocol.DEFAULT_PORT; + private String password = null; + private int timeout = Protocol.DEFAULT_TIMEOUT; + private int database = Protocol.DEFAULT_DATABASE; + + private int maxTotal = 500; + + public JedisPool build() { + JedisPoolConfig config = new JedisPoolConfig(); + config.setMaxTotal(maxTotal); + + return new JedisPool(config, host, port, timeout, StringUtils.trimToNull(password), database); + } + +}