开发者

Which of my friends have the same 'like'?

开发者 https://www.devze.com 2023-01-09 06:09 出处:网络
I have web app where each user can have friends. Users can look at different pages on my site, like: pizza.html

I have web app where each user can have friends. Users can look at different pages on my site, like:

pizza.html
hamburger.html
etc..

If I am userA, and I'm looking at page 'pizza.html', I'd like to know which of my friends have also 'liked' this page. Assuming I want to show this when I load pizza.html, I could do it on demand really painfully like:

User me;
Like current = "pizza.html";
List<User> friendsWhoAlsoLikePageImLookingAt;
for (User friend : me.getFriends()) {
    for (Like like : friend.getLikes()) {
        if (like == current) {
            friendsWhoAlsoLikePageImLookingAt.add(friend);
        }
    }
}

// now friendsWhoAlsoLikePageImLookingAt contains all of my
// friends that like the page I'm viewing.

that would work, but would scale really poorly, as the # of friends and their likes grows. Is there any algorithm I can look into to solve this?

Another approach would be to precompute all likes for each user (this would not scale that well either I think). For example, each 开发者_StackOverflowuser gets their own txt file per interest like:

userA_pizza.txt
userB_pizza.txt
userA_hamburger.txt
userB_hamburger.txt
...

assuming I'm friends with userB, and userB adds 'pizza.html' as a new like - then I would update the userA_pizza.txt file to look like:

// userA_pizza.txt
userB  // latest friend to also like this interest.
userX  // previous friend likes
userY  // previous friend likes

now whenever I (userA) view the pizza page, I can open 'userA_pizza.txt' and just dump all the friends names without doing any computations. Now every time a user likes a page, I have to update N txt files, where N is the number of their friends. Furthermore, each user would need a text file per possible interest (and there could be thousands of interests). If a user has 100k friends and they each have 1000 interests, this could also be very costly!

Yes so any ideas or thoughts would be great -

Thanks


Couldn't you just keep a list per "like" page for which people liked it? Then, instead of having to go through all the "likes" for each friend for the current user, you could just go through the users who liked the page that user is currently on and only list those that are friends with the current user.

Still inefficient and not awesome for scaling, but that way, when a user likes a page you only have to update the actual "like" page and not a whole bunch of pages. Are you limited to text files only? A database could make this very easy.


I would use a database to store the data instead of text files.

You could have a table of users containing the users themselves, table userFriends linking users to the friends, a table of likes (one row per likable thing), a table userLikes linking users to what they like.

It would then me much easier to maintain and a database query to generate a recordset of what a user's friends like.


Assuming your back-end is a database, you should have 3 tables that are referenced for this functionality

User table (Id, Name, etc) 
Page table (Id, Url, etc)
LikedPages (UserId, PageId)

You can query LikedPages table against UserId to see a list of all likes for one user. You can also query against PageId to see who likes that page. The intersection of those two queries is the result you're looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号