Understand the micro blog push technology scheme of redis distributed cache

Java Sea of clouds 2022-01-27 05:20:24 阅读数:790

understand micro blog push technology

redis Distributed cache ( Thirty-one ) 11 ️ Microblog push technology solution

1️⃣ Business scenario analysis

  1. Focus on weibo
  • The login home page shows the microblogs sent by all the people I follow , The display form is a list
  • Scrolling with paging loading

image.png

2. Personal micro blog

  • My microblog is displayed on my personal microblog , The display form is also a list
  • Scrolling with paging loading

image.png

2️⃣ be based on redis Technical solution

Micro-blog

article

Personal micro blog

Focus on weibo

user

The server

use map structure-preservation

use list Structure save article id

Also used list Structure save article id

  1. The display forms of attention microblog and personal microblog are lists , use redis Of list Structure storage .
  2. Scrolling with paging loading , use redis Of  lrange key start stop Command range query element .

problem : Everyone has 2 individual list: One is a person list, One is attention list, And this 2 individual list Every time I tweet, I will push To the individual list And attention list, this 2 individual list There are performance problems , This is this. list Wireless growth . Time is long. redis Data continues to expand .

The most effective solution is to limit the number of times , Common scenarios are

  • qq Group , The number of wechat groups is limited
  • Baidu search limits the search results 75 page
  1. Limit the length of personal microblog and attention microblog to 1000, namely , When tweeting , To individual list And attention list push After completion , hold list The length of the cut is 1000, The specific technical scheme adopts list Of ltrim Command to implement .
  • ltrim key start end Intercept the elements of the specified interval of the queue , The rest of the elements are deleted

image.png

3️⃣ Code writing

️ Micro-blog , Write to your home page

 /**
* push Go to your home page
*/
public void pushHomeList(Integer userId,Integer postId){
// Personal microblog list key
String key="myPostBox:list:"+userId;
// Put the microblog article id Push to queue
this.redisTemplate.opsForList().leftPush(key,postId);
// Limited length , Prevent expansion , Before interception 1000 strip
if(this.redisTemplate.opsForList().size(key)>1000){
this.redisTemplate.opsForList().trim(key,0,1000);
}
}
Copy code 

️ Micro-blog , Mass push to fans

 /**
* Send a micro blog , Mass push to all fans
*/
private void pushFollower(int userId,int postId){
SetOperations<String, Integer> opsForSet = redisTemplate.opsForSet();
// Read fan collection , Fan collection with set preservation
String followerkey=Constants.CACHE_KEY_FOLLOWER+userId;
// Never take set All the data of the collection , If there's a lot of data , Club card
// Set<Integer> sets= opsForSet.members(followerkey);
Cursor<Integer> cursor = opsForSet.scan(followerkey, ScanOptions.NONE);
try{
while (cursor.hasNext()){
// Take out the fans userid
Integer object = cursor.next();
// The following list of this fan key
String key= "myAttentionBox:list:"+object;
// Add the article to the queue
this.redisTemplate.opsForList().leftPush(key,postId);
}
}catch (Exception ex){
log.error("",ex);
}finally {
try {
cursor.close();
} catch (IOException e) {
log.error("",e);
}
}
}
Copy code 

️ View personal list

 /**
* Get a list of personal home pages
*/
public PageResult<Content> homeList(Integer userId,int page, int size){
// Pagination
PageResult<Content> pageResult=new PageResult();
List<Integer> list=null;
long start = (page - 1) * size;
long end = start + size - 1;
try {
String key= "myPostBox:list:"+userId;
//1. Total number of query users
int total=this.redisTemplate.opsForList().size(key).intValue();
// Set the total number
pageResult.setTotal(total);
//2. use redis list data-structured lrange Command to realize paging query .
list = this.redisTemplate.opsForList().range(key, start, end);
//3. According to the article id Go to redis Query details ,redis If not, go to db take
List<Content> contents=this.getContents(list);
pageResult.setRows(contents);
}catch (Exception e){
log.error(" abnormal ",e);
}
return pageResult;
}
Copy code 

️ Check out the following list

 /**
* Get the following list
*/
public PageResult<Content> attentionList(Integer userId,int page, int size){
PageResult<Content> pageResult=new PageResult();
List<Integer> list=null;
long start = (page - 1) * size;
long end = start + size - 1;
try {
String key= "myAttentionBox:list:"+userId;
//1. Set the total number
int total=this.redisTemplate.opsForList().size(key).intValue();
pageResult.setTotal(total);
//2. use redis,list data-structured lrange Command to realize paging query .
list = this.redisTemplate.opsForList().range(key, start, end);
//3. According to the article id Go to redis Query details ,redis If not, go to db take
List<Content> contents=this.getContents(list);
pageResult.setRows(contents);
}catch (Exception e){
log.error(" abnormal ",e);
}
return pageResult;
}
Copy code 

redis Distributed caching series

Previous chapter : redis Distributed cache ( thirty ) 11 High concurrency articles in L2 cache PV Solution

  • : There is a harvest , Praise and encourage !
  • ️: Collect articles , It's convenient to look back !
  • : Comment exchange , Make progress with each other !
copyright:author[Java Sea of clouds],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201270520186160.html