send date of the newest subscriptions item

This commit is contained in:
Athou
2013-08-08 17:00:35 +02:00
parent 74ee810757
commit 474995c8dd
9 changed files with 65 additions and 40 deletions

View File

@@ -9,6 +9,7 @@ import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User;
import com.commafeed.frontend.model.Category;
import com.commafeed.frontend.model.UnreadCount;
public abstract class CacheService {
@@ -29,9 +30,9 @@ public abstract class CacheService {
public abstract void invalidateUserRootCategory(User... users);
// unread count
public abstract Long getUnreadCount(FeedSubscription sub);
public abstract UnreadCount getUnreadCount(FeedSubscription sub);
public abstract void setUnreadCount(FeedSubscription sub, Long count);
public abstract void setUnreadCount(FeedSubscription sub, UnreadCount count);
public abstract void invalidateUnreadCount(FeedSubscription... subs);

View File

@@ -10,6 +10,7 @@ import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.User;
import com.commafeed.frontend.model.Category;
import com.commafeed.frontend.model.UnreadCount;
@Alternative
@ApplicationScoped
@@ -25,12 +26,12 @@ public class NoopCacheService extends CacheService {
}
@Override
public Long getUnreadCount(FeedSubscription sub) {
public UnreadCount getUnreadCount(FeedSubscription sub) {
return null;
}
@Override
public void setUnreadCount(FeedSubscription sub, Long count) {
public void setUnreadCount(FeedSubscription sub, UnreadCount count) {
}

View File

@@ -20,6 +20,7 @@ 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.commafeed.frontend.model.UnreadCount;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
@@ -104,14 +105,14 @@ public class RedisCacheService extends CacheService {
}
@Override
public Long getUnreadCount(FeedSubscription sub) {
Long count = null;
public UnreadCount getUnreadCount(FeedSubscription sub) {
UnreadCount count = null;
Jedis jedis = pool.getResource();
try {
String key = buildRedisUnreadCountKey(sub);
String countString = jedis.get(key);
if (countString != null) {
count = Long.valueOf(countString);
String json = jedis.get(key);
if (json != null) {
count = mapper.readValue(json, UnreadCount.class);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
@@ -122,14 +123,14 @@ public class RedisCacheService extends CacheService {
}
@Override
public void setUnreadCount(FeedSubscription sub, Long count) {
public void setUnreadCount(FeedSubscription sub, UnreadCount count) {
Jedis jedis = pool.getResource();
try {
String key = buildRedisUnreadCountKey(sub);
Pipeline pipe = jedis.pipelined();
pipe.del(key);
pipe.set(key, String.valueOf(count));
pipe.set(key, mapper.writeValueAsString(count));
pipe.expire(key, (int) TimeUnit.MINUTES.toSeconds(30));
pipe.sync();
} catch (Exception e) {

View File

@@ -39,6 +39,7 @@ import com.commafeed.backend.model.Models;
import com.commafeed.backend.model.User;
import com.commafeed.backend.model.UserSettings.ReadingOrder;
import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.frontend.model.UnreadCount;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
@@ -240,18 +241,21 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
}
@SuppressWarnings("unchecked")
public Long getUnreadCount(FeedSubscription subscription) {
Long count = null;
public UnreadCount getUnreadCount(FeedSubscription subscription) {
UnreadCount uc = null;
Criteria criteria = buildSearchCriteria(subscription, true, null, null, -1, -1, null, false, null);
ProjectionList projection = Projections.projectionList();
projection.add(Projections.rowCount(), "count");
projection.add(Projections.max(FeedEntry_.updated.getName()), "updated");
criteria.setProjection(projection);
criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List<Map<String, Long>> list = criteria.list();
for (Map<String, Long> row : list) {
count = row.get("count");
List<Map<String, Object>> list = criteria.list();
for (Map<String, Object> row : list) {
Long count = (Long) row.get("count");
Date updated = (Date) row.get("updated");
uc = new UnreadCount(subscription.getId(), count, updated);
}
return count;
return uc;
}
private List<FeedEntryStatus> lazyLoadContent(boolean includeContent, List<FeedEntryStatus> results) {

View File

@@ -21,6 +21,7 @@ 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.UnreadCount;
import com.google.common.collect.Maps;
public class FeedSubscriptionService {
@@ -95,8 +96,8 @@ public class FeedSubscriptionService {
}
}
public Long getUnreadCount(FeedSubscription sub) {
Long count = cache.getUnreadCount(sub);
public UnreadCount getUnreadCount(FeedSubscription sub) {
UnreadCount count = cache.getUnreadCount(sub);
if (count == null) {
log.debug("unread count cache miss for {}", Models.getId(sub));
count = feedEntryStatusDAO.getUnreadCount(sub);
@@ -105,8 +106,8 @@ public class FeedSubscriptionService {
return count;
}
public Map<Long, Long> getUnreadCount(User user) {
Map<Long, Long> map = Maps.newHashMap();
public Map<Long, UnreadCount> getUnreadCount(User user) {
Map<Long, UnreadCount> map = Maps.newHashMap();
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
for (FeedSubscription sub : subs) {
map.put(sub.getId(), getUnreadCount(sub));