功能:根据用户展示pn的相关信息,能搜索具体的pn, 可以分页思路:redis常用结构第一就想到了hash,可以搜索和存储信息;Sorted Set 可以搜索和截取 将Pn存储在两种结构中,先从set分页后,在去hash获取具体内容
数据格式:'用户ID1' => [ 'pnNumber1' => [ 'pnName' => 'E', 'pnCategory' => 'A', 'pnDesc' => 'C' ], 'pnNumber2' => [ 'pnName' => 'E', 'pnCategory' => 'A', 'pnDesc' => 'C' ] //...],//...redis数据格式://sorted set'setName' => [ 'pnNumber1', 'pnNumber2'],//hash 将pn号作为hash的field, 相关信息编码成json格式作为field的value'hashName' => [ 'pnNumber1' => '{pnName: "E", pnCategory: "A", pnDesc: "C"}']代码实现://add to sorted set (sorted set 可以用来排序,有个分数的概念,所以这里用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));//分页的实现,结合redis的两种数据格式public function getPartByLimit($start, $end){ $result = []; //先从set中取出pn(从大到小排列) $pns = Yii::$app->redis->zrevrange($this->setName, $start, $end); //循环pn从hash中取出相应信息 foreach ($pns as $pn) { $result[] = json_decode(Yii::$app->redis->hget($this->hashName, $pn), true); } return $result;}//搜索的实现if (Yii::$app->redis->hexists($this->hashName, $pn)) { return json_decode(Yii::$app->redis->hget($this->hashName, $pn), true);}