1.update user collect journal count

main
Gary 10 months ago
parent aea4918a18
commit 0dd1dfb6f8

@ -31,6 +31,7 @@ public class Constants {
public static final String J2CACHE_REGION_ADVERTISEMENT_PAGE = "advertisement_page";
public static final String RABBIT_MESSAGE_CLEAN_JOURANL_QUERY_PAGE = "clean_journal_query_page";
public static final String RABBIT_MESSAGE_USER_COLLECT_JOURNAL = "user_collect_journal";
public static final String TOKEN_PREFIX = "Bearer ";
public static final int TOKEN_PREFIX_LENGTH = TOKEN_PREFIX.length();

@ -0,0 +1,80 @@
package com.luoo.music.listener;
import constants.Constants;
import net.oschina.j2cache.CacheChannel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.luoo.music.dao.JournalDao;
import com.luoo.music.dto.request.JournalQueryReq;
import com.luoo.music.pojo.Journal;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Component
@RabbitListener(queues = Constants.RABBIT_MESSAGE_USER_COLLECT_JOURNAL)
public class UserCollectJournalListener {
private static final int DEFAULT_PAGE_SIZE = 10;
@Autowired
private CacheChannel cacheChannel;
@Autowired
private JournalDao journalDao;
@RabbitHandler
public void updateUserCollectCount(String journalId) {
boolean isReduceCount = journalId.startsWith("-");
if (isReduceCount) {
journalId = journalId.replaceFirst("-", "");
}
Optional<Journal> optional = journalDao.findById(journalId);
if (!optional.isPresent()) {
return;
}
Journal journal = optional.get();
int userCollectCount = journal.getUserCollectCount();
journal.setUserCollectCount(isReduceCount ? (userCollectCount - 1) : (userCollectCount + 1));
journalDao.save(journal);
excuteClearCache(journal);
}
private void excuteClearCache(Journal journal) {
String journalQueryPageKey = getJournalQueryPageKey(journal);
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, journalQueryPageKey);
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_ID, journal.getId());
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journal.getId());
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_NO, journal.getJournalNo());
}
private String getJournalQueryPageKey(Journal journal) {
List<Integer> journalNos = journalDao.findValidJournals().stream().map(j -> Integer.valueOf(j.getJournalNo()))
.sorted().collect(Collectors.toList());
Collections.reverse(journalNos);
JournalQueryReq journalQueryReq = new JournalQueryReq();
journalQueryReq.setPageNum(getPageNum(Integer.valueOf(journal.getJournalNo()), journalNos));
journalQueryReq.setPageSize(DEFAULT_PAGE_SIZE);
return journalQueryReq.toString();
}
private int getPageNum(int value, List<Integer> list) {
if (value > list.get(0)) {
return 0;
}
int pageSize = DEFAULT_PAGE_SIZE;
for (int pageNum = 1; (pageNum - 1) * pageSize < list.size(); pageNum++) {
int startIndex = (pageNum - 1) * pageSize;
int endIndex = pageNum * pageSize - 1;
if (endIndex > list.size()) {
return pageNum;
}
if (list.get(startIndex) >= value && list.get(endIndex) <= value) {
return pageNum;
}
}
return 0;
}
}

@ -122,7 +122,7 @@ public class UserCollectInfoService {
addSongCount(user);
break;
case JOURNAL:
addJournalCount(user);
addJournalCount(user, objectId);
break;
case FOLLOWS:
addFollowCount(user, objectId);
@ -165,7 +165,7 @@ public class UserCollectInfoService {
reduceSongCount(user);
break;
case JOURNAL:
reduceJournalCount(user);
reduceJournalCount(user, objectId);
break;
case FOLLOWS:
reduceFollowCount(user, objectId);
@ -298,12 +298,13 @@ public class UserCollectInfoService {
rabbitTemplate.convertAndSend("userMessage", userMessageDto);
}
private void addJournalCount(UserInfo user) {
private void addJournalCount(UserInfo user, String objectId) {
user.setJournalCount(user.getJournalCount() + 1);
userInfoDao.save(user);
// 更新redis 中的缓存
redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL + user.getId(),
getCollectSet(user.getId(), CollectTypeEnum.JOURNAL),6,TimeUnit.HOURS);
rabbitTemplate.convertAndSend(Constants.RABBIT_MESSAGE_USER_COLLECT_JOURNAL, objectId);
}
private void addSongCount(UserInfo user) {
@ -311,12 +312,13 @@ public class UserCollectInfoService {
userInfoDao.save(user);
}
private void reduceJournalCount(UserInfo user) {
private void reduceJournalCount(UserInfo user, String objectId) {
user.setJournalCount(user.getJournalCount() - 1);
userInfoDao.save(user);
// 更新redis 中的缓存
redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL + user.getId(),
getCollectSet(user.getId(), CollectTypeEnum.JOURNAL),6,TimeUnit.HOURS);
rabbitTemplate.convertAndSend(Constants.RABBIT_MESSAGE_USER_COLLECT_JOURNAL, "-"+objectId);
}
private void reduceSongCount(UserInfo user) {

Loading…
Cancel
Save