Skip to content

Commit

Permalink
🎨 follow count增加缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
adlered committed Dec 21, 2024
1 parent fa7f025 commit eb57e01
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/main/java/org/b3log/symphony/cache/FollowingCountCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.b3log.symphony.cache;

import org.b3log.latke.ioc.Singleton;
import org.json.JSONObject;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

@Singleton
public class FollowingCountCache {

public static final Map<String, JSONObject> COUNT_CACHE = Collections.synchronizedMap(new LinkedHashMap<>() {
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > 2000;
}
});
}
35 changes: 33 additions & 2 deletions src/main/java/org/b3log/symphony/service/FollowQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.b3log.latke.repository.*;
import org.b3log.latke.service.annotation.Service;
import org.b3log.latke.util.Stopwatchs;
import org.b3log.symphony.cache.FollowingCountCache;
import org.b3log.symphony.model.Follow;
import org.b3log.symphony.repository.ArticleRepository;
import org.b3log.symphony.repository.FollowRepository;
Expand Down Expand Up @@ -395,9 +396,23 @@ public long getFollowingCount(final String followerId, final int followingType)
final List<Filter> filters = new ArrayList<>();
filters.add(new PropertyFilter(Follow.FOLLOWER_ID, FilterOperator.EQUAL, followerId));
filters.add(new PropertyFilter(Follow.FOLLOWING_TYPE, FilterOperator.EQUAL, followingType));
JSONObject cacheJSON = new JSONObject();
cacheJSON.put(Follow.FOLLOWER_ID, followerId);
cacheJSON.put(Follow.FOLLOWING_TYPE, followingType);
JSONObject result = FollowingCountCache.COUNT_CACHE.get(cacheJSON.toString());
if (result != null) {
if (result.optLong("time") > System.currentTimeMillis() - FIVE_MINUTES_IN_MILLIS) {
return result.optLong("count");
}
}
final Query query = new Query().setFilter(new CompositeFilter(CompositeFilterOperator.AND, filters));
try {
return followRepository.count(query);
long count = followRepository.count(query);
JSONObject json = new JSONObject();
json.put("count", count);
json.put("time", System.currentTimeMillis());
FollowingCountCache.COUNT_CACHE.put(cacheJSON.toString(), json);
return count;
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Counts following count failed", e);
return 0;
Expand All @@ -407,6 +422,8 @@ public long getFollowingCount(final String followerId, final int followingType)
}
}

private static final long FIVE_MINUTES_IN_MILLIS = 1000 * 60 * 5;

/**
* Gets the follower count of a following specified by the given following id and following type.
*
Expand All @@ -418,9 +435,23 @@ public long getFollowerCount(final String followingId, final int followingType)
final List<Filter> filters = new ArrayList<>();
filters.add(new PropertyFilter(Follow.FOLLOWING_ID, FilterOperator.EQUAL, followingId));
filters.add(new PropertyFilter(Follow.FOLLOWING_TYPE, FilterOperator.EQUAL, followingType));
JSONObject cacheJSON = new JSONObject();
cacheJSON.put(Follow.FOLLOWING_ID, followingId);
cacheJSON.put(Follow.FOLLOWING_TYPE, followingType);
JSONObject result = FollowingCountCache.COUNT_CACHE.get(cacheJSON.toString());
if (result != null) {
if (result.optLong("time") > System.currentTimeMillis() - FIVE_MINUTES_IN_MILLIS) {
return result.optLong("count");
}
}
final Query query = new Query().setFilter(new CompositeFilter(CompositeFilterOperator.AND, filters));
try {
return followRepository.count(query);
long count = followRepository.count(query);
JSONObject json = new JSONObject();
json.put("count", count);
json.put("time", System.currentTimeMillis());
FollowingCountCache.COUNT_CACHE.put(cacheJSON.toString(), json);
return count;
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Counts follower count failed", e);
return 0;
Expand Down

0 comments on commit eb57e01

Please sign in to comment.