2013-06-29 11:20:53 +02:00
|
|
|
package com.commafeed.backend.cache;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import javax.enterprise.context.ApplicationScoped;
|
|
|
|
|
import javax.enterprise.inject.Alternative;
|
|
|
|
|
|
2013-07-02 18:07:08 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2013-06-29 11:20:53 +02:00
|
|
|
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;
|
2013-07-25 10:21:11 +02:00
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
2013-07-02 18:07:08 +02:00
|
|
|
import com.commafeed.backend.model.Models;
|
|
|
|
|
import com.commafeed.backend.model.User;
|
2013-07-25 10:38:23 +02:00
|
|
|
import com.commafeed.frontend.model.Category;
|
2013-07-02 18:07:08 +02:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2013-08-01 13:35:04 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2013-06-29 11:20:53 +02:00
|
|
|
|
|
|
|
|
@Alternative
|
|
|
|
|
@ApplicationScoped
|
|
|
|
|
public class RedisCacheService extends CacheService {
|
|
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(RedisCacheService.class);
|
2013-07-25 10:21:11 +02:00
|
|
|
private static ObjectMapper mapper = new ObjectMapper();
|
2013-07-02 18:07:08 +02:00
|
|
|
|
2013-06-29 11:20:53 +02:00
|
|
|
private JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<String> getLastEntries(Feed feed) {
|
|
|
|
|
List<String> list = Lists.newArrayList();
|
|
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
2013-07-02 18:07:08 +02:00
|
|
|
String key = buildRedisEntryKey(feed);
|
2013-06-29 11:20:53 +02:00
|
|
|
Set<String> members = jedis.smembers(key);
|
|
|
|
|
for (String member : members) {
|
|
|
|
|
list.add(member);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setLastEntries(Feed feed, List<String> entries) {
|
|
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
2013-07-02 18:07:08 +02:00
|
|
|
String key = buildRedisEntryKey(feed);
|
2013-06-29 11:20:53 +02:00
|
|
|
|
|
|
|
|
Pipeline pipe = jedis.pipelined();
|
|
|
|
|
pipe.del(key);
|
|
|
|
|
for (String entry : entries) {
|
|
|
|
|
pipe.sadd(key, entry);
|
|
|
|
|
}
|
2013-07-02 10:48:54 +02:00
|
|
|
pipe.expire(key, (int) TimeUnit.DAYS.toSeconds(7));
|
2013-06-29 11:20:53 +02:00
|
|
|
pipe.sync();
|
|
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-02 18:07:08 +02:00
|
|
|
@Override
|
2013-07-25 10:38:23 +02:00
|
|
|
public Category getUserRootCategory(User user) {
|
|
|
|
|
Category cat = null;
|
2013-07-02 18:07:08 +02:00
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
2013-07-25 10:38:23 +02:00
|
|
|
String key = buildRedisUserRootCategoryKey(user);
|
2013-07-02 18:07:08 +02:00
|
|
|
String json = jedis.get(key);
|
|
|
|
|
if (json != null) {
|
2013-07-25 10:38:23 +02:00
|
|
|
cat = mapper.readValue(json, Category.class);
|
2013-07-02 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
2013-07-25 10:38:23 +02:00
|
|
|
return cat;
|
2013-07-02 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-07-25 10:38:23 +02:00
|
|
|
public void setUserRootCategory(User user, Category category) {
|
2013-07-02 18:07:08 +02:00
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
2013-07-25 10:38:23 +02:00
|
|
|
String key = buildRedisUserRootCategoryKey(user);
|
2013-07-02 18:07:08 +02:00
|
|
|
|
|
|
|
|
Pipeline pipe = jedis.pipelined();
|
|
|
|
|
pipe.del(key);
|
2013-07-25 10:38:23 +02:00
|
|
|
pipe.set(key, mapper.writeValueAsString(category));
|
2013-07-03 12:42:01 +02:00
|
|
|
pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
|
|
|
|
|
pipe.sync();
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-07-25 10:21:11 +02:00
|
|
|
public Long getUnreadCount(FeedSubscription sub) {
|
|
|
|
|
Long count = null;
|
2013-07-02 18:07:08 +02:00
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
2013-07-25 10:21:11 +02:00
|
|
|
String key = buildRedisUnreadCountKey(sub);
|
|
|
|
|
String countString = jedis.get(key);
|
|
|
|
|
if (countString != null) {
|
|
|
|
|
count = Long.valueOf(countString);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setUnreadCount(FeedSubscription sub, Long count) {
|
|
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
|
|
|
|
String key = buildRedisUnreadCountKey(sub);
|
|
|
|
|
|
2013-07-02 18:07:08 +02:00
|
|
|
Pipeline pipe = jedis.pipelined();
|
2013-07-25 10:21:11 +02:00
|
|
|
pipe.del(key);
|
|
|
|
|
pipe.set(key, String.valueOf(count));
|
|
|
|
|
pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
|
|
|
|
|
pipe.sync();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-07-25 10:38:23 +02:00
|
|
|
public void invalidateUserRootCategory(User... users) {
|
2013-07-25 10:21:11 +02:00
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
2013-07-25 10:38:23 +02:00
|
|
|
Pipeline pipe = jedis.pipelined();
|
|
|
|
|
if (users != null) {
|
|
|
|
|
for (User user : users) {
|
|
|
|
|
String key = buildRedisUserRootCategoryKey(user);
|
|
|
|
|
pipe.del(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pipe.sync();
|
2013-07-25 10:21:11 +02:00
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void invalidateUnreadCount(FeedSubscription... subs) {
|
|
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
|
|
|
|
Pipeline pipe = jedis.pipelined();
|
|
|
|
|
if (subs != null) {
|
|
|
|
|
for (FeedSubscription sub : subs) {
|
|
|
|
|
String key = buildRedisUnreadCountKey(sub);
|
2013-07-03 12:42:01 +02:00
|
|
|
pipe.del(key);
|
2013-07-02 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pipe.sync();
|
|
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 10:21:11 +02:00
|
|
|
private String buildRedisEntryKey(Feed feed) {
|
|
|
|
|
return "f:" + Models.getId(feed);
|
2013-07-02 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-25 10:38:23 +02:00
|
|
|
private String buildRedisUserRootCategoryKey(User user) {
|
2013-07-25 10:21:11 +02:00
|
|
|
return "c:" + Models.getId(user);
|
2013-07-03 12:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-25 10:21:11 +02:00
|
|
|
private String buildRedisUnreadCountKey(FeedSubscription sub) {
|
|
|
|
|
return "u:" + Models.getId(sub);
|
2013-06-29 11:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|