revert part of caching mechanism

This commit is contained in:
Athou
2013-07-25 10:38:23 +02:00
parent 73f2871235
commit c618e22c52
11 changed files with 69 additions and 188 deletions

View File

@@ -5,10 +5,10 @@ import java.util.List;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import com.commafeed.backend.model.Feed; import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedEntry; import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedSubscription; import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User; import com.commafeed.backend.model.User;
import com.commafeed.frontend.model.Category;
public abstract class CacheService { public abstract class CacheService {
@@ -22,18 +22,11 @@ public abstract class CacheService {
} }
// user categories // user categories
public abstract List<FeedCategory> getUserCategories(User user); public abstract Category getUserRootCategory(User user);
public abstract void setUserCategories(User user, List<FeedCategory> categories); public abstract void setUserRootCategory(User user, Category category);
public abstract void invalidateUserCategories(User user); public abstract void invalidateUserRootCategory(User... users);
// subscriptions
public abstract List<FeedSubscription> getUserSubscriptions(User user);
public abstract void setUserSubscriptions(User user, List<FeedSubscription> subs);
public abstract void invalidateUserSubscriptions(User user);
// unread count // unread count
public abstract Long getUnreadCount(FeedSubscription sub); public abstract Long getUnreadCount(FeedSubscription sub);

View File

@@ -7,9 +7,9 @@ import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Alternative; import javax.enterprise.inject.Alternative;
import com.commafeed.backend.model.Feed; import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedSubscription; import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User; import com.commafeed.backend.model.User;
import com.commafeed.frontend.model.Category;
@Alternative @Alternative
@ApplicationScoped @ApplicationScoped
@@ -34,39 +34,24 @@ public class NoopCacheService extends CacheService {
} }
@Override
public List<FeedCategory> getUserCategories(User user) {
return null;
}
@Override
public void setUserCategories(User user, List<FeedCategory> categories) {
}
@Override
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 @Override
public void invalidateUnreadCount(FeedSubscription... subs) { public void invalidateUnreadCount(FeedSubscription... subs) {
} }
@Override
public Category getUserRootCategory(User user) {
return null;
}
@Override
public void setUserRootCategory(User user, Category category) {
}
@Override
public void invalidateUserRootCategory(User... users) {
}
} }

View File

@@ -16,13 +16,12 @@ import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline; import redis.clients.jedis.Pipeline;
import com.commafeed.backend.model.Feed; import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedSubscription; import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.Models; import com.commafeed.backend.model.Models;
import com.commafeed.backend.model.User; import com.commafeed.backend.model.User;
import com.commafeed.frontend.model.Category;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.google.api.client.util.Lists; import com.google.api.client.util.Lists;
@Alternative @Alternative
@@ -31,9 +30,6 @@ public class RedisCacheService extends CacheService {
private static final Logger log = LoggerFactory.getLogger(RedisCacheService.class); private static final Logger log = LoggerFactory.getLogger(RedisCacheService.class);
private static ObjectMapper mapper = new ObjectMapper(); 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 JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
@@ -72,68 +68,32 @@ public class RedisCacheService extends CacheService {
} }
@Override @Override
public List<FeedCategory> getUserCategories(User user) { public Category getUserRootCategory(User user) {
List<FeedCategory> cats = null; Category cat = null;
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
try { try {
String key = buildRedisUserCategoriesKey(user); String key = buildRedisUserRootCategoryKey(user);
String json = jedis.get(key); String json = jedis.get(key);
if (json != null) { if (json != null) {
cats = mapper.readValue(json, TYPE_LIST_CATEGORIES); cat = mapper.readValue(json, Category.class);
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} finally { } finally {
pool.returnResource(jedis); pool.returnResource(jedis);
} }
return cats; return cat;
} }
@Override @Override
public void setUserCategories(User user, List<FeedCategory> categories) { public void setUserRootCategory(User user, Category category) {
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
try { try {
String key = buildRedisUserCategoriesKey(user); String key = buildRedisUserRootCategoryKey(user);
Pipeline pipe = jedis.pipelined(); Pipeline pipe = jedis.pipelined();
pipe.del(key); pipe.del(key);
pipe.set(key, mapper.writeValueAsString(categories)); pipe.set(key, mapper.writeValueAsString(category));
pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
pipe.sync();
} catch (JsonProcessingException e) {
log.error(e.getMessage(), e);
} finally {
pool.returnResource(jedis);
}
}
@Override
public List<FeedSubscription> getUserSubscriptions(User user) {
List<FeedSubscription> subs = null;
Jedis jedis = pool.getResource();
try {
String key = buildRedisUserSubscriptionsKey(user);
String json = jedis.get(key);
if (json != null) {
subs = mapper.readValue(json, TYPE_LIST_SUBSCRIPTIONS);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
pool.returnResource(jedis);
}
return subs;
}
@Override
public void setUserSubscriptions(User user, List<FeedSubscription> subs) {
Jedis jedis = pool.getResource();
try {
String key = buildRedisUserSubscriptionsKey(user);
Pipeline pipe = jedis.pipelined();
pipe.del(key);
pipe.set(key, mapper.writeValueAsString(subs));
pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30)); pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
pipe.sync(); pipe.sync();
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
@@ -180,22 +140,17 @@ public class RedisCacheService extends CacheService {
} }
@Override @Override
public void invalidateUserCategories(User user) { public void invalidateUserRootCategory(User... users) {
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
try { try {
String key = buildRedisUserCategoriesKey(user); Pipeline pipe = jedis.pipelined();
jedis.del(key); if (users != null) {
} finally { for (User user : users) {
pool.returnResource(jedis); String key = buildRedisUserRootCategoryKey(user);
pipe.del(key);
} }
} }
pipe.sync();
@Override
public void invalidateUserSubscriptions(User user) {
Jedis jedis = pool.getResource();
try {
String key = buildRedisUserSubscriptionsKey(user);
jedis.del(key);
} finally { } finally {
pool.returnResource(jedis); pool.returnResource(jedis);
} }
@@ -222,14 +177,10 @@ public class RedisCacheService extends CacheService {
return "f:" + Models.getId(feed); return "f:" + Models.getId(feed);
} }
private String buildRedisUserCategoriesKey(User user) { private String buildRedisUserRootCategoryKey(User user) {
return "c:" + Models.getId(user); return "c:" + Models.getId(user);
} }
private String buildRedisUserSubscriptionsKey(User user) {
return "s:" + Models.getId(user);
}
private String buildRedisUnreadCountKey(FeedSubscription sub) { private String buildRedisUnreadCountKey(FeedSubscription sub) {
return "u:" + Models.getId(sub); return "u:" + Models.getId(sub);
} }

View File

@@ -4,7 +4,6 @@ import java.util.Date;
import java.util.List; import java.util.List;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
@@ -17,15 +16,11 @@ import org.slf4j.LoggerFactory;
import com.commafeed.backend.model.FeedEntry; import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedEntry_; import com.commafeed.backend.model.FeedEntry_;
import com.commafeed.backend.model.Feed_; import com.commafeed.backend.model.Feed_;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@Stateless @Stateless
public class FeedEntryDAO extends GenericDAO<FeedEntry> { public class FeedEntryDAO extends GenericDAO<FeedEntry> {
@Inject
ApplicationSettingsService applicationSettingsService;
protected static final Logger log = LoggerFactory.getLogger(FeedEntryDAO.class); protected static final Logger log = LoggerFactory.getLogger(FeedEntryDAO.class);
public FeedEntry findExisting(String guid, String url, Long feedId) { public FeedEntry findExisting(String guid, String url, Long feedId) {

View File

@@ -97,7 +97,6 @@ public class OPMLImporter {
log.error("error while importing {}: {}", outline.getXmlUrl(), e.getMessage()); log.error("error while importing {}: {}", outline.getXmlUrl(), e.getMessage());
} }
} }
cache.invalidateUserCategories(user); cache.invalidateUserRootCategory(user);
cache.invalidateUserSubscriptions(user);
} }
} }

View File

@@ -1,35 +0,0 @@
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;
}
}

View File

@@ -47,6 +47,7 @@ public class FeedEntryService {
status.setRead(read); status.setRead(read);
feedEntryStatusDAO.saveOrUpdate(status); feedEntryStatusDAO.saveOrUpdate(status);
cache.invalidateUnreadCount(sub); cache.invalidateUnreadCount(sub);
cache.invalidateUserRootCategory(user);
} }
} }
@@ -67,10 +68,11 @@ public class FeedEntryService {
feedEntryStatusDAO.saveOrUpdate(status); feedEntryStatusDAO.saveOrUpdate(status);
} }
public void markSubscriptionEntries(List<FeedSubscription> subscriptions, Date olderThan) { public void markSubscriptionEntries(User user, List<FeedSubscription> subscriptions, Date olderThan) {
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(subscriptions, true, null, null, -1, -1, null, false); List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(subscriptions, true, null, null, -1, -1, null, false);
markList(statuses, olderThan); markList(statuses, olderThan);
cache.invalidateUnreadCount(subscriptions.toArray(new FeedSubscription[0])); cache.invalidateUnreadCount(subscriptions.toArray(new FeedSubscription[0]));
cache.invalidateUserRootCategory(user);
} }
public void markStarredEntries(User user, Date olderThan) { public void markStarredEntries(User user, Date olderThan) {

View File

@@ -80,7 +80,7 @@ public class FeedSubscriptionService {
feedSubscriptionDAO.saveOrUpdate(sub); feedSubscriptionDAO.saveOrUpdate(sub);
taskGiver.add(feed); taskGiver.add(feed);
cache.invalidateUserSubscriptions(user); cache.invalidateUserRootCategory(user);
return feed; return feed;
} }
@@ -88,23 +88,13 @@ public class FeedSubscriptionService {
FeedSubscription sub = feedSubscriptionDAO.findById(user, subId); FeedSubscription sub = feedSubscriptionDAO.findById(user, subId);
if (sub != null) { if (sub != null) {
feedSubscriptionDAO.delete(sub); feedSubscriptionDAO.delete(sub);
cache.invalidateUserSubscriptions(user); cache.invalidateUserRootCategory(user);
return true; return true;
} else { } else {
return false; 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) { public Long getUnreadCount(FeedSubscription sub) {
Long count = cache.getUnreadCount(sub); Long count = cache.getUnreadCount(sub);
if (count == null) { if (count == null) {
@@ -117,7 +107,7 @@ public class FeedSubscriptionService {
public Map<Long, Long> getUnreadCount(User user) { public Map<Long, Long> getUnreadCount(User user) {
Map<Long, Long> map = Maps.newHashMap(); Map<Long, Long> map = Maps.newHashMap();
List<FeedSubscription> subs = getSubscriptions(user); List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
for (FeedSubscription sub : subs) { for (FeedSubscription sub : subs) {
map.put(sub.getId(), getUnreadCount(sub)); map.put(sub.getId(), getUnreadCount(sub));
} }

View File

@@ -70,5 +70,6 @@ public class FeedUpdateService {
feedEntryDAO.saveOrUpdate(entry); feedEntryDAO.saveOrUpdate(entry);
metricsBean.entryInserted(); metricsBean.entryInserted();
cache.invalidateUnreadCount(subscriptions.toArray(new FeedSubscription[0])); cache.invalidateUnreadCount(subscriptions.toArray(new FeedSubscription[0]));
cache.invalidateUserRootCategory(users.toArray(new User[0]));
} }
} }

View File

@@ -33,7 +33,6 @@ import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User; import com.commafeed.backend.model.User;
import com.commafeed.backend.model.UserRole.Role; import com.commafeed.backend.model.UserRole.Role;
import com.commafeed.backend.model.UserSettings.ReadingOrder; import com.commafeed.backend.model.UserSettings.ReadingOrder;
import com.commafeed.backend.services.FeedCategoryService;
import com.commafeed.backend.services.FeedEntryService; import com.commafeed.backend.services.FeedEntryService;
import com.commafeed.backend.services.FeedSubscriptionService; import com.commafeed.backend.services.FeedSubscriptionService;
import com.commafeed.frontend.SecurityCheck; import com.commafeed.frontend.SecurityCheck;
@@ -82,9 +81,6 @@ public class CategoryREST extends AbstractResourceREST {
@Inject @Inject
FeedSubscriptionService feedSubscriptionService; FeedSubscriptionService feedSubscriptionService;
@Inject
FeedCategoryService feedCategoryService;
@Inject @Inject
CacheService cache; CacheService cache;
@@ -214,15 +210,15 @@ public class CategoryREST extends AbstractResourceREST {
Date olderThan = req.getOlderThan() == null ? null : new Date(req.getOlderThan()); Date olderThan = req.getOlderThan() == null ? null : new Date(req.getOlderThan());
if (ALL.equals(req.getId())) { if (ALL.equals(req.getId())) {
List<FeedSubscription> subscriptions = feedSubscriptionService.getSubscriptions(getUser()); List<FeedSubscription> subscriptions = feedSubscriptionDAO.findAll(getUser());
feedEntryService.markSubscriptionEntries(subscriptions, olderThan); feedEntryService.markSubscriptionEntries(getUser(), subscriptions, olderThan);
} else if (STARRED.equals(req.getId())) { } else if (STARRED.equals(req.getId())) {
feedEntryService.markStarredEntries(getUser(), olderThan); feedEntryService.markStarredEntries(getUser(), olderThan);
} else { } else {
FeedCategory parent = feedCategoryDAO.findById(getUser(), Long.valueOf(req.getId())); FeedCategory parent = feedCategoryDAO.findById(getUser(), Long.valueOf(req.getId()));
List<FeedCategory> categories = feedCategoryDAO.findAllChildrenCategories(getUser(), parent); List<FeedCategory> categories = feedCategoryDAO.findAllChildrenCategories(getUser(), parent);
List<FeedSubscription> subs = feedSubscriptionDAO.findByCategories(getUser(), categories); List<FeedSubscription> subs = feedSubscriptionDAO.findByCategories(getUser(), categories);
feedEntryService.markSubscriptionEntries(subs, olderThan); feedEntryService.markSubscriptionEntries(getUser(), subs, olderThan);
} }
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@@ -245,7 +241,7 @@ public class CategoryREST extends AbstractResourceREST {
cat.setParent(parent); cat.setParent(parent);
} }
feedCategoryDAO.saveOrUpdate(cat); feedCategoryDAO.saveOrUpdate(cat);
cache.invalidateUserCategories(getUser()); cache.invalidateUserRootCategory(getUser());
return Response.ok().build(); return Response.ok().build();
} }
@@ -273,7 +269,7 @@ public class CategoryREST extends AbstractResourceREST {
feedCategoryDAO.saveOrUpdate(categories); feedCategoryDAO.saveOrUpdate(categories);
feedCategoryDAO.delete(cat); feedCategoryDAO.delete(cat);
cache.invalidateUserCategories(getUser()); cache.invalidateUserRootCategory(getUser());
return Response.ok().build(); return Response.ok().build();
} else { } else {
return Response.status(Status.NOT_FOUND).build(); return Response.status(Status.NOT_FOUND).build();
@@ -329,7 +325,7 @@ public class CategoryREST extends AbstractResourceREST {
} }
feedCategoryDAO.saveOrUpdate(category); feedCategoryDAO.saveOrUpdate(category);
cache.invalidateUserCategories(getUser()); cache.invalidateUserRootCategory(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@@ -346,7 +342,7 @@ public class CategoryREST extends AbstractResourceREST {
} }
category.setCollapsed(req.isCollapse()); category.setCollapsed(req.isCollapse());
feedCategoryDAO.saveOrUpdate(category); feedCategoryDAO.saveOrUpdate(category);
cache.invalidateUserCategories(getUser()); cache.invalidateUserRootCategory(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@@ -371,14 +367,18 @@ public class CategoryREST extends AbstractResourceREST {
public Response getSubscriptions() { public Response getSubscriptions() {
User user = getUser(); User user = getUser();
Category root = null; Category root = cache.getUserRootCategory(user);
List<FeedCategory> categories = feedCategoryService.getCategories(user); if (root == null) {
List<FeedSubscription> subscriptions = feedSubscriptionService.getSubscriptions(getUser()); log.debug("cat miss for {}", user.getId());
Map<Long, Long> unreadCount = feedSubscriptionService.getUnreadCount(getUser()); List<FeedCategory> categories = feedCategoryDAO.findAll(user);
List<FeedSubscription> subscriptions = feedSubscriptionDAO.findAll(user);
Map<Long, Long> unreadCount = feedSubscriptionService.getUnreadCount(user);
root = buildCategory(null, categories, subscriptions, unreadCount); root = buildCategory(null, categories, subscriptions, unreadCount);
root.setId("all"); root.setId("all");
root.setName("All"); root.setName("All");
cache.setUserRootCategory(user, root);
}
return Response.ok(root).build(); return Response.ok(root).build();
} }

View File

@@ -278,7 +278,7 @@ public class FeedREST extends AbstractResourceREST {
FeedSubscription subscription = feedSubscriptionDAO.findById(getUser(), Long.valueOf(req.getId())); FeedSubscription subscription = feedSubscriptionDAO.findById(getUser(), Long.valueOf(req.getId()));
if (subscription != null) { if (subscription != null) {
feedEntryService.markSubscriptionEntries(Arrays.asList(subscription), olderThan); feedEntryService.markSubscriptionEntries(getUser(), Arrays.asList(subscription), olderThan);
} }
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@@ -446,7 +446,7 @@ public class FeedREST extends AbstractResourceREST {
} else { } else {
feedSubscriptionDAO.saveOrUpdate(subscription); feedSubscriptionDAO.saveOrUpdate(subscription);
} }
cache.invalidateUserSubscriptions(getUser()); cache.invalidateUserRootCategory(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }