mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
revamp cache service
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
package com.commafeed.backend.cache;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.frontend.model.Category;
|
||||
|
||||
public abstract class CacheService {
|
||||
|
||||
// feed entries for faster refresh
|
||||
public abstract List<String> getLastEntries(Feed feed);
|
||||
|
||||
public abstract void setLastEntries(Feed feed, List<String> entries);
|
||||
@@ -20,14 +21,25 @@ public abstract class CacheService {
|
||||
return DigestUtils.sha1Hex(entry.getGuid() + entry.getUrl());
|
||||
}
|
||||
|
||||
public abstract Category getRootCategory(User user);
|
||||
// user categories
|
||||
public abstract List<FeedCategory> getUserCategories(User user);
|
||||
|
||||
public abstract void setRootCategory(User user, Category category);
|
||||
public abstract void setUserCategories(User user, List<FeedCategory> categories);
|
||||
|
||||
public abstract Map<Long, Long> getUnreadCounts(User user);
|
||||
public abstract void invalidateUserCategories(User user);
|
||||
|
||||
public abstract void setUnreadCounts(User user, Map<Long, Long> map);
|
||||
// subscriptions
|
||||
public abstract List<FeedSubscription> getUserSubscriptions(User user);
|
||||
|
||||
public abstract void invalidateUserData(User... users);
|
||||
public abstract void setUserSubscriptions(User user, List<FeedSubscription> subs);
|
||||
|
||||
public abstract void invalidateUserSubscriptions(User user);
|
||||
|
||||
// unread count
|
||||
public abstract Long getUnreadCount(FeedSubscription sub);
|
||||
|
||||
public abstract void setUnreadCount(FeedSubscription sub, Long count);
|
||||
|
||||
public abstract void invalidateUnreadCount(FeedSubscription... subs);
|
||||
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ package com.commafeed.backend.cache;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.inject.Alternative;
|
||||
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.frontend.model.Category;
|
||||
|
||||
@Alternative
|
||||
@ApplicationScoped
|
||||
@@ -25,27 +25,47 @@ public class NoopCacheService extends CacheService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Category getRootCategory(User user) {
|
||||
public Long getUnreadCount(FeedSubscription sub) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRootCategory(User user, Category category) {
|
||||
public void setUnreadCount(FeedSubscription sub, Long count) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, Long> getUnreadCounts(User user) {
|
||||
public List<FeedCategory> getUserCategories(User user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUnreadCounts(User user, Map<Long, Long> map) {
|
||||
public void setUserCategories(User user, List<FeedCategory> categories) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateUserData(User... users) {
|
||||
public void invalidateUserCategories(User user) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FeedSubscription> getUserSubscriptions(User user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUserSubscriptions(User user, List<FeedSubscription> subs) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateUserSubscriptions(User user) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateUnreadCount(FeedSubscription... subs) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.commafeed.backend.cache;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -17,12 +16,13 @@ import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.Pipeline;
|
||||
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.Models;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.frontend.model.Category;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.MapType;
|
||||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
import com.google.api.client.util.Lists;
|
||||
|
||||
@Alternative
|
||||
@@ -30,9 +30,12 @@ import com.google.api.client.util.Lists;
|
||||
public class RedisCacheService extends CacheService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(RedisCacheService.class);
|
||||
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);
|
||||
|
||||
private JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
|
||||
private ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public List<String> getLastEntries(Feed feed) {
|
||||
@@ -69,32 +72,32 @@ public class RedisCacheService extends CacheService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Category getRootCategory(User user) {
|
||||
Category cat = null;
|
||||
public List<FeedCategory> getUserCategories(User user) {
|
||||
List<FeedCategory> cats = null;
|
||||
Jedis jedis = pool.getResource();
|
||||
try {
|
||||
String key = buildRedisRootCategoryKey(user);
|
||||
String key = buildRedisUserCategoriesKey(user);
|
||||
String json = jedis.get(key);
|
||||
if (json != null) {
|
||||
cat = mapper.readValue(json, Category.class);
|
||||
cats = mapper.readValue(json, TYPE_LIST_CATEGORIES);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
pool.returnResource(jedis);
|
||||
}
|
||||
return cat;
|
||||
return cats;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRootCategory(User user, Category category) {
|
||||
public void setUserCategories(User user, List<FeedCategory> categories) {
|
||||
Jedis jedis = pool.getResource();
|
||||
try {
|
||||
String key = buildRedisRootCategoryKey(user);
|
||||
String key = buildRedisUserCategoriesKey(user);
|
||||
|
||||
Pipeline pipe = jedis.pipelined();
|
||||
pipe.del(key);
|
||||
pipe.set(key, mapper.writeValueAsString(category));
|
||||
pipe.set(key, mapper.writeValueAsString(categories));
|
||||
pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
|
||||
pipe.sync();
|
||||
} catch (JsonProcessingException e) {
|
||||
@@ -105,33 +108,32 @@ public class RedisCacheService extends CacheService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, Long> getUnreadCounts(User user) {
|
||||
Map<Long, Long> map = null;
|
||||
public List<FeedSubscription> getUserSubscriptions(User user) {
|
||||
List<FeedSubscription> subs = null;
|
||||
Jedis jedis = pool.getResource();
|
||||
try {
|
||||
String key = buildRedisUnreadCountKey(user);
|
||||
String key = buildRedisUserSubscriptionsKey(user);
|
||||
String json = jedis.get(key);
|
||||
if (json != null) {
|
||||
MapType type = mapper.getTypeFactory().constructMapType(Map.class, Long.class, Long.class);
|
||||
map = mapper.readValue(json, type);
|
||||
subs = mapper.readValue(json, TYPE_LIST_SUBSCRIPTIONS);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
pool.returnResource(jedis);
|
||||
}
|
||||
return map;
|
||||
return subs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUnreadCounts(User user, Map<Long, Long> map) {
|
||||
public void setUserSubscriptions(User user, List<FeedSubscription> subs) {
|
||||
Jedis jedis = pool.getResource();
|
||||
try {
|
||||
String key = buildRedisUnreadCountKey(user);
|
||||
String key = buildRedisUserSubscriptionsKey(user);
|
||||
|
||||
Pipeline pipe = jedis.pipelined();
|
||||
pipe.del(key);
|
||||
pipe.set(key, mapper.writeValueAsString(map));
|
||||
pipe.set(key, mapper.writeValueAsString(subs));
|
||||
pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
|
||||
pipe.sync();
|
||||
} catch (JsonProcessingException e) {
|
||||
@@ -142,15 +144,71 @@ public class RedisCacheService extends CacheService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateUserData(User... users) {
|
||||
public Long getUnreadCount(FeedSubscription sub) {
|
||||
Long count = null;
|
||||
Jedis jedis = pool.getResource();
|
||||
try {
|
||||
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);
|
||||
|
||||
Pipeline pipe = jedis.pipelined();
|
||||
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 (users != null) {
|
||||
for (User user : users) {
|
||||
String key = buildRedisRootCategoryKey(user);
|
||||
pipe.del(key);
|
||||
key = buildRedisUnreadCountKey(user);
|
||||
if (subs != null) {
|
||||
for (FeedSubscription sub : subs) {
|
||||
String key = buildRedisUnreadCountKey(sub);
|
||||
pipe.del(key);
|
||||
}
|
||||
}
|
||||
@@ -160,16 +218,20 @@ public class RedisCacheService extends CacheService {
|
||||
}
|
||||
}
|
||||
|
||||
private String buildRedisRootCategoryKey(User user) {
|
||||
return "root_cat:" + Models.getId(user);
|
||||
}
|
||||
|
||||
private String buildRedisUnreadCountKey(User user) {
|
||||
return "unread_count:" + Models.getId(user);
|
||||
}
|
||||
|
||||
private String buildRedisEntryKey(Feed feed) {
|
||||
return "feed:" + feed.getId();
|
||||
return "f:" + Models.getId(feed);
|
||||
}
|
||||
|
||||
private String buildRedisUserCategoriesKey(User user) {
|
||||
return "c:" + Models.getId(user);
|
||||
}
|
||||
|
||||
private String buildRedisUserSubscriptionsKey(User user) {
|
||||
return "s:" + Models.getId(user);
|
||||
}
|
||||
|
||||
private String buildRedisUnreadCountKey(FeedSubscription sub) {
|
||||
return "u:" + Models.getId(sub);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -266,28 +266,4 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
|
||||
setTimeout(query, applicationSettingsService.get().getQueryTimeout());
|
||||
}
|
||||
|
||||
public void markSubscriptionEntries(List<FeedSubscription> subscriptions, Date olderThan) {
|
||||
List<FeedEntryStatus> statuses = findBySubscriptions(subscriptions, true, null, null, -1, -1, null, false);
|
||||
markList(statuses, olderThan);
|
||||
}
|
||||
|
||||
public void markStarredEntries(User user, Date olderThan) {
|
||||
List<FeedEntryStatus> statuses = findStarred(user, null, -1, -1, null, false);
|
||||
markList(statuses, olderThan);
|
||||
}
|
||||
|
||||
private void markList(List<FeedEntryStatus> statuses, Date olderThan) {
|
||||
List<FeedEntryStatus> list = Lists.newArrayList();
|
||||
for (FeedEntryStatus status : statuses) {
|
||||
if (!status.isRead()) {
|
||||
Date inserted = status.getEntry().getInserted();
|
||||
if (olderThan == null || inserted == null || olderThan.after(inserted)) {
|
||||
status.setRead(true);
|
||||
list.add(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
saveOrUpdate(list);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -97,6 +97,7 @@ public class OPMLImporter {
|
||||
log.error("error while importing {}: {}", outline.getXmlUrl(), e.getMessage());
|
||||
}
|
||||
}
|
||||
cache.invalidateUserData(user);
|
||||
cache.invalidateUserCategories(user);
|
||||
cache.invalidateUserSubscriptions(user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.commafeed.backend.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.commafeed.backend.cache.CacheService;
|
||||
import com.commafeed.backend.dao.FeedCategoryDAO;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.Models;
|
||||
import com.commafeed.backend.model.User;
|
||||
|
||||
public class FeedCategoryService {
|
||||
private static Logger log = LoggerFactory.getLogger(FeedSubscriptionService.class);
|
||||
|
||||
@Inject
|
||||
FeedCategoryDAO feedCategoryDAO;
|
||||
|
||||
@Inject
|
||||
CacheService cache;
|
||||
|
||||
public List<FeedCategory> getCategories(User user) {
|
||||
List<FeedCategory> list = cache.getUserCategories(user);
|
||||
if (list == null) {
|
||||
log.debug("cat list miss for {}", Models.getId(user));
|
||||
list = feedCategoryDAO.findAll(user);
|
||||
cache.setUserCategories(user, list);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.commafeed.backend.services;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import com.commafeed.backend.cache.CacheService;
|
||||
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
||||
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
||||
@@ -10,6 +14,7 @@ import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntryStatus;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@Stateless
|
||||
public class FeedEntryService {
|
||||
@@ -23,6 +28,9 @@ public class FeedEntryService {
|
||||
@Inject
|
||||
FeedEntryDAO feedEntryDAO;
|
||||
|
||||
@Inject
|
||||
CacheService cache;
|
||||
|
||||
public void markEntry(User user, Long entryId, Long subscriptionId, boolean read) {
|
||||
FeedSubscription sub = feedSubscriptionDAO.findById(user, subscriptionId);
|
||||
if (sub == null) {
|
||||
@@ -38,6 +46,7 @@ public class FeedEntryService {
|
||||
if (status.isMarkable()) {
|
||||
status.setRead(read);
|
||||
feedEntryStatusDAO.saveOrUpdate(status);
|
||||
cache.invalidateUnreadCount(sub);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,4 +66,29 @@ public class FeedEntryService {
|
||||
status.setStarred(starred);
|
||||
feedEntryStatusDAO.saveOrUpdate(status);
|
||||
}
|
||||
|
||||
public void markSubscriptionEntries(List<FeedSubscription> subscriptions, Date olderThan) {
|
||||
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(subscriptions, true, null, null, -1, -1, null, false);
|
||||
markList(statuses, olderThan);
|
||||
cache.invalidateUnreadCount(subscriptions.toArray(new FeedSubscription[0]));
|
||||
}
|
||||
|
||||
public void markStarredEntries(User user, Date olderThan) {
|
||||
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findStarred(user, null, -1, -1, null, false);
|
||||
markList(statuses, olderThan);
|
||||
}
|
||||
|
||||
private void markList(List<FeedEntryStatus> statuses, Date olderThan) {
|
||||
List<FeedEntryStatus> list = Lists.newArrayList();
|
||||
for (FeedEntryStatus status : statuses) {
|
||||
if (!status.isRead()) {
|
||||
Date inserted = status.getEntry().getInserted();
|
||||
if (olderThan == null || inserted == null || olderThan.after(inserted)) {
|
||||
status.setRead(true);
|
||||
list.add(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
feedEntryStatusDAO.saveOrUpdate(list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,20 +80,48 @@ public class FeedSubscriptionService {
|
||||
feedSubscriptionDAO.saveOrUpdate(sub);
|
||||
|
||||
taskGiver.add(feed);
|
||||
cache.invalidateUserSubscriptions(user);
|
||||
return feed;
|
||||
}
|
||||
|
||||
public boolean unsubscribe(User user, Long subId){
|
||||
FeedSubscription sub = feedSubscriptionDAO.findById(user, subId);
|
||||
if (sub != null) {
|
||||
feedSubscriptionDAO.delete(sub);
|
||||
cache.invalidateUserSubscriptions(user);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public List<FeedSubscription> getSubscriptions(User user) {
|
||||
List<FeedSubscription> list = cache.getUserSubscriptions(user);
|
||||
if (list == null) {
|
||||
log.debug("sub list miss for {}", Models.getId(user));
|
||||
list = feedSubscriptionDAO.findAll(user);
|
||||
cache.setUserSubscriptions(user, list);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public Long getUnreadCount(FeedSubscription sub) {
|
||||
Long count = cache.getUnreadCount(sub);
|
||||
if (count == null) {
|
||||
log.debug("unread count cache miss for {}", Models.getId(sub));
|
||||
count = feedEntryStatusDAO.getUnreadCount(sub);
|
||||
cache.setUnreadCount(sub, count);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public Map<Long, Long> getUnreadCount(User user) {
|
||||
Map<Long, Long> map = cache.getUnreadCounts(user);
|
||||
if (map == null) {
|
||||
log.debug("unread count cache miss for {}", Models.getId(user));
|
||||
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
|
||||
map = Maps.newHashMap();
|
||||
for (FeedSubscription sub : subs) {
|
||||
map.put(sub.getId(), feedEntryStatusDAO.getUnreadCount(sub));
|
||||
}
|
||||
cache.setUnreadCounts(user, map);
|
||||
Map<Long, Long> map = Maps.newHashMap();
|
||||
List<FeedSubscription> subs = getSubscriptions(user);
|
||||
for (FeedSubscription sub : subs) {
|
||||
map.put(sub.getId(), getUnreadCount(sub));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -69,6 +69,6 @@ public class FeedUpdateService {
|
||||
}
|
||||
feedEntryDAO.saveOrUpdate(entry);
|
||||
metricsBean.entryInserted();
|
||||
cache.invalidateUserData(users.toArray(new User[0]));
|
||||
cache.invalidateUnreadCount(subscriptions.toArray(new FeedSubscription[0]));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user