會員註冊 / 登入  |  電腦版  |  Jump to bottom of page

Developer Forum » Topics by forum

發表人: rostek
10 年 前
Hi, I downloaded JForum and started changing it to my needs. First was making urls more friendly, now they contain info about post/topic title but it's not important. When I tested friendly urls with pagination, I saw results on subsequent pages do not appear. I looked at code and my question is, what sense is in this method:

public static List<Topic> topicsByForum(int forumId, int start)

{
TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
List<Topic> topics;

// Try to get the first's page of topics from the cache
if (SystemGlobals.getBoolValue(ConfigKeys.TOPIC_CACHE_ENABLED)) {
topics = TopicRepository.getTopics(forumId);

if (topics.isEmpty() || !TopicRepository.isLoaded(forumId)) {
synchronized (MUTEXT) {
if (topics.isEmpty() || !TopicRepository.isLoaded(forumId)) {
topics = tm.selectAllByForum(forumId);
TopicRepository.addAll(forumId, topics);
}
}
}
}
else {
topics = tm.selectAllByForumByLimit(forumId, start, topicsPerPage);
}

int size = topics.size();

while (size < start) {
start -= topicsPerPage;
}
if (start < 0) {
start = 0;
}

return topics.subList(start, (size < start + topicsPerPage) ? size : start + topicsPerPage);
}


in TopicsCommon class. What if in cache I have 20 results and go to records on 30th page? What is the subListing for, if I would not use cache and get exact list from database?

Thanks.

發表人: andowson
10 年 前
TopicsCommon.topicsByForum(int forumId, int start) is used to fetch topics in forum forumId beginning from the index start.
For example:
TopicsCommon.topicsByForum(1, 0) returns topics start from index 0 in forum where forum_id=1.
How many topics returned is determined by topicsPerPage.

And if you enable cache of topics, JForum will look for them in the topic repository(cache) first. If the topic repository for the forum forumId is empty, JForum will load topics from database and cache it.
JForum try to load all the topics from database first, and then use subList to return only topicsPerPage line of records from the start index.

If topicsPerPage = 15, and you only have 20 topics in cache for the forum, the JForum will display the last 5 topics in the 2nd page.

If you disable cache of topics, JForum will fetch topics from the database directly. But only with limit number of records (topicsPerPage) returned.


發表人: rostek
10 年 前
Thank you for the answer. Of course I understand the idea, but code does not do that.

// If the cache is full, remove the eldest element

int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
if (!contains && forumTopicsList.size() + 1 > topicsPerPage) {
forumTopicsList.removeLast();


in TopicRepository:290, so cache can actually contain only ConfigKeys.TOPICS_PER_PAGE newest topics.
When you get topics = TopicRepository.getTopics(forumId); in TopicsCommon:101 you get newest topics from cache. How can you get topic for, for example start=1000?


發表人: andowson
10 年 前
You are right. This is a bug.
The quick way to fix it is to remove this code fragment for checking with topicsPerPage limit.

I've committed a fix for it as revision 208.
The idea is to check if (start + topicsPerPage >= topicCacheSize) then reload the topics from database again.
topicCacheSize is a new configurable value in SystemGlobals.properties and the default value is 45:
topic.cache.size = 45





會員註冊 / 登入  |  電腦版  |  Jump to top of page