@Override @Transactional(propagation = Propagation.REQUIRED) public long unlike(Long feedId, Long uid) { if (feedId == null || uid == null) { return -1; } Like like = likeMapper.getLike(feedId, uid); if (like == null) { return -1; } else { int deleteResult = likeMapper.delete(like.getId()); if (deleteResult > 0) { ZSetOperations<String, String> feedLikes = this.template.opsForZSet(); ValueOperations<String, String> feedCount = this.template.opsForValue(); feedLikes.remove(KEY_FEED_LIKE + feedId, Long.toString(uid)); return feedCount.increment(KEY_FEED_LIKE_COUNT + feedId, -1); } return -1; } }
@Override public List<Long> likeUserIdsWithFriendsFirst(int start, int pageCount, Long feedId, Long uid) { if (feedId == null) { return Collections.emptyList(); } ZSetOperations<String, String> feedLikes = this.template.opsForZSet(); // 求交集,得到好友关系 feedLikes.intersectAndStore(KEY_FEED_LIKE + feedId, UserServiceImpl.KEY_USER_FRIENDS + uid, KEY_FEED_LIKE_FRIENDS + feedId); Set<String> friends = feedLikes.range(KEY_FEED_LIKE_FRIENDS + feedId, start, start + pageCount); if (friends.size() > pageCount) { return transfer(friends); } else { // TODO // 剩下的,全部遍历,然后检查是否是好友,如果是,直接排除。如果不是,增加到列表中; // 由于涉及到分页,需要记录上次操作的index } return Collections.emptyList(); }
@Override public List<Long> likeUserIds(int start, int pageCount, Long feedId) { if (feedId == null) { return Collections.emptyList(); } ZSetOperations<String, String> feedLikes = this.template.opsForZSet(); Set<String> uids = feedLikes.range(KEY_FEED_LIKE + feedId, start, start + pageCount - 1); return transfer(uids); }
public void sortByScore(List<Post> posts, Set<ZSetOperations.TypedTuple<Long>> scores) { Map<Long, Double> map = new HashMap<>(); scores.forEach(s -> map.put(s.getValue(), s.getScore())); posts.sort((o1, o2) -> { double diff = map.get(o2.getId()) - map.get(o1.getId()); return diff > 0 ? 1 : (diff < 0 ? -1 : 0); }); }
protected Map<String, Double> getZSetData(String counterKey) { Set<ZSetOperations.TypedTuple<String>> rangeWithScore = this.redisTemplate .boundZSetOps(counterKey).rangeWithScores(0, -1); Map<String, Double> values = new HashMap<String, Double>( rangeWithScore.size()); for (Iterator<ZSetOperations.TypedTuple<String>> iterator = rangeWithScore.iterator(); iterator .hasNext();) { ZSetOperations.TypedTuple<String> typedTuple = iterator.next(); values.put(typedTuple.getValue(), typedTuple.getScore()); } return values; }
@Override public ZSetOperations<K, V> opsForZSet() { try { return redisTemplate.opsForZSet(); } catch (Exception ex) { throw new RedisBaoException(ex); } }
public List<Post> getHotPostsOfPage(int page, int pageSize) throws PageNotFoundException { Set<ZSetOperations.TypedTuple<Long>> idsWithScore = hotPostService.getPageWithScore(page, pageSize); return getHotPostsOfPage(page, idsWithScore); }
public List<Post> getHotPostsOfPage(int page, int pageSize, Tag tag) throws PageNotFoundException { Set<ZSetOperations.TypedTuple<Long>> idsWithScore = hotPostService.getPageWithScoreOfTag(tag.getId(), page, pageSize); return getHotPostsOfPage(page, idsWithScore); }
public List<Post> getHotPostsOfPage(int page, int pageSize, Topic topic) throws PageNotFoundException { Set<ZSetOperations.TypedTuple<Long>> idsWithScore = hotPostService.getPageWithScoreOfTopic(topic.getId(), page, pageSize); return getHotPostsOfPage(page, idsWithScore); }
public Set<ZSetOperations.TypedTuple<Long>> getPageWithScore(int page, int pageSize) { return hotPosts.reverseRangeWithScores(CACHE_HOT_POSTS, page * pageSize, (page + 1) * pageSize - 1); }
public Set<ZSetOperations.TypedTuple<Long>> getPageWithScoreOfTag(Long tagId, int page, int pageSize) { return hotPosts.reverseRangeWithScores(CACHE_HOT_TAG_POSTS + tagId, page * pageSize, (page + 1) * pageSize - 1); }
public Set<ZSetOperations.TypedTuple<Long>> getPageWithScoreOfTopic(Long topicId, int page, int pageSize) { return hotPosts.reverseRangeWithScores(CACHE_HOT_TOPIC_POSTS + topicId, page * pageSize, (page + 1) * pageSize - 1); }
@Override public ZSetOperations<K, V> opsForZSet() { int dbIndex = RedisApplication.redisConnectionDbIndex.get(); return new DefaultZSetOperations<K, V>(this, dbIndex); }
public ZSetOperations<String, String> zSetOps() { return redisTemplate.opsForZSet(); }
@Before public void setUp() throws Exception { redisTemplate = mock(RedisTemplate.class); zSetOperations = mock(ZSetOperations.class); super.setUp(); }
public ZSetOperations<String, Object> opsForZSet(){ return redisTemplate.opsForZSet(); }
ZSetOperations<K, V> opsForZSet();