Php + redis Cache
Php + redis Cache
-
- Class redisCache {
- /**
- * $ Host: ip address of the redis server
- * $ Port: redis server port
- * $ Lifetime: cache file validity period, in seconds
- * $ Cacheid: cache file path, including file name
- */
- Private $ host;
- Private $ port;
- Private $ lifetime;
- Private $ cacheid;
- Private $ data;
- Public $ redis;
- /**
- * Destructor: checks whether the cache directory is valid. the default value is assigned.
- */
- Function _ construct ($ lifetime = 1800 ){
- // Configure
- $ This-> host = "127.0.0.1 ";
- $ This-> port = "6379 ";
- $ Redis = new Redis ();
- $ Redis-> pconnect ($ this-> host, $ this-> port );
- $ 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. the page content is obtained 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 () stops the execution of the original page program after loading the cache. if the cache is invalid, run the original page program to generate the cache.
- * Ob_start () enables browser caching to retrieve 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 ('cache cleanup failed! ');
- }
- }
- /**
- * Obtain the cache file path
- */
- Private function getcacheid (){
- Return $ this-> dir. md5 ($ this-> geturl (). $ this-> ext;
- }
- /**
- * Obtain 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); // sets the cache lifetime.
- // If ($ _ GET ['Your ache']) $ cache-> clean ();
- // Else $ cache-> load (); // load the cache. if the cache is valid, the following page code is not executed.
- /// Page code starts
-
- /// The page code ends.
- // $ Cache-> write (); // The cache is generated when the first operation or cache expires.
-
-
-
- ?>
|
Php, redis
This topic was moved by Beckham