Java Sea of clouds 2022-01-27 05:20:24 阅读数:790
2. Personal micro blog
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
list
Structure storage .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
ltrim key start end
Intercept the elements of the specified interval of the queue , The rest of the elements are deleted /**
* 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
/**
* 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
/**
* 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
/**
* 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
Previous chapter : redis Distributed cache ( thirty ) 11 High concurrency articles in L2 cache PV Solution
copyright:author[Java Sea of clouds],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201270520186160.html