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 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 {
@@ -22,18 +22,11 @@ public abstract class CacheService {
}
// 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);
// subscriptions
public abstract List<FeedSubscription> getUserSubscriptions(User user);
public abstract void setUserSubscriptions(User user, List<FeedSubscription> subs);
public abstract void invalidateUserSubscriptions(User user);
public abstract void invalidateUserRootCategory(User... users);
// unread count
public abstract Long getUnreadCount(FeedSubscription sub);

View File

@@ -7,9 +7,9 @@ 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
@@ -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
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 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.CollectionType;
import com.google.api.client.util.Lists;
@Alternative
@@ -31,9 +30,6 @@ 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");
@@ -72,68 +68,32 @@ public class RedisCacheService extends CacheService {
}
@Override
public List<FeedCategory> getUserCategories(User user) {
List<FeedCategory> cats = null;
public Category getUserRootCategory(User user) {
Category cat = null;
Jedis jedis = pool.getResource();
try {
String key = buildRedisUserCategoriesKey(user);
String key = buildRedisUserRootCategoryKey(user);
String json = jedis.get(key);
if (json != null) {
cats = mapper.readValue(json, TYPE_LIST_CATEGORIES);
cat = mapper.readValue(json, Category.class);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
pool.returnResource(jedis);
}
return cats;
return cat;
}
@Override
public void setUserCategories(User user, List<FeedCategory> categories) {
public void setUserRootCategory(User user, Category category) {
Jedis jedis = pool.getResource();
try {
String key = buildRedisUserCategoriesKey(user);
String key = buildRedisUserRootCategoryKey(user);
Pipeline pipe = jedis.pipelined();
pipe.del(key);
pipe.set(key, mapper.writeValueAsString(categories));
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.set(key, mapper.writeValueAsString(category));
pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
pipe.sync();
} catch (JsonProcessingException e) {
@@ -180,22 +140,17 @@ public class RedisCacheService extends CacheService {
}
@Override
public void invalidateUserCategories(User user) {
public void invalidateUserRootCategory(User... users) {
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);
Pipeline pipe = jedis.pipelined();
if (users != null) {
for (User user : users) {
String key = buildRedisUserRootCategoryKey(user);
pipe.del(key);
}
}
pipe.sync();
} finally {
pool.returnResource(jedis);
}
@@ -222,14 +177,10 @@ public class RedisCacheService extends CacheService {
return "f:" + Models.getId(feed);
}
private String buildRedisUserCategoriesKey(User user) {
private String buildRedisUserRootCategoryKey(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);
}

View File

@@ -4,7 +4,6 @@ import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
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.Feed_;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.google.common.collect.Iterables;
@Stateless
public class FeedEntryDAO extends GenericDAO<FeedEntry> {
@Inject
ApplicationSettingsService applicationSettingsService;
protected static final Logger log = LoggerFactory.getLogger(FeedEntryDAO.class);
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());
}
}
cache.invalidateUserCategories(user);
cache.invalidateUserSubscriptions(user);
cache.invalidateUserRootCategory(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);
feedEntryStatusDAO.saveOrUpdate(status);
cache.invalidateUnreadCount(sub);
cache.invalidateUserRootCategory(user);
}
}
@@ -67,10 +68,11 @@ public class FeedEntryService {
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);
markList(statuses, olderThan);
cache.invalidateUnreadCount(subscriptions.toArray(new FeedSubscription[0]));
cache.invalidateUserRootCategory(user);
}
public void markStarredEntries(User user, Date olderThan) {

View File

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

View File

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