Php/** * Class Rpt Redis is used to cache basic storage and read-Write 2.0 * @simple * rpt::read ("Diamond.account", $nick); * Rpt::readsync ("Diamond.accoun T ", $nick); * $finder = Rpt::createfinder ("Diamond.account", $nick); * $finder->read (); * $finder->readsync (); * * Rpt::save ("Diamond.account", $nick, $data); * $storage = Rpt::createstorage ("Diamond.account", $nick); * $storage->save ($data); * $storage->save ($data, 7200); */classRpt { Public Static functionRead$key,$nick){ $finder= Self::createfinder ($key,$nick); return $finder-read (); } Public Static functionReadsync ($key,$nick){ $finder= Self::createfinder ($key,$nick); return $finder-Readsync (); } Public Static functionCreatefinder ($key,$nick){ $key= Rptgenerate::Key($key,$nick); return NewRptfinder ($key); } Public Static functionCreatestorage ($key,$nick){ $key= Rptgenerate::Key($key,$nick); return NewRptstorage ($key); } Public Static functionSave$key,$nick,$data,$expired=7200){ $storage= Self::createstorage ($key,$nick); return $storage->save ($data,$expired); } Public Static functionCreateredis () {$redis=NewRedis (); $redis->connect (Yii::app ()->params["Redisserverip"]); return $redis; }}/** * Class rptfinder Data Read*/classRptfinder {/** * @var string $key*/ Public $key; /** * @param string $key*/ Public function__construct ($key){ $this-Key=$key; } /** * Non-secure read data * @return mixed*/ Public functionRead () {$data=$this-ReadData (); if($data->isread &&!$data-isexpired ())return $data-data; return NULL; } protected functionReadData () {$redis= Rpt::Createredis (); $rptData=NewRptdata (); $data= Json_decode ($redis->get ($this-Key)); if(false==$data){ $rptData->isread =false; $rptData->expiredtime = Time(); $rptData->expired = 24*3600; }Else{ $rptData->expired =$data-expired; $rptData->isread =$data-Isread; $rptData->expiredtime =$data-ExpiredTime; $rptData->data =$data-data; } return $rptData; } /** * Read data synchronously * @return mixed*/ Public functionReadsync () { while(true){ $rptData=$this-ReadData (); if($rptData->isread &&!$rptData-isexpired ())return $this-read (); Sleep(1); } }}/** * Class rptstorage data storage*/classRptstorage {/** * @var string key*/ Public $key; /** * @param string $key*/ Public function__construct ($key){ $this-Key=$key; } /** * Write Data * @param $data * @param int $expired * @return bool*/ Public functionSave$data,$expired=7200){ $rptData=NewRptdata (); $rptData->data =$data; $rptData->expiredtime = Time(); $rptData->isread =true; $rptData->expired =$expired; $redis= Rpt::Createredis (); return $redis->setex ($this-Key,$rptData->expired,json_encode ($rptData)); }}/** * Class rptdata redis Storage Data Entity*/classRptdata { Public $expired; Public $expiredTime; Public $data; Public $isRead; Public functionisexpired () {if( Time()-$this->expiredtime >$this-expired)return true; return false; }}/** * Class rptgenerate key Generation*/classRptgenerate { Public Static function Key($key,$nick){ return $key.".".MD5($nick); }}
The above describes the report cache basic storage and read and write, including the content, I hope the PHP tutorial interested in a friend helpful.