revamp cache service

This commit is contained in:
Athou
2013-07-25 10:21:11 +02:00
parent bdb30a60c3
commit 73f2871235
13 changed files with 285 additions and 124 deletions

View File

@@ -1,17 +1,18 @@
package com.commafeed.backend.cache; package com.commafeed.backend.cache;
import java.util.List; import java.util.List;
import java.util.Map;
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.User; import com.commafeed.backend.model.User;
import com.commafeed.frontend.model.Category;
public abstract class CacheService { public abstract class CacheService {
// feed entries for faster refresh
public abstract List<String> getLastEntries(Feed feed); public abstract List<String> getLastEntries(Feed feed);
public abstract void setLastEntries(Feed feed, List<String> entries); public abstract void setLastEntries(Feed feed, List<String> entries);
@@ -20,14 +21,25 @@ public abstract class CacheService {
return DigestUtils.sha1Hex(entry.getGuid() + entry.getUrl()); 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);
} }

View File

@@ -2,14 +2,14 @@ package com.commafeed.backend.cache;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped; 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.User; import com.commafeed.backend.model.User;
import com.commafeed.frontend.model.Category;
@Alternative @Alternative
@ApplicationScoped @ApplicationScoped
@@ -25,27 +25,47 @@ public class NoopCacheService extends CacheService {
} }
@Override @Override
public Category getRootCategory(User user) { public Long getUnreadCount(FeedSubscription sub) {
return null; return null;
} }
@Override @Override
public void setRootCategory(User user, Category category) { public void setUnreadCount(FeedSubscription sub, Long count) {
} }
@Override @Override
public Map<Long, Long> getUnreadCounts(User user) { public List<FeedCategory> getUserCategories(User user) {
return null; return null;
} }
@Override @Override
public void setUnreadCounts(User user, Map<Long, Long> map) { public void setUserCategories(User user, List<FeedCategory> categories) {
} }
@Override @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) {
} }

View File

@@ -1,7 +1,6 @@
package com.commafeed.backend.cache; package com.commafeed.backend.cache;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -17,12 +16,13 @@ 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.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.MapType; import com.fasterxml.jackson.databind.type.CollectionType;
import com.google.api.client.util.Lists; import com.google.api.client.util.Lists;
@Alternative @Alternative
@@ -30,9 +30,12 @@ import com.google.api.client.util.Lists;
public class RedisCacheService extends CacheService { 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 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");
private ObjectMapper mapper = new ObjectMapper();
@Override @Override
public List<String> getLastEntries(Feed feed) { public List<String> getLastEntries(Feed feed) {
@@ -69,32 +72,32 @@ public class RedisCacheService extends CacheService {
} }
@Override @Override
public Category getRootCategory(User user) { public List<FeedCategory> getUserCategories(User user) {
Category cat = null; List<FeedCategory> cats = null;
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
try { try {
String key = buildRedisRootCategoryKey(user); String key = buildRedisUserCategoriesKey(user);
String json = jedis.get(key); String json = jedis.get(key);
if (json != null) { if (json != null) {
cat = mapper.readValue(json, Category.class); cats = mapper.readValue(json, TYPE_LIST_CATEGORIES);
} }
} 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 cat; return cats;
} }
@Override @Override
public void setRootCategory(User user, Category category) { public void setUserCategories(User user, List<FeedCategory> categories) {
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
try { try {
String key = buildRedisRootCategoryKey(user); String key = buildRedisUserCategoriesKey(user);
Pipeline pipe = jedis.pipelined(); Pipeline pipe = jedis.pipelined();
pipe.del(key); pipe.del(key);
pipe.set(key, mapper.writeValueAsString(category)); pipe.set(key, mapper.writeValueAsString(categories));
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) {
@@ -105,33 +108,32 @@ public class RedisCacheService extends CacheService {
} }
@Override @Override
public Map<Long, Long> getUnreadCounts(User user) { public List<FeedSubscription> getUserSubscriptions(User user) {
Map<Long, Long> map = null; List<FeedSubscription> subs = null;
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
try { try {
String key = buildRedisUnreadCountKey(user); String key = buildRedisUserSubscriptionsKey(user);
String json = jedis.get(key); String json = jedis.get(key);
if (json != null) { if (json != null) {
MapType type = mapper.getTypeFactory().constructMapType(Map.class, Long.class, Long.class); subs = mapper.readValue(json, TYPE_LIST_SUBSCRIPTIONS);
map = mapper.readValue(json, type);
} }
} 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 map; return subs;
} }
@Override @Override
public void setUnreadCounts(User user, Map<Long, Long> map) { public void setUserSubscriptions(User user, List<FeedSubscription> subs) {
Jedis jedis = pool.getResource(); Jedis jedis = pool.getResource();
try { try {
String key = buildRedisUnreadCountKey(user); String key = buildRedisUserSubscriptionsKey(user);
Pipeline pipe = jedis.pipelined(); Pipeline pipe = jedis.pipelined();
pipe.del(key); pipe.del(key);
pipe.set(key, mapper.writeValueAsString(map)); 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) {
@@ -142,15 +144,71 @@ public class RedisCacheService extends CacheService {
} }
@Override @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(); Jedis jedis = pool.getResource();
try { try {
Pipeline pipe = jedis.pipelined(); Pipeline pipe = jedis.pipelined();
if (users != null) { if (subs != null) {
for (User user : users) { for (FeedSubscription sub : subs) {
String key = buildRedisRootCategoryKey(user); String key = buildRedisUnreadCountKey(sub);
pipe.del(key);
key = buildRedisUnreadCountKey(user);
pipe.del(key); 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) { 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);
} }
} }

View File

@@ -266,28 +266,4 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
setTimeout(query, applicationSettingsService.get().getQueryTimeout()); 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);
}
} }

View File

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

View File

@@ -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;
}
}

View File

@@ -1,8 +1,12 @@
package com.commafeed.backend.services; package com.commafeed.backend.services;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import javax.inject.Inject; import javax.inject.Inject;
import com.commafeed.backend.cache.CacheService;
import com.commafeed.backend.dao.FeedEntryDAO; import com.commafeed.backend.dao.FeedEntryDAO;
import com.commafeed.backend.dao.FeedEntryStatusDAO; import com.commafeed.backend.dao.FeedEntryStatusDAO;
import com.commafeed.backend.dao.FeedSubscriptionDAO; 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.FeedEntryStatus;
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.google.common.collect.Lists;
@Stateless @Stateless
public class FeedEntryService { public class FeedEntryService {
@@ -23,6 +28,9 @@ public class FeedEntryService {
@Inject @Inject
FeedEntryDAO feedEntryDAO; FeedEntryDAO feedEntryDAO;
@Inject
CacheService cache;
public void markEntry(User user, Long entryId, Long subscriptionId, boolean read) { public void markEntry(User user, Long entryId, Long subscriptionId, boolean read) {
FeedSubscription sub = feedSubscriptionDAO.findById(user, subscriptionId); FeedSubscription sub = feedSubscriptionDAO.findById(user, subscriptionId);
if (sub == null) { if (sub == null) {
@@ -38,6 +46,7 @@ public class FeedEntryService {
if (status.isMarkable()) { if (status.isMarkable()) {
status.setRead(read); status.setRead(read);
feedEntryStatusDAO.saveOrUpdate(status); feedEntryStatusDAO.saveOrUpdate(status);
cache.invalidateUnreadCount(sub);
} }
} }
@@ -57,4 +66,29 @@ public class FeedEntryService {
status.setStarred(starred); status.setStarred(starred);
feedEntryStatusDAO.saveOrUpdate(status); 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);
}
} }

View File

@@ -80,20 +80,48 @@ public class FeedSubscriptionService {
feedSubscriptionDAO.saveOrUpdate(sub); feedSubscriptionDAO.saveOrUpdate(sub);
taskGiver.add(feed); taskGiver.add(feed);
cache.invalidateUserSubscriptions(user);
return feed; 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) { public Map<Long, Long> getUnreadCount(User user) {
Map<Long, Long> map = cache.getUnreadCounts(user); Map<Long, Long> map = Maps.newHashMap();
if (map == null) { List<FeedSubscription> subs = getSubscriptions(user);
log.debug("unread count cache miss for {}", Models.getId(user)); for (FeedSubscription sub : subs) {
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user); map.put(sub.getId(), getUnreadCount(sub));
map = Maps.newHashMap();
for (FeedSubscription sub : subs) {
map.put(sub.getId(), feedEntryStatusDAO.getUnreadCount(sub));
}
cache.setUnreadCounts(user, map);
} }
return map; return map;
} }
} }

View File

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

View File

@@ -33,6 +33,8 @@ 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.FeedSubscriptionService; import com.commafeed.backend.services.FeedSubscriptionService;
import com.commafeed.frontend.SecurityCheck; import com.commafeed.frontend.SecurityCheck;
import com.commafeed.frontend.model.Category; import com.commafeed.frontend.model.Category;
@@ -68,6 +70,9 @@ public class CategoryREST extends AbstractResourceREST {
@Inject @Inject
FeedEntryStatusDAO feedEntryStatusDAO; FeedEntryStatusDAO feedEntryStatusDAO;
@Inject
FeedEntryService feedEntryService;
@Inject @Inject
FeedCategoryDAO feedCategoryDAO; FeedCategoryDAO feedCategoryDAO;
@@ -77,6 +82,9 @@ public class CategoryREST extends AbstractResourceREST {
@Inject @Inject
FeedSubscriptionService feedSubscriptionService; FeedSubscriptionService feedSubscriptionService;
@Inject
FeedCategoryService feedCategoryService;
@Inject @Inject
CacheService cache; CacheService cache;
@@ -206,17 +214,16 @@ 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 = feedSubscriptionDAO.findAll(getUser()); List<FeedSubscription> subscriptions = feedSubscriptionService.getSubscriptions(getUser());
feedEntryStatusDAO.markSubscriptionEntries(subscriptions, olderThan); feedEntryService.markSubscriptionEntries(subscriptions, olderThan);
} else if (STARRED.equals(req.getId())) { } else if (STARRED.equals(req.getId())) {
feedEntryStatusDAO.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);
feedEntryStatusDAO.markSubscriptionEntries(subs, olderThan); feedEntryService.markSubscriptionEntries(subs, olderThan);
} }
cache.invalidateUserData(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@@ -238,7 +245,7 @@ public class CategoryREST extends AbstractResourceREST {
cat.setParent(parent); cat.setParent(parent);
} }
feedCategoryDAO.saveOrUpdate(cat); feedCategoryDAO.saveOrUpdate(cat);
cache.invalidateUserData(getUser()); cache.invalidateUserCategories(getUser());
return Response.ok().build(); return Response.ok().build();
} }
@@ -266,7 +273,7 @@ public class CategoryREST extends AbstractResourceREST {
feedCategoryDAO.saveOrUpdate(categories); feedCategoryDAO.saveOrUpdate(categories);
feedCategoryDAO.delete(cat); feedCategoryDAO.delete(cat);
cache.invalidateUserData(getUser()); cache.invalidateUserCategories(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();
@@ -322,7 +329,7 @@ public class CategoryREST extends AbstractResourceREST {
} }
feedCategoryDAO.saveOrUpdate(category); feedCategoryDAO.saveOrUpdate(category);
cache.invalidateUserData(getUser()); cache.invalidateUserCategories(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@@ -339,7 +346,7 @@ public class CategoryREST extends AbstractResourceREST {
} }
category.setCollapsed(req.isCollapse()); category.setCollapsed(req.isCollapse());
feedCategoryDAO.saveOrUpdate(category); feedCategoryDAO.saveOrUpdate(category);
cache.invalidateUserData(getUser()); cache.invalidateUserCategories(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@@ -364,18 +371,15 @@ public class CategoryREST extends AbstractResourceREST {
public Response getSubscriptions() { public Response getSubscriptions() {
User user = getUser(); User user = getUser();
Category root = cache.getRootCategory(user); Category root = null;
if (root == null) { List<FeedCategory> categories = feedCategoryService.getCategories(user);
log.debug("root category cache miss for {}", user.getName()); List<FeedSubscription> subscriptions = feedSubscriptionService.getSubscriptions(getUser());
List<FeedCategory> categories = feedCategoryDAO.findAll(user); Map<Long, Long> unreadCount = feedSubscriptionService.getUnreadCount(getUser());
List<FeedSubscription> subscriptions = feedSubscriptionDAO.findAll(getUser());
Map<Long, Long> unreadCount = feedSubscriptionService.getUnreadCount(getUser()); 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.setRootCategory(user, root);
}
return Response.ok(root).build(); return Response.ok(root).build();
} }

View File

@@ -13,7 +13,6 @@ import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import com.commafeed.backend.cache.CacheService;
import com.commafeed.backend.dao.FeedEntryStatusDAO; import com.commafeed.backend.dao.FeedEntryStatusDAO;
import com.commafeed.backend.dao.FeedSubscriptionDAO; import com.commafeed.backend.dao.FeedSubscriptionDAO;
import com.commafeed.backend.model.FeedEntryStatus; import com.commafeed.backend.model.FeedEntryStatus;
@@ -44,9 +43,6 @@ public class EntryREST extends AbstractResourceREST {
@Inject @Inject
FeedSubscriptionDAO feedSubscriptionDAO; FeedSubscriptionDAO feedSubscriptionDAO;
@Inject
CacheService cache;
@Path("/mark") @Path("/mark")
@POST @POST
@ApiOperation(value = "Mark a feed entry", notes = "Mark a feed entry as read/unread") @ApiOperation(value = "Mark a feed entry", notes = "Mark a feed entry as read/unread")
@@ -56,7 +52,6 @@ public class EntryREST extends AbstractResourceREST {
Preconditions.checkNotNull(req.getFeedId()); Preconditions.checkNotNull(req.getFeedId());
feedEntryService.markEntry(getUser(), Long.valueOf(req.getId()), req.getFeedId(), req.isRead()); feedEntryService.markEntry(getUser(), Long.valueOf(req.getId()), req.getFeedId(), req.isRead());
cache.invalidateUserData(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }

View File

@@ -55,6 +55,7 @@ import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription; import com.commafeed.backend.model.FeedSubscription;
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.FeedEntryService;
import com.commafeed.backend.services.FeedSubscriptionService; import com.commafeed.backend.services.FeedSubscriptionService;
import com.commafeed.frontend.SecurityCheck; import com.commafeed.frontend.SecurityCheck;
import com.commafeed.frontend.model.Entries; import com.commafeed.frontend.model.Entries;
@@ -97,6 +98,9 @@ public class FeedREST extends AbstractResourceREST {
@Inject @Inject
FeedSubscriptionDAO feedSubscriptionDAO; FeedSubscriptionDAO feedSubscriptionDAO;
@Inject
FeedEntryService feedEntryService;
@Inject @Inject
FeedSubscriptionService feedSubscriptionService; FeedSubscriptionService feedSubscriptionService;
@@ -274,9 +278,8 @@ 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) {
feedEntryStatusDAO.markSubscriptionEntries(Arrays.asList(subscription), olderThan); feedEntryService.markSubscriptionEntries(Arrays.asList(subscription), olderThan);
} }
cache.invalidateUserData(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@@ -353,7 +356,6 @@ public class FeedREST extends AbstractResourceREST {
log.info("Failed to subscribe to URL {}: {}", url, e.getMessage()); log.info("Failed to subscribe to URL {}: {}", url, e.getMessage());
return Response.status(Status.SERVICE_UNAVAILABLE).entity("Failed to subscribe to URL " + url + ": " + e.getMessage()).build(); return Response.status(Status.SERVICE_UNAVAILABLE).entity("Failed to subscribe to URL " + url + ": " + e.getMessage()).build();
} }
cache.invalidateUserData(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@@ -390,10 +392,8 @@ public class FeedREST extends AbstractResourceREST {
Preconditions.checkNotNull(req); Preconditions.checkNotNull(req);
Preconditions.checkNotNull(req.getId()); Preconditions.checkNotNull(req.getId());
FeedSubscription sub = feedSubscriptionDAO.findById(getUser(), req.getId()); boolean deleted = feedSubscriptionService.unsubscribe(getUser(), req.getId());
if (sub != null) { if (deleted) {
feedSubscriptionDAO.delete(sub);
cache.invalidateUserData(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} else { } else {
return Response.status(Status.NOT_FOUND).build(); return Response.status(Status.NOT_FOUND).build();
@@ -446,7 +446,7 @@ public class FeedREST extends AbstractResourceREST {
} else { } else {
feedSubscriptionDAO.saveOrUpdate(subscription); feedSubscriptionDAO.saveOrUpdate(subscription);
} }
cache.invalidateUserData(getUser()); cache.invalidateUserSubscriptions(getUser());
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@@ -480,7 +480,6 @@ public class FeedREST extends AbstractResourceREST {
} catch (Exception e) { } catch (Exception e) {
throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build()); throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build());
} }
cache.invalidateUserData(getUser());
return Response.temporaryRedirect(URI.create(applicationSettingsService.get().getPublicUrl())).build(); return Response.temporaryRedirect(URI.create(applicationSettingsService.get().getPublicUrl())).build();
} }

View File

@@ -12,7 +12,6 @@ import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import com.commafeed.backend.StartupBean; import com.commafeed.backend.StartupBean;
import com.commafeed.backend.cache.CacheService;
import com.commafeed.backend.dao.UserDAO; import com.commafeed.backend.dao.UserDAO;
import com.commafeed.backend.dao.UserRoleDAO; import com.commafeed.backend.dao.UserRoleDAO;
import com.commafeed.backend.dao.UserSettingsDAO; import com.commafeed.backend.dao.UserSettingsDAO;
@@ -58,9 +57,6 @@ public class UserREST extends AbstractResourceREST {
@Inject @Inject
PasswordEncryptionService encryptionService; PasswordEncryptionService encryptionService;
@Inject
CacheService cache;
@Inject @Inject
ApplicationSettingsService applicationSettingsService; ApplicationSettingsService applicationSettingsService;
@@ -200,7 +196,6 @@ public class UserREST extends AbstractResourceREST {
return Response.status(Status.FORBIDDEN).build(); return Response.status(Status.FORBIDDEN).build();
} }
userService.unregister(getUser()); userService.unregister(getUser());
cache.invalidateUserData(getUser());
return Response.ok().build(); return Response.ok().build();
} }
} }