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.FeedCategory;
|
|
|
|
|
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;
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2013-07-25 10:21:11 +02:00
|
|
|
import com.fasterxml.jackson.databind.type.CollectionType;
|
2013-06-29 11:20:53 +02:00
|
|
|
import com.google.api.client.util.Lists;
|
|
|
|
|
|
|
|
|
|
@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();
|
|
|
|
|
private static CollectionType TYPE_LIST_CATEGORIES = mapper.getTypeFactory().constructCollectionType(List.class, FeedCategory.class);
|
|
|
|
|
private static CollectionType TYPE_LIST_SUBSCRIPTIONS = mapper.getTypeFactory().constructCollectionType(List.class,
|
|
|
|
|
FeedSubscription.class);
|
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:21:11 +02:00
|
|
|
public List<FeedCategory> getUserCategories(User user) {
|
|
|
|
|
List<FeedCategory> cats = null;
|
2013-07-02 18:07:08 +02:00
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
2013-07-25 10:21:11 +02:00
|
|
|
String key = buildRedisUserCategoriesKey(user);
|
2013-07-02 18:07:08 +02:00
|
|
|
String json = jedis.get(key);
|
|
|
|
|
if (json != null) {
|
2013-07-25 10:21:11 +02:00
|
|
|
cats = mapper.readValue(json, TYPE_LIST_CATEGORIES);
|
2013-07-02 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
2013-07-25 10:21:11 +02:00
|
|
|
return cats;
|
2013-07-02 18:07:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-07-25 10:21:11 +02:00
|
|
|
public void setUserCategories(User user, List<FeedCategory> categories) {
|
2013-07-02 18:07:08 +02:00
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
2013-07-25 10:21:11 +02:00
|
|
|
String key = buildRedisUserCategoriesKey(user);
|
2013-07-02 18:07:08 +02:00
|
|
|
|
|
|
|
|
Pipeline pipe = jedis.pipelined();
|
|
|
|
|
pipe.del(key);
|
2013-07-25 10:21:11 +02:00
|
|
|
pipe.set(key, mapper.writeValueAsString(categories));
|
2013-07-02 18:07:08 +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 List<FeedSubscription> getUserSubscriptions(User user) {
|
|
|
|
|
List<FeedSubscription> subs = null;
|
2013-07-03 12:42:01 +02:00
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
2013-07-25 10:21:11 +02:00
|
|
|
String key = buildRedisUserSubscriptionsKey(user);
|
2013-07-03 12:42:01 +02:00
|
|
|
String json = jedis.get(key);
|
|
|
|
|
if (json != null) {
|
2013-07-25 10:21:11 +02:00
|
|
|
subs = mapper.readValue(json, TYPE_LIST_SUBSCRIPTIONS);
|
2013-07-03 12:42:01 +02:00
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
2013-07-25 10:21:11 +02:00
|
|
|
return subs;
|
2013-07-03 12:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-07-25 10:21:11 +02:00
|
|
|
public void setUserSubscriptions(User user, List<FeedSubscription> subs) {
|
2013-07-03 12:42:01 +02:00
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
2013-07-25 10:21:11 +02:00
|
|
|
String key = buildRedisUserSubscriptionsKey(user);
|
2013-07-03 12:42:01 +02:00
|
|
|
|
|
|
|
|
Pipeline pipe = jedis.pipelined();
|
|
|
|
|
pipe.del(key);
|
2013-07-25 10:21:11 +02:00
|
|
|
pipe.set(key, mapper.writeValueAsString(subs));
|
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
|
|
|
|
|
public void invalidateUserCategories(User user) {
|
|
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
|
|
|
|
String key = buildRedisUserCategoriesKey(user);
|
|
|
|
|
jedis.del(key);
|
|
|
|
|
} finally {
|
|
|
|
|
pool.returnResource(jedis);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void invalidateUserSubscriptions(User user) {
|
|
|
|
|
Jedis jedis = pool.getResource();
|
|
|
|
|
try {
|
|
|
|
|
String key = buildRedisUserSubscriptionsKey(user);
|
|
|
|
|
jedis.del(key);
|
|
|
|
|
} 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:21:11 +02:00
|
|
|
private String buildRedisUserCategoriesKey(User user) {
|
|
|
|
|
return "c:" + Models.getId(user);
|
2013-07-03 12:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-25 10:21:11 +02:00
|
|
|
private String buildRedisUserSubscriptionsKey(User user) {
|
|
|
|
|
return "s:" + Models.getId(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String buildRedisUnreadCountKey(FeedSubscription sub) {
|
|
|
|
|
return "u:" + Models.getId(sub);
|
2013-06-29 11:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|