Skip to content

Commit

Permalink
b3log#602 近期热议缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Apr 3, 2018
1 parent 5bfc7c8 commit 75a4a1d
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 64 deletions.
75 changes: 74 additions & 1 deletion src/main/java/org/b3log/symphony/cache/ArticleCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,49 @@
*/
package org.b3log.symphony.cache;

import org.apache.commons.lang.time.DateUtils;
import org.b3log.latke.Keys;
import org.b3log.latke.cache.Cache;
import org.b3log.latke.cache.CacheFactory;
import org.b3log.latke.ioc.LatkeBeanManager;
import org.b3log.latke.ioc.LatkeBeanManagerImpl;
import org.b3log.latke.ioc.inject.Named;
import org.b3log.latke.ioc.inject.Singleton;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.repository.*;
import org.b3log.latke.util.CollectionUtils;
import org.b3log.symphony.model.Article;
import org.b3log.symphony.model.Common;
import org.b3log.symphony.model.Tag;
import org.b3log.symphony.model.UserExt;
import org.b3log.symphony.repository.ArticleRepository;
import org.b3log.symphony.service.ArticleQueryService;
import org.b3log.symphony.util.JSONs;
import org.b3log.symphony.util.Symphonys;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* Article cache.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.1.4, Jul 23, 2017
* @version 1.2.0.0, Apr 3, 2018
* @since 1.4.0
*/
@Named
@Singleton
public class ArticleCache {

/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(ArticleCache.class);

/**
* Article cache.
*/
Expand All @@ -50,11 +71,63 @@ public class ArticleCache {
private static final Cache ARTICLE_ABSTRACT_CACHE = CacheFactory.getCache(Article.ARTICLES + "_"
+ Article.ARTICLE_T_PREVIEW_CONTENT);

/**
* Side hot articles cache.
*/
private static final List<JSONObject> SIDE_HOT_ARTICLES = new ArrayList<>();

static {
ARTICLE_CACHE.setMaxCount(Symphonys.getInt("cache.articleCnt"));
ARTICLE_ABSTRACT_CACHE.setMaxCount(Symphonys.getInt("cache.articleCnt"));
}

/**
* Gets side hot articles.
*
* @return side hot articles
*/
public List<JSONObject> getSideHotArticles() {
if (SIDE_HOT_ARTICLES.isEmpty()) {
return Collections.emptyList();
}

return new ArrayList<>(SIDE_HOT_ARTICLES);
}

/**
* Loads side hot articles.
*/
public void loadSideHotArticles() {
final LatkeBeanManager beanManager = LatkeBeanManagerImpl.getInstance();
final ArticleRepository articleRepository = beanManager.getReference(ArticleRepository.class);
final ArticleQueryService articleQueryService = beanManager.getReference(ArticleQueryService.class);

try {
final String id = String.valueOf(DateUtils.addDays(new Date(), -7).getTime());
final Query query = new Query().addSort(Article.ARTICLE_COMMENT_CNT, SortDirection.DESCENDING).
addSort(Keys.OBJECT_ID, SortDirection.ASCENDING).setCurrentPageNum(1).setPageSize(Symphonys.getInt("sideHotArticlesCnt"));

final List<Filter> filters = new ArrayList<>();
filters.add(new PropertyFilter(Keys.OBJECT_ID, FilterOperator.GREATER_THAN_OR_EQUAL, id));
filters.add(new PropertyFilter(Article.ARTICLE_TYPE, FilterOperator.NOT_EQUAL, Article.ARTICLE_TYPE_C_DISCUSSION));
filters.add(new PropertyFilter(Article.ARTICLE_TAGS, FilterOperator.NOT_EQUAL, Tag.TAG_TITLE_C_SANDBOX));

query.setFilter(new CompositeFilter(CompositeFilterOperator.AND, filters)).
addProjection(Article.ARTICLE_TITLE, String.class).
addProjection(Article.ARTICLE_PERMALINK, String.class).
addProjection(Article.ARTICLE_AUTHOR_ID, String.class);

final JSONObject result = articleRepository.get(query);
final List<JSONObject> articles = CollectionUtils.jsonArrayToList(result.optJSONArray(Keys.RESULTS));
articleQueryService.organizeArticles(UserExt.USER_AVATAR_VIEW_MODE_C_STATIC, articles);

SIDE_HOT_ARTICLES.clear();
SIDE_HOT_ARTICLES.addAll(articles);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Loads hot articles failed", e);
}
}

/**
* Gets an article abstract by the specified article id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void showCharacter(final HTTPRequestContext context,
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);

dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);

Expand Down Expand Up @@ -233,7 +233,7 @@ public void showActivities(final HTTPRequestContext context,
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);

dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);

Expand Down Expand Up @@ -275,7 +275,7 @@ public void showDailyCheckin(final HTTPRequestContext context,
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);

dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
Expand Down Expand Up @@ -435,7 +435,7 @@ public void show1A0001(final HTTPRequestContext context,
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);

dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
Expand Down Expand Up @@ -519,7 +519,7 @@ public void showEatingSnake(final HTTPRequestContext context,
dataModelService.fillHeaderAndFooter(request, response, dataModel);
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);

Expand Down Expand Up @@ -616,7 +616,7 @@ public void showGobang(final HTTPRequestContext context,
dataModelService.fillHeaderAndFooter(request, response, dataModel);
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);

String pointActivityGobang = langPropsService.get("activityStartGobangTipLabel");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ public void showArticle(final HTTPRequestContext context, final HttpServletReque

dataModelService.fillRelevantArticles(avatarViewMode, dataModel, article);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);

// Qiniu file upload authenticate
final Auth auth = Auth.create(Symphonys.get("qiniu.accessKey"), Symphonys.get("qiniu.secretKey"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.b3log.latke.servlet.HTTPRequestMethod;
import org.b3log.latke.servlet.annotation.RequestProcessing;
import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.symphony.cache.ArticleCache;
import org.b3log.symphony.cache.DomainCache;
import org.b3log.symphony.cache.TagCache;
import org.b3log.symphony.util.Symphonys;
Expand Down Expand Up @@ -60,6 +61,12 @@ public class CacheProcessor {
@Inject
private DomainCache domainCache;

/**
* Article cache.
*/
@Inject
private ArticleCache articleCache;

/**
* Refreshes cache.
* <ul>
Expand All @@ -84,6 +91,7 @@ public void refreshCache(final HTTPRequestContext context,

tagCache.loadTags();
domainCache.loadDomains();
articleCache.loadSideHotArticles();

context.renderJSON().renderTrueResult();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void showChargePoint(final HTTPRequestContext context, final HttpServletR
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);

dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void showChatRoom(final HTTPRequestContext context,
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);

dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/b3log/symphony/processor/CityProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void showCityArticles(final HTTPRequestContext context,
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);

dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);

Expand Down Expand Up @@ -225,7 +225,7 @@ public void showCityUsers(final HTTPRequestContext context,

final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void showDomainArticles(final HTTPRequestContext context, final HttpServl

dataModelService.fillHeaderAndFooter(request, response, dataModel);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void handleErrorPage(final HTTPRequestContext context, final HttpServletR
dataModel.putAll(langPropsService.getAll(Locales.getLocale()));
dataModelService.fillHeaderAndFooter(request, response, dataModel);
if (HttpServletResponse.SC_FORBIDDEN == Integer.valueOf(statusCode)) {
dataModelService.fillSideHotArticles(UserExt.USER_AVATAR_VIEW_MODE_C_ORIGINAL, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillRandomArticles(UserExt.USER_AVATAR_VIEW_MODE_C_ORIGINAL, dataModel);
dataModelService.fillSideTags(dataModel);
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/b3log/symphony/processor/IndexProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public void showRecent(final HTTPRequestContext context, final HttpServletReques

dataModelService.fillHeaderAndFooter(request, response, dataModel);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);

Expand Down Expand Up @@ -333,7 +333,7 @@ public void showHotArticles(final HTTPRequestContext context,
if (!(Boolean) dataModel.get(Common.IS_MOBILE)) {
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
}
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
} finally {
Expand Down Expand Up @@ -369,7 +369,7 @@ public void showSymHub(final HTTPRequestContext context,
if (!(Boolean) dataModel.get(Common.IS_MOBILE)) {
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
}
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
} finally {
Expand Down Expand Up @@ -431,7 +431,7 @@ public void showPerfectArticles(final HTTPRequestContext context,

dataModelService.fillHeaderAndFooter(request, response, dataModel);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
Expand Down Expand Up @@ -472,7 +472,7 @@ public void showB3log(final HTTPRequestContext context,
dataModelService.fillHeaderAndFooter(request, response, dataModel);
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void showMan(final HTTPRequestContext context,
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);

dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void search(final HTTPRequestContext context, final HttpServletRequest re

dataModelService.fillHeaderAndFooter(request, response, dataModel);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void showStatistic(final HTTPRequestContext context,
final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);

dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void showTagArticles(final HTTPRequestContext context, final HttpServletR
dataModel.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);

dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/b3log/symphony/processor/TopProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void showBalance(final HTTPRequestContext context, final HttpServletReque

dataModelService.fillHeaderAndFooter(request, response, dataModel);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public void showConsumption(final HTTPRequestContext context, final HttpServletR

dataModelService.fillHeaderAndFooter(request, response, dataModel);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
Expand Down Expand Up @@ -171,7 +171,7 @@ public void showCheckin(final HTTPRequestContext context,

dataModelService.fillHeaderAndFooter(request, response, dataModel);
dataModelService.fillRandomArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
dataModelService.fillSideHotArticles(dataModel);
dataModelService.fillSideTags(dataModel);
dataModelService.fillLatestCmts(dataModel);
}
Expand Down
Loading

0 comments on commit 75a4a1d

Please sign in to comment.