I'm currently implementing a doc with a like button like this:
The like button is associated with certain user account. When you press a like, it will stay liked for that user (similar to youtube video).
My entities and DTOs are below:
Doc.java:
@Entity(name = "Doc")
@Table(name = "doc")
@Data
public class Doc {
//Unrelated code reacted for clarity
@ManyToMany(cascade = {
CascadeType.MERGE,
CascadeType.PERSIST
})
@JoinTable(
name = "doc_user_dislike",
joinColumns = @JoinColumn(name = "doc_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private Set<UserWebsite> dislikedUsers;
@ManyToMany(cascade = {
CascadeType.MERGE,
CascadeType.PERSIST
})
@JoinTable(
name = "doc_user_like",
joinColumns = @JoinColumn(name = "doc_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private Set<UserWebsite> likedUsers;
}
User.java:
@Entity
@Table(name = "user_website")
@Data
public class UserWebsite {
//Unrelated code reacted for clarity
@ManyToMany(mappedBy = "likedUsers")
private Set<Doc> likedDocs;
@ManyToMany(mappedBy = "dislikedUsers")
private Set<Doc> dislikedDocs;
}
DocDetailsDTO.java (This will be sent to client).
@Data
public class DocDetailsDTO {
private Long id;
private Boolean isDisliked;
private Boolean isLiked;
}
I'm having some solutions:
- Add a field called isLiked to Doc.java with @Formular combine with
@Transient and perform queries to DB.
- Have another API which accept from Client a list of DocID, and a
UserID, then return a list of DocID that UserID liked.
- Check if UserID exist in likedUsers list (not very efficient,
sometimes not feasible since I have to initialize that big
lazy-loaded list).
The question is: What is the most efficient way to retrieve liked/disliked status for many post at once (>10 doc but max 100 doc per request) for about thousand users (1000 CCU) at once ? Are above solutions already optimal ?
Any help is appreciated. Thanks for your time reading through the question.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…