Interested people, lists, QQ group random display solutions

“This is the 23rd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Randomly display service scenario analysis

  1. In the e-commerce platform, because of the displayRegional co., LTD.It’s impossible to display all the data in a small space. The usual way is to display a random batch of data and then the user clicks”change“Button to randomly display another batch.
  2. If the recommended data is too much, it is impossible to load all of them at once, which is also a problem for network resources.

Random display of Redis technology solutions

As mentioned above, the reason for random display is limited area, and the limited area is usually the home page or channel page, which is usually with high volume of traffic and concurrent volume. It is generally impossible to use database to achieve the random display, which is usually achieved by Redis.

Redis implementation technical scheme:

  • Step 1: Get the data ready first. Store all the contents that need to be displayed into the Set data structure of Redis.
  • Step 2: Run the srandmember command to randomly obtain a batch of data.

Interested people,QQ group random recommendation

QQ group random recommendationRecommended by those who are interested in weibo

Step 1: Flush the data to the Redis cache in advance.

    /** * flush data to redis cache */
    @PostConstruct
    public void init(a){
        log.info("Start initializing qq group..........");
        List<String> crowds=this.crowd();
        this.redisTemplate.delete(Constants.CROWD_KEY);
        / / set structure
        crowds.forEach(t->this.redisTemplate.opsForSet().add(Constants.CROWD_KEY,t));
    }

    /** * simulate 100 popular groups for recommendation */
    public List<String> crowd(a) {
        List<String> list=new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Random rand = new Random();
            int id= rand.nextInt(10000);
            list.add("Group"+id);
        }
        return list;
    }
Copy the code

Step 2: Write a random query interface

@GetMapping(value = "/crowd")
public List<String> crowd(a) {
    List<String> list=null;
    try {
        // Use redis set data structure to randomly select 10 data
        list = this.redisTemplate.opsForSet().randomMembers(Constants.CROWD_KEY,10);
        log.info("Query result: {}", list);
    } catch (Exception ex) {
        // The redis network timeout is not enabled
        log.error("exception:", ex);
        //TODO go to DB query
    }
    return list;
}
Copy the code

Micro blog list random recommendation

The difference between the weibo list and the QQ group is that the weibo list is a block of data, click to change is another list of data, while the QQ group is a random display group, change or recommendation group, there is no block concept. So random data should be recommended by block so we define a Java Bean to wrap the whole block of data

Top trending list recommendations:

Click here to change the list:

Step 1: Flush the data to the Redis cache in advance.

@Data
public class WeiboList {
    
    private int id;
    /** * list name */
    private String name;

    private List<String> users;

}
Copy the code

    /** * Periodically flush the database to the redis cache. * /
    @PostConstruct
    public void init(a){
        log.info("Start initializing lists..........");
        List<WeiboList> crowds=this.list();
        this.redisTemplate.delete(Constants.WEIBO_LIST_KEY);
        crowds.forEach(t->this.redisTemplate.opsForSet().add(Constants.WEIBO_LIST_KEY,t));
    }


    /** * simulates 10 top lists for recommendation */
    public List<WeiboList> list(a) {
        List<WeiboList> list=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            WeiboList wl=new WeiboList();
            wl.setId(i);
            wl.setName("A list"+i);
            Random rand = new Random();
            List<String> users=new ArrayList<>();
            for (int j=0; j<3; j++){int id= rand.nextInt(10000);
                users.add("user:"+id);
            }
            wl.setUsers(users);
            list.add(wl);
        }
        return list;
    }
Copy the code

Step 2: Write a random query interface

    @GetMapping(value = "/weibolist")
    public WeiboList weibolist(a) {
        WeiboList list=null;
        try {
            // Select a random block of data
            list = (WeiboList)this.redisTemplate.opsForSet().randomMember(Constants.WEIBO_LIST_KEY);
            log.info("Query result: {}", list);
        } catch (Exception ex) {
            // The redis network timeout is not enabled
            log.error("exception:", ex);
            //TODO go to DB query
        }
        return list;
    }
Copy the code

Redis distributed cache family

  • Redis Distributed Cache (1) – Redis Installation (Linux and Docker)
  • Redis Distributed cache (ii) – RDB and AOF
  • SpringBoot integrates Mybatis-Plus,Redis and Swagger
  • Redis distributed cache (4) – SpringCache integrates redis
  • Redis Distributed Cache (5) — Common command (String)
  • Redis distributed cache (vi) – article read volume PV solution
  • Redis Distributed cache (7) – distributed global ID solution
  • Redis Distributed Cache (8) — Highly Concurrent atomic Operations (Redis+Lua)
  • Redis Distributed cache (NINE) – a hacker anti-brush attack solution
  • Redis Distributed Cache (10) common Commands (Hash)
  • Redis Distributed Cache (11) stores Java Bean object business profile one by one
  • Redis distributed Cache (twelve) — short link solution
  • Redis Distributed Cache (13) — JD Shopping cart solution
  • Redis distributed cache (14) – distributed session inconsistency solution
  • Redis Distributed Cache (15) – a highly available user registration solution
  • Redis distributed Cache (16) – user microblogging solution
  • Redis Distributed cache (17)
  • Redis Distributed Cache (18) – one – blacklist verifier solution
  • Redis Distributed Cache (19) – Tokyo Beans lottery solution
  • Redis Distributed Cache (20) – Alipay lottery solution
  • The article continues to be updated…