LOGGER.log(Level.INFO, "Not found statistic in memcache, ignores sync");
return;
}
final Transaction transaction = statisticRepository.beginTransaction();
transaction.clearQueryCache(false);
try {
// For blog view counter
statisticRepository.update(Statistic.STATISTIC, statistic);
// For article view counter
final Set<String> cachedPageKeys = PageCaches.getKeys();
for (final String cachedPageKey : cachedPageKeys) {
final JSONObject cachedPage = PageCaches.get(cachedPageKey);
if (null == cachedPage) {
continue;
}
final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
if (!cachedPage.optString(AbstractCacheablePageAction.CACHED_TYPE).
equals(langs.get(PageTypes.ARTICLE))) { // Cached is not an article page
continue;
}
final int hitCount = cachedPage.optInt(PageCaches.CACHED_HIT_COUNT);
if (2 > hitCount) {
// Skips for view count tiny-changes, reduces Datastore Write Quota for Solo GAE version
continue;
}
final String articleId = cachedPage.optString(AbstractCacheablePageAction.CACHED_OID);
LOGGER.log(Level.FINER, "Updating article[id={0}, title={1}] view count",
new Object[]{articleId, cachedPage.optString(AbstractCacheablePageAction.CACHED_TITLE)});
final JSONObject article = articleRepository.get(articleId);
if (null == article) {
continue;
}
final int oldViewCount = article.optInt(Article.ARTICLE_VIEW_COUNT);
final int viewCount = oldViewCount + hitCount;
article.put(Article.ARTICLE_VIEW_COUNT, viewCount);
articleRepository.update(articleId, article);
cachedPage.put(PageCaches.CACHED_HIT_COUNT, 0);
LOGGER.log(Level.FINER, "Updating article[id={0}, title={1}] view count from [{2}] to [{3}]",
new Object[]{articleId, cachedPage.optString(
AbstractCacheablePageAction.CACHED_TITLE), oldViewCount, viewCount});
}
transaction.commit();
LOGGER.log(Level.INFO, "Synchronized statistic from cache to repository[statistic={0}]", statistic);
} catch (final RepositoryException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.SEVERE, "Updates statistic failed", e);
}
}