In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Build an article voting website, which generally has the following functions
Publish an article
Article voting score (graded according to the number of votes cast)
Articles are sorted (by release time, by grade)
Article grouping (eg topics)
...
1. Relational database design
Among them, the two tables of users and groups are simplified. The business is also quite simple to implement. I won't repeat it. The focus is on how to use redis to implement similar business logic.
Because redis is based on key-value management, it belongs to column database. There is a big difference between the implementation of relational database and relational database, which is worth studying.
The most important part of the design of redis is the naming of key and the selection of key data types.
2.Redis design
Relational database belongs to two-dimensional, the data relationship is mainly explained by both rows and columns, while the data relationship in redis is described by key key value, so the redis key value is required to have a hierarchy.
2.1 article release
Implementation code
Private static final int ONE_WEEK_IN_SECONDS = 7 * 86400; private static final int VOTE_SCORE = 432; public String postArticle (Jedis conn, String user, String title, String link) {String articleId = String.valueOf (conn.incr ("article:")); String voted = "voted:" + articleId; conn.sadd (voted, user); conn.expire (voted, ONE_WEEK_IN_SECONDS); / / one-week validity long now = System.currentTimeMillis () / 1000 String article = "article:" + articleId; HashMap articleData = new HashMap (); articleData.put ("title", title); articleData.put ("link", link); articleData.put ("user", user); articleData.put ("now", String.valueOf (now)); articleData.put ("votes", "1"); conn.hmset (article, articleData) / / maintain two sorting sets to solve the two sorting methods of articles / / if there are three sorting methods, I'm sorry, you also need to maintain another sorting set conn.zadd ("score:", now + VOTE_SCORE, article); / / maintain the score information of articles conn.zadd ("time:", now, article); / / maintain the release time information of articles return articleId;}
2.2 Article voting
Implementation code
Public void articleVote (Jedis conn, String user, String article) {long cutoff = (System.currentTimeMillis () / 1000)-ONE_WEEK_IN_SECONDS; if (conn.zscore ("time:", article) < cutoff) {return;} String articleId = article.substring (article.indexOf (':') + 1) / / maintain one-time if for voting (conn.sadd ("voted:" + articleId, user) = = 1) {conn.zincrby ("score:", VOTE_SCORE, article); conn.hincrBy (article, "votes", 11);}}
2.3 return the list of articles
There are two sorting strategies: by release time and by article.
Paging sorting is supported.
The implementation of sorting in redis is very different from that in relational database, which is also a major feature of key-value database.
Based on key operation.
Public List getArticles (Jedis conn, int page, String order) {int start = (page-1) * ARTICLES_PER_PAGE; int end = start + ARTICLES_PER_PAGE-1; / / get the id list from the sorted collection Set ids = conn.zrevrange (order, start, end); List articles = new ArrayList (); / / traverse the id list, initializing for (String id: ids) {Map articleData = conn.hgetAll (id) one by one ArticleData.put ("id", id); articles.add (articleData);} / / Note: there is no list of total return articles; in the returned information.
2.4 articles grouped
This piece of logic is relatively independent, it is only an analytical dimension of the article, and it is relatively simple to operate. Is to maintain the groups:$ {group} collection.
Public void addGroups (Jedis conn, String articleId, String [] toAdd) {String article = "article:" + articleId; for (String group: toAdd) {conn.sadd ("group:" + group, article);}} / / sort public List getGroupArticles (Jedis conn, String group, int page, String order) {String key = order + group / / 60-second validity period if (! conn.exists (key)) {ZParams params = new ZParams () .aggregate (ZParams.Aggregate.MAX); conn.zinterstore (key, params, "group:" + group, order); / / ordered set, intersection with group to generate a new set conn.expire (key, 60) / / the validity period of 60 seconds, the balance between performance and real-time performance, you need to analyze} return getArticles (conn, page, key);}
Zinterstore API
Public java.lang.Long zinterstore (java.lang.String dstkey
Redis.clients.jedis.ZParams params
Java.lang.String... Sets)
Reference resources
"redis in action"
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.