configurable redis pool (fix #629)

This commit is contained in:
Athou
2014-09-22 09:51:55 +02:00
parent c3feaf9a15
commit 2a36cc4327
6 changed files with 72 additions and 17 deletions

View File

@@ -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
timeZone: UTC
# Redis pool configuration
# (only used if app.cache is 'redis')
# -----------------------------------
redis:
host: localhost
port: 6379
password:
timeout: 2000
database: 0
maxTotal: 500

View File

@@ -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
timeZone: UTC
# Redis pool configuration
# (only used if app.cache is 'redis')
# -----------------------------------
redis:
host: localhost
port: 6379
password:
timeout: 2000
database: 0
maxTotal: 500

View File

@@ -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")

View File

@@ -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);
}

View File

@@ -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<String> 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) {

View File

@@ -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);
}
}