About the definition and use of Redis cache classes in PHP

Source: Internet
Author: User
Tags redis server
This article describes the implementation of the Redis cache class definition and use of PHP. Share to everyone for your reference, as follows:

Php+redis Cache Class


<?phpclass Rediscache {/** * $host: Redis server IP * $port: Redis Server port * $lifetime: Cache file validity, in seconds * $cacheid: Cache file  Path, including file name */private $host;  Private $port;  Private $lifetime;  Private $cacheid;  Private $data;  Public $redis;    /** * destructor, check whether the cache directory is valid, default assignment */function __construct ($lifetime =1800) {//config $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->hmget ($this->cacheid, Array (' content ', ' creattime '));    Print_r ($this->redis);  Print_r ($this->data);    }/** * Check that 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 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 () load cache to terminate the execution of the original page program, cache invalid run the original page program generate cache * Ob_start () Open 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 (' Purge cache failed! ');  }}/** * Get cache file path */Private Function Getcacheid () {return $this->dir.md5 ($this->geturl ()). $this->ext; }  /**  * Gets the current page full URL */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 ' <p style= "color:red;"  > '. $str. ' </p> '; }}//usage://require_once (' rediscache.php ');//$cache = new Rediscache (10); Set the cache lifetime//if ($_get[' ClearCache ']) $cache->clean ();/Else $cache->load (); Load cache, cache valid then do not execute the following page code////page code start///page code end//$cache->write (); First run or cache expiration, generate cache?>
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.