Microrelational computing solutions

1. Business analysis

Weibo weibo Relationship:

  • Mutual concern: Calculate which people A and B care about in common.

  • The people I follow follow him: Is to calculate which people in A’s following group also care about B as much as A does

2. Technical solutions

Traditional development, using a database to persist relational data, is as follows

Table:

id user_name fans
1 A C
2 A B
3 B A
4 C A
. . .

If it is to use a database to achieve the relationship between users, general SQL how to write?

  • For example :A focuses on 2 people, B focuses on 100 people, and calculates which people are the common concerns of 2 people?
    • SQL writing is generally implemented by in or not in. However, in or not in is obviously not suitable for highly concurrent systems on the Internet.
select fans from table_name where user_name='A' and fans in (select fans from table_name where user_name='B')
Copy the code
  • The general approach is to use redis set set to achieve.

Redis Set data structure, perfect for storing collections of friends, followers, fans, and interested people. Then use the set command to get the data we want.

  1. sinter: Obtain mutual friends of both users A and B
  2. sismember: Checks whether C is B’s friend
  3. scard: Gets the number of friends

1. 3. Smart trashes

  • Pay attention to the order
    • A, B, C, D
    • A B C D
    • C
    • A B C D
  • Micro relationships:
    • Common concern of A and B ={C,D}

Common concerns: What does Redis calculate as common concerns of A and B?

@apiOperation (value=" ")
@GetMapping(value = "/intersect")
public List<UserVO> intersect(Integer userId1, Integer userId2){
    return  this.relationService.intersect(userId1,userId2);
}
Copy the code
/** * find the intersection of two users' concerns */
public List<UserVO> intersect(Integer userId1,Integer userId2){
    SetOperations<String, Integer> opsForSet = redisTemplate.opsForSet();
    String fans1 = Constants.KEY_FANS + userId1;
    String fans2 = Constants.KEY_FANS + userId2;
    // Find the intersection of 2 sets
    Set<Integer> sets= opsForSet.intersect(fans1,fans2);
    // Return intersection user information
    return this.getUserInfo(sets);
}
Copy the code

1. 4. Smart trashes

  • Pay attention to the order
    • A, B, C, D
    • A B C D
    • C
    • A B C
  • Micro relationships:
    • APay attention to the peopleXXXAlso pay close attention toC
    • What are the people in XXX?
        1. {B, C, D}
          1. Whether the person concerned by B cares about c-true
          1. Whether the person concerned by C cares about c-false
          1. Whether the person concerned about D cares about c-true
          • XXX={B,D}

Case 1: XXX, the person whom A cares about, also cares about C

Find out who is in XXX

/** *- A: B C D *- B: A C D *- C: C A * -d concern: A B C * - micro relation: * - A concern of the person XXX also concern C: 'XXX={B,D}' * - 1. Who {B, C,D} * - 2. Whether C-true * - 3@param userId1 A
 * @param userId2 C
 * @return XXX:XXX is also concerned about C */
public List<UserVO> getUser1(String userId1, String userId2) {
    SetOperations<String, Integer> opsForSet = redisTemplate.opsForSet();
    Set<Integer> users = new HashSet<>();
    String followeekey1 = Constants.KEY_FANS + userId1;
    // if a is concerned about c
    Boolean c = opsForSet.isMember(followeekey1, userId2);
    if (c) {
        //a focus on what people are
        Set<Integer> aMembers = opsForSet.members(followeekey1);
        if(aMembers ! =null) {
            aMembers.forEach(m -> {
                String memberKsy = Constants.KEY_FANS + m;
                // Whether the person a cares about cares about C
                Boolean isMember = opsForSet.isMember(memberKsy, userId2);
                if(isMember) { users.add(m); }});// Query user information based on the user ID
            return this.getUserInfo(users); }}return null;
}
Copy the code

Redis distributed cache series

Redis distributed Cache (23) – Micro blog attention and fans solution