Redis Cache php code
Class redisCache {/*** $ host: redis server ip address * $ port: redis server port * $ lifetime: cache file validity period, in seconds * $ cacheid: cache file path, contains the file name */private $ host; private $ port; private $ lifetime; private $ cacheid; private $ data; public $ redis;/*** destructor, check whether the cache directory is valid. the default value is */function _ construct ($ lifetime = 1800) {// Configure $ this-> host = "127.0.0.1 "; $ this-> port = "6379"; $ redis = new Redis (); $ redis-> pconnect ($ this-> host, $ this-> po Rt); $ this-> redis = $ redis; $ this-> cacheid = $ this-> getcacheid (); $ this-> lifetime = $ lifetime; $ this-> data = $ redis-> HMET ($ this-> cacheid, array ('content', 'creattime ')); // print_r ($ this-> redis); // print_r ($ this-> data);}/*** check whether the cache is valid */private function isvalid () {$ data = $ this-> data; if (! $ Data ['content']) return false; if (time ()-$ data ['creattime']> $ this-> lifetime) return false; return true ;} /*** write cache ** $ mode = 0, get the page content in browser cache */public function write ($ mode = 0, $ content = '') {switch ($ mode) {case 0: $ content = ob_get_contents (); break; default: break;} ob_end_flush (); try {$ this-> redis-> hMset ($ this-> cacheid, array ('content' => $ content, 'creattime' => time ()));} catch (Exc Eption $ e) {$ this-> error ('write cache failed! ') ;}}/*** Load cache * exit () after loading the cache, terminate the execution of the original page program. if the cache is invalid, run the original page program to generate the cache * ob_start () enable browser cache to get page content at the end of the page */public function load () {if ($ this-> isvalid ()) {echo $ this-> data ['content']; exit () ;}else {ob_start () ;}/ *** clear cache */public function clean () {try {$ this-> redis-> hDel ($ this-> cacheid, array ('content', 'creattime');} catch (Exception $ e) {$ this-> error ('failed to clear cache! ') ;}}/*** Obtain the cache file path */private function getcacheid () {return $ this-> dir. md5 ($ this-> geturl ()). $ this-> ext;}/*** get the complete url of the current page */private function geturl () {$ url = ''; if (isset ($ _ SERVER ['request _ URI ']) {$ url = $ _ SERVER ['request _ URI'];} else {$ url = $ _ SERVER ['php _ SELF ']; $ url. = empty ($ _ SERVER ['query _ string'])? '':'? '. $ _ SERVER ['query _ string'];} return $ url;}/*** output error message */private function error ($ str) {echo''. $ Str .'
';} // Usage: // require_once ('rediscache. php '); // $ cache = new redisCache (10); // you can specify the cache lifetime. // if ($ _ GET ['expacache']) $ cache-> clean (); // else $ cache-> load (); // load the cache, if the cache is valid, the following page code is not executed. // The page code starts. // The page code ends. // $ cache-> write (); // The first run or the cache expires, generate cache
Fix redis Cache survival time
Class redisCache {/*** $ host: redis server ip address * $ port: redis server port * $ lifetime: cache file validity period, in seconds * $ cacheid: cache file path, contains the file name */private $ host; private $ port; private $ lifetime; private $ cacheid; private $ data; public $ redis;/*** destructor, check whether the cache directory is valid. the default value is */function _ construct ($ lifetime = 1800) {// Configure $ this-> host = "127.0.0.1 "; $ this-> port = "6379"; $ redis = new Redis (); $ redis-> pconnect ($ this-> host, $ this-> po Rt); $ this-> redis = $ redis; $ this-> cacheid = $ this-> getcacheid (); $ this-> lifetime = $ lifetime; $ this-> data = $ redis-> HMET ($ this-> cacheid, array ('content', 'creattime ')); // print_r ($ this-> redis); // print_r ($ this-> data);}/*** check whether the cache is valid */private function isvalid () {$ data = $ this-> data; if (! $ Data ['content']) return false; if (time ()-$ data ['creattime']> $ this-> lifetime) return false; return true ;} /*** write cache ** $ mode = 0, get the page content in browser cache */public function write ($ mode = 0, $ content = '') {switch ($ mode) {case 0: $ content = ob_get_contents (); break; default: break;} ob_end_flush (); try {$ this-> redis-> hMset ($ this-> cacheid, array ('content' => $ content, 'creattime' => time ())); $ this-> redis -> ExpireAt ($ this-> cacheid, time () + $ this-> lifetime);} catch (Exception $ e) {$ this-> error ('write cache failed! ') ;}}/*** Load cache * exit () after loading the cache, terminate the execution of the original page program. if the cache is invalid, run the original page program to generate the cache * ob_start () enable browser cache to get page content at the end of the page */public function load () {if ($ this-> isvalid ()) {echo $ this-> data ['content']; exit () ;}else {ob_start () ;}/ *** clear cache */public function clean () {try {$ this-> redis-> hDel ($ this-> cacheid, array ('content', 'creattime');} catch (Exception $ e) {$ this-> error ('failed to clear cache! ') ;}}/*** Obtain the cache file path */private function getcacheid () {return $ this-> dir. md5 ($ this-> geturl ()). $ this-> ext;}/*** get the complete url of the current page */private function geturl () {$ url = ''; if (isset ($ _ SERVER ['request _ URI ']) {$ url = $ _ SERVER ['request _ URI'];} else {$ url = $ _ SERVER ['php _ SELF ']; $ url. = empty ($ _ SERVER ['query _ string'])? '':'? '. $ _ SERVER ['query _ string'];} return $ url;}/*** output error message */private function error ($ str) {echo''. $ Str .'
';} // Usage: // require_once ('rediscache. php '); // $ cache = new redisCache (10); // you can specify the cache lifetime. // if ($ _ GET ['expacache']) $ cache-> clean (); // else $ cache-> load (); // load the cache, if the cache is valid, the following page code is not executed. // The page code starts. // The page code ends. // $ cache-> write (); // The first run or the cache expires, generate cache?>