Cache basic storage and read-write for reports

Source: Internet
Author: User
 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.

  • 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.