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