Redis completed using simple backend Netease cloud music

NetEase cloud music analysis function 
1. Store all songs (Comments)
2. A plurality of comments corresponding to song
3. Each point can review Like Like numbered according to the ranking
4. single song set list of songs already sorted heat (reviews * 432 + the number of people who like this song * 432 (you can also adjust the ratio) + published)
5. my favorite songs

Solution 
1. Hash songs stored using specific information stored String ID
2. The Set corresponding to each song stored in a comment ID
3. Use zset
4. Use the Set songs stored ID
5. The use java reflection

pojo:

Comments entity class
 1 public class Comment {
 2 
 3     private String id;
 4 
 5     private String comment;
 6 
 7     private String time;
 8 
 9     private String good;
10 
11     public Comment() {
12     }
13 
14     public String getId() {
15         return id;
16     }
17 
18     public void setId(String id) {
19         this.id = id;
20     }
21 
22     public String getComment() {
23         return comment;
24     }
25 
26     public void setComment(String comment) {
27         this.comment = comment;
28     }
29 
30     public String getTime() {
31         return time;
32     }
33 
34     public void setTime(String time) {
35         this.time = time;
36     }
37 
38     public String getGood() {
39         return good;
40     }
41 
42     public void setGood(String good) {
43         this.good = good;
44     }
45 }

Songs entity class

 1 public class Song {
 2 
 3     private String id;
 4 
 5     private String name;
 6 
 7     private String singer;
 8 
 9     private String comments;
10 
11     private List<Comment> commentList;
12 
13     public Song() {
14     }
15 
16     public Song(String name, String singer) {
17         this.name = name;
18         this.singer = singer;
19         this.comments = "0";
20     }
21 
22     public String getId() {
23         return id;
24     }
25 
26     public void setId(String id) {
27         this.id = id;
28     }
29 
30     public String getName() {
31         return name;
32     }
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37 
38     public String getSinger() {
39         return singer;
40     }
41 
42     public void setSinger(String singer) {
43         this.singer = singer;
44     }
45 
46     public String getComments() {
47         return comments;
48     }
49 
50     public void setComments(String comments) {
51         this.comments = comments;
52     }
53 
54     public List<Comment> getCommentList() {
55         return commentList;
56     }
57 
58     public void setCommentList(List<Comment> commentList) {
59         this.commentList = commentList;
60     }
61 }


New Songs
1  // New Song 
2      Private String addSong (Jedis conn, Song Song) {
 3          // get the song ID
 4          // ID does not exist will create a (1) presence will increase from 
5          String the above mentioned id = String.valueOf (conn .incr ( "song:" ));
 . 6  
. 7          // songs stored in the hash 
. 8          String Key = "song:" + ID;
 . 9          the HashMap <String, String> Map = new new the HashMap <String, String> ();
 10          map.put ( "ID" , ID);
 . 11          map.put ( "name" , song.getName ());
 12 is          map.put ( "Singer", Song.getSinger ());
 13 is          map.put ( "Comments" , String.valueOf (song.getComments ()));
 14          conn.hmset (Key, Map);
 15  
16          // record heat initialized to the current time 
17          conn.zadd ( "Hot:" , now (), Key);
 18 is          return ID;
 . 19      }

 

Increased Reviews

1  // increase comment 
2      Private  void the addComment (Jedis Conn, ID String, String Comment) {
 . 3          String songKey = "Song:" + ID;
 . 4          // song information, comment, or add 1 
. 5          conn.hincrBy (songKey, "Comments ", 1 );
 6          // add additional heat 
. 7          conn.zincrby (" Hot: " , VOTE_SCORE, songKey);
 . 8  
. 9          String commentKey =" Song: "+ ID +" comments: " ;
 10  
. 11          // get comments ID 
12 is          CommentID = String String.valueOf (conn.incr (commentKey));
 13 is  
14         commentKey += commentID;
15 
16         HashMap<String, String> map = new HashMap<String, String>();
17         map.put("id", commentID);
18         map.put("comment", comment);
19         map.put("time", time());
20         map.put("good", "0");
21 
22         conn.hmset(commentKey, map);
23 
24         //默认给自己点个赞
25         conn.zadd("song:" + id + " score:", 1, "comments:" + commentID);
26     }

 

Comments thumbs up

1  // thumbs 
2      Private  void Good (Jedis Conn, Song String, String Comment) {
 . 3          // increase the number of points like (rank) 
. 4          conn.zincrby ( "Song:" Song + + "Score:", 1, " Comments: "+ Comment);
 . 5          // the hash recorded 
. 6          conn.hincrBy (" song: "song + +" Comments: "Comment +," Good ",. 1 );
 . 7          // add songs heat 
. 8          conn.zincrby ( "Hot:", VOTE_SCORE, "Song:" + Song);
 . 9      }

 

Queries song

 1 //按照热度获取歌曲
 2     private List<Song> song(Jedis conn) {
 3         Set<String> set = conn.zrevrange("hot:", 0, -1);
 4         List<Song> list = new ArrayList<>();
 5         for (String str : set) {
 6             list.add(get(conn.hgetAll(str), conn));
 7         }
 8         return list;
 9     }
10 
11     private Song get(Map<String, String> map, Jedis conn) {
12         Class<Song> clazz = Song.class;
13         Song song = null;
14         try {
15             song = clazz.newInstance();
16         } catch (InstantiationException | IllegalAccessException e) {
17             e.printStackTrace();
18         }
19         for (Map.Entry<String, String> entry : map.entrySet()) {
20             try {
21                 Field field = clazz.getDeclaredField(entry.getKey());
22                 if (entry.getKey().equals("id")) {
23                     String key = "song:" + entry.getValue() + " score:";
24                     Set<String> set = conn.zrevrange(key, 0, -1);
25                     List<Comment> list = new ArrayList<>();
26                     for (String str : set) {
27                         list.add(getComment(conn.hgetAll("song:" + entry.getValue() + " " + str)));
28                     }
29                     Field commentList = clazz.getDeclaredField("commentList");
30                     commentList.setAccessible(true);
31                     commentList.set(song, list);
32                 }
33                 field.setAccessible(true);
34                 field.set(song, entry.getValue());
35             } catch (IllegalAccessException | NoSuchFieldException e) {
36                 e.printStackTrace();
37             }
38         }
39         return song;
40     }
41 
42     private Comment getComment(Map<String, String> map) {
43         Class<Comment> clazz = Comment.class;
44         Comment comment = null;
45         try {
46             comment = clazz.newInstance();
47         } catch (InstantiationException | IllegalAccessException e) {
48             e.printStackTrace();
49         }
50         for (Map.Entry<String, String> entry : map.entrySet()) {
51             try {
52                 Field field = clazz.getDeclaredField(entry.getKey());
53                 field.setAccessible(true);
54                 field.set(comment, entry.getValue());
55             } catch (IllegalAccessException | NoSuchFieldException e) {
56                 e.printStackTrace();
57             }
58         }
59         return comment;
60     }

 

 

Methods Tools

1 private long now() {
2         return System.currentTimeMillis() / 1000;
3     }
4 
5     private String time() {
6         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
7         return df.format(new Date());
8     }

 experiment

 1 private static final int VOTE_SCORE = 432;
 2 
 3     private static Song[] songs = new Song[5];
 4 
 5     static {
 6         for (int i = 0; i < songs.length; i++) {
 7             songs[i] = new Song("1989" + i, "Taylor Swift" + i);
 8         }
 9     }
10 
11     public static void main(String[] args) {
12         MyChapter01 chapter01 = new new MyChapter01 ();
 13 is  
14          Jedis Conn = new new Jedis ( "localhost" );
 15          conn.select (10 );
 16  
. 17  //         for (Song Song: Songs) {
 18 is  //             chapter01.addSong (Conn, Song);
 . 19  @         }
 20 is  
21 is  //         chapter01.addComment (Conn, '. 6 "," Terrific mildew mildew ");
 22 is  //         chapter01.addComment (Conn,'. 6", "fuel goddess");
 23 is  
24          // less Like a verified write point HHH SET 
25          chapter01.good (Conn, ". 6", ". 1" );
26 
27         List<Song> list = chapter01.song(conn);
28         for (Song song : list) {
29             System.out.println(song);
30         }
31     }

 

result

 




Guess you like

Origin www.cnblogs.com/Gang-Bryant/p/10993729.html