Php+redis Cache Class
-
- Class 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) {
- Configuration
- $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 if 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 () to terminate the execution of the original page program after loading the cache, and run the original page program generation cache if the cache is invalid
- * 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;
- }
- /**
- * Get the full 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); Set Cache lifetime
- if ($_get[' ClearCache ')) $cache->clean ();
- else $cache->load (); Load cache, cache is valid do not execute the following page code
- Page code Start
- Page code End
- $cache->write (); First run or cache expiration, generating cache
- ?>
Copy Code
|
PHP, Redis This topic was moved by Beckham on 2015-11-12 08:43