Basic storage and read/write of the report cache, and read/write of the report Cache

Source: Internet
Author: User

Basic storage and read/write of the report cache, and read/write of the report Cache

<? Php/*** Class Rpt redis is used to Cache basic storage and read/write reports 2.0 * @ simple * Rpt: read ("diamond. account ", $ nick); * Rpt: readSync (" diamond. account ", $ 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, 7200); */class Rp T {public static function read ($ key, $ nick) {$ finder = self: createFinder ($ key, $ nick); return $ finder-> read ();} public static function readSync ($ key, $ nick) {$ finder = self: createFinder ($ key, $ nick); return $ finder-> readSync ();} public static function createFinder ($ key, $ nick) {$ key = RptGenerate: key ($ key, $ nick); return new RptFinder ($ key );} public static function createStorage ($ key, $ nick) {$ key = RptGenerate: key ($ key, $ nick); return new RptStorage ($ key);} public static function save ($ key, $ nick, $ data, $ expired = 7200) {$ storage = self: createStorage ($ key, $ nick); return $ storage-> save ($ data, $ expired );} public static function createRedis () {$ redis = new Redis (); $ redis-> connect (Yii: app ()-> params ["RedisServerIP"]); return $ redis;}/*** Class RptFinder Data Reading */class RptFinder {/*** @ var s Tring $ key */public $ key;/*** @ param string $ key */public function _ construct ($ key) {$ this-> key = $ key ;} /*** unsafe data Reading * @ return mixed */public function read () {$ data = $ this-> readData (); if ($ data-> isRead &&! $ Data-> isExpired () return $ data-> data; return null;} protected function readData () {$ redis = Rpt: createRedis (); $ rptData = new RptData (); $ 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;}/*** synchronously read data * @ return mixed */public function readSync () {while (true) {$ rptData = $ this-> readData (); if ($ rptData-> isRead &&! $ RptData-> isExpired () return $ this-> read (); sleep (1 );}}} /*** Class RptStorage data storage */class RptStorage {/*** @ 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 function save ($ data, $ expired = 7200) {$ rptData = new RptData (); $ 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 stores data entities */class RptData {public $ expired; public $ expiredTime; public $ data; public $ isRead; public function isExpired () {if (time ()-$ this-> expiredTime> $ this-> expired) return true; return false ;}} /*** Class RptGenerate key generation */class RptGenerate {public static function key ($ key, $ nick) {return $ key. ". ". md5 ($ nick );}}

 

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.