Complete search/pagination/sorting using Redis

Source: Internet
Author: User
Tags yii

Function: According to the user display PN information, can search for specific pn, can be paged ideas: Redis commonly used structure first think of the hash, you can search and store information; Sorted Set can search and intercept
Store PN in two structures, first from set paging, in the hash to get the specific content


Data format:' User ID1 ' = [ ' PnNumber1 ' = [ ' Pnname ' = ' E ', ' pncategory ' = ' A ', ' pndesc ' = ' C ' ], ' pnNumber2 ' = [ ' Pnname ' = ' E ', ' pncategory ' = ' A ', ' pndesc ' = ' C ' ] //...],//...redis data format://Sorted Set' SetName ' = [ ' PnNumber1 ', ' pnNumber2 '],//Hash The PN number as the field of the hash, the relevant information encoded in JSON format as field value' Hashname ' = [ ' PnNumber1 ' = ' {pnname: ' E ', pncategory: ' A ', Pndesc: ' C '} '] Code implementation://add to sorted set (sorted set can be used to sort the concept of a fraction, so here's The time ())YII::$app->redis->zadd ($this->setname, Time(),$PN);//Add to Hash$data= [ ' PN ' =$PN, ' desc ' =$desc, ' category ' =$category]; Yii::$app->redis->hset ($this->hashname,$PN, Json_encode ($data));//The implementation of paging, combined with two data formats of Redis Public functionGetpartbylimit ($start,$end){ $result= []; //first remove PN from set (from large to small) $pns= Yii::$app->redis->zrevrange ($this->setname,$start,$end); //loop PN Extracts the corresponding information from the hash foreach($pns as $PN) { $result[] = Json_decode (Yii::$app->redis->hget ($this->hashname,$PN),true); } return $result;}//implementation of the searchif(Yii::$app->redis->hexists ($this->hashname,$PN)) { returnJson_decode (Yii::$app->redis->hget ($this->hashname,$PN),true);}

Complete search/pagination/sorting using Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.