This article mainly introduces the implementation of phpredis and the user voting system, which has some reference value, interested friends can refer to this article for details on the php redis implementation article publishing system and user voting system, which has a certain reference value, for more information, see
The examples in this article share the code for implementing the php article publishing system and user voting system for your reference. the specific content is as follows:
/*** @ Data article publishing * incremental ID in the detailed breakdown list of the article, indicating that the ID of the article publisher is written into the voting user set, set the voting time to one week * write the article content to the article hash, about writing articles into an orderly set of article scores and an orderly set of article publishing * @ author Lorne * @ date 2017-03-03 */public function post_article ($ user) {$ VOTE_SCORE = 24; $ redis = $ this-> redis; $ key = "queue"; $ ONE_WEEK_IN_SECONDS = 7*86400; $ redis-> multi ($ key ); // generate a new article id $ article_id = $ redis-> incr ("article:", $ key); // The voting username list in the article $ voted = "voted :". $ article_id; $ this-> redis-> sadd ($ voted, $ user, $ key); // Set the Expiration Time (1 week) $ this-> redis-> expipre ($ voted, $ ONE_WEEK_IN_SECONDS, $ key); // get the current time $ now = time (); $ article = "article :". $ article_id; $ data = ['title' => 'test 1', 'link' => 'Www .hahaha.com ', 'poster' => $ user, 'twine' => $ now, 'Votes '=> 1]; // $ data = json_encode ($ data); $ redis-> hmset ($ article, $ data, $ key); // add an article to a sorted set by time and a sorted combination by scoring $ this-> redis-> zadd ("score:", 1, $ article, $ key); $ this-> redis-> zadd ("time:", $ now, $ article, $ key ); $ redis-> exec ($ key );}
/*** @ Data User vote * get the article ID and user ID to determine whether the article has passed the voting time, then determine whether the user has voted * write the article into the corresponding voting user table (voted: Article ID), add the corresponding article score, add 1 * @ author Lorne * @ date 2017-03-03 */public function article_vote () {$ ONE_WEEK_IN_SECONDS = 7*86400; $ article = "article: 3 "; $ user = "user: 7777"; $ redis = $ this-> redis; $ key = "queue"; $ cutoff = time ()-$ ONE_WEEK_IN_SECONDS; // comparison of the article release time and voting end date if ($ redis-> zscore ('Time: ', $ article, $ key) <$ Cutoff) {var_dump ("This article has passed the voting time! "); Exit;} $ article_id = explode (':', $ article) ['1']; if ($ redis-> sadd ('voted :'. $ article_id, $ user, $ key) {$ redis-> zincrby ('Score: ', $ article, 1, $ key); $ redis-> hincrby ($ article, 'votes ', 1, $ key);} else {var_dump ("You have voted! "); Exit ;}}
/*** @ Data article list page * processes the sorted set of article scores or the sorted set of time publication by page. after obtaining the article ID, go to the article details hash to query the article details * @ author Lorne * @ date 2017-03-03 */public function get_articles ($ page = 1, $ orders = '') {$ redis = $ this-> redis; $ db = "queue"; // $ orders = "time:"; $ per_page = 3; $ start = ($ page-1) * $ per_page; $ end = $ start + $ per_page-1; $ ids = $ redis-> zrevrange ($ orders, $ start, $ end, $ db); foreach ($ ids as $ key => $ val) {$ data = $ redis-> hgetall ($ val, $ db ); $ data ['id'] = $ val; $ articles [] = $ data;} return $ articles ;}
/*** @ Data article add Group and remove Group *: This article is added to different groups, or remove the article * @ author Lorne * @ date 2017-03-03 */public function add_remove_group ($ article_id, $ to_add = [], $ to_remove = []) from the group. {$ redis = $ this-> redis; $ db = "queue"; $ article = "article :". $ article_id; foreach ($ to_add as $ key => $ val) {$ redis-> sadd ('group :'. $ val, $ article, $ db);} foreach ($ to_remove as $ key => $ val) {$ redis-> srem ('grouo :'. $ val, $ article, $ db );}}
/*** @ Articles in the data group set are sorted by score or time pagination ** @ author Lorne * @ date */public function get_grouop_articles ($ orders = "time :") {$ redis = $ this-> redis; $ db = "queue"; $ group = 'developing'; $ key = $ orders. $ group; if ($ redis-> exists ($ key, $ db) {$ argument = 2; $ data = $ redis-> zinterstore ($ key, $ argument, ['Group :'. $ group, $ orders], $ db); // $ this-> expire ($ key, 60, $ db);} return $ this-> get_articles (2, $ key );}
Php + redis is used to easily implement the article publishing system, user voting, article grouping, and paging sorting.
Redis is a high-performance key-value storage system. The five most common types are string, list, and set), zset (sorted set -- ordered set), and hash (hash type ), what's different from memcache is that redis periodically writes updated data to the disk or writes modifications to the append record file. In addition, it achieves master-slave (master-slave) synchronization.
Recently, I have become more and more fond of using redis. if you are interested in redis, I would like to share more with you.
The above is the content published in the php, redis and article. For more information, please follow the PHP Chinese network (www.php1.cn )!