Last active
August 29, 2015 14:07
-
-
Save shollingsworth/de0049b0c57961df1703 to your computer and use it in GitHub Desktop.
initial Redis Thinger (RedCache)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* RedCache : Redis Cacher Interface | |
* @version 2014-10-20 13:16 | |
* @author Steven Hollingsworth <[email protected]> | |
*/ | |
/** Set Constants and default path for all includes */ | |
if(!@include_once("{$_SERVER['PHP_REPORT_BASE_DIR']}/include/_constants.php")) { print "Could not Load standard include file. Bailing...\n<br>"; exit(99); } | |
/* | |
Requires Redis Server and php5-redis package | |
Package: php5-redis | |
Status: install ok installed | |
Priority: optional | |
Section: php | |
Installed-Size: 351 | |
Maintainer: Ubuntu Developers <[email protected]> | |
Architecture: amd64 | |
Source: php-redis | |
Version: 2.2.4-1build2 | |
Depends: libc6 (>= 2.14), phpapi-20121212 | |
Suggests: redis-server | |
Description: PHP extension for interfacing with Redis | |
This extension allows php applications to communicate with the Redis | |
persistent key-value store. The php-redis module provides an easy object | |
oriented interface. | |
Original-Maintainer: Debian PHP PECL Maintainers <[email protected]> | |
Homepage: http://pecl.php.net/package/redis | |
*/ | |
/** | |
* RedCache : Redis Cacher Interface | |
* @author Steven Hollingsworth <[email protected]> | |
* @package Redis | |
* @version 2014-10-20 13:16 | |
*/ | |
class RedCache { | |
/** | |
* @const KEYSEP KEY Separator String | |
*/ | |
const KEYSEP = "\x02:"; //chr(2) . ":" | |
const KEYFLAT = ":"; //used when we flatten a keyid | |
/** Instance variable defaults to null, when null a new one will be created */ | |
protected static $instance = null; | |
/** | |
* Get Instance of self, if exists, otherwise create one | |
* @version 2014-10-20 13:23 | |
* @author Steven Hollingsworth <[email protected]> | |
* @return RedCache | |
*/ | |
protected static function getInstance() { | |
if (is_null(self::$instance)) { self::$instance = new self; } | |
return self::$instance; | |
} | |
/** redis | |
* @var Redis Redis Class Object | |
*/ | |
protected $redis ; | |
/** | |
* Consructor | |
*/ | |
protected function __construct() { | |
$this->redis = new Redis(); | |
$this->redis->connect('localhost',6379,'60'); | |
} | |
/** | |
* Generate Redis Key based on prefix, middle (key) , and end $keyid | |
* @version 2014-10-20 18:06 | |
* @author Steven Hollingsworth <[email protected]> | |
* @param string $prefix | |
* @param string $key | |
* @param string $keyid | |
*/ | |
public static function genKey($prefix,$key,$keyid=NULL) { | |
if(is_array($key)) { | |
$key = implode(self::KEYSEP,$key); | |
} | |
if(is_null($keyid)) { | |
return implode(self::KEYSEP,array($prefix,$key)); | |
} else { | |
return implode(self::KEYSEP,array($prefix,$key,$keyid)); | |
} | |
} | |
/** | |
* Get Last part of Key based on | |
* @version 2014-10-20 18:50 | |
* @author Steven Hollingsworth <[email protected]> | |
* @param string $key description | |
*/ | |
public static function getKeyID($key) { | |
$arr = explode(self::KEYSEP,$key,3); | |
$tmp = array_pop($arr); | |
return implode(self::KEYFLAT,explode(self::KEYSEP,$tmp)); | |
} | |
/** | |
* Search Redis Keyspace | |
* @version 2014-10-20 18:42 | |
* @author Steven Hollingsworth <[email protected]> | |
* @param string $prefix | |
* @param string $key | |
* @param string $keyid | |
*/ | |
public static function keySpace($prefix,$key) { | |
$key = self::genKey($prefix,$key); | |
return self::Redis()->keys($key . '*'); | |
} | |
/** | |
* Get Info | |
* @version 2014-10-20 13:28 | |
* @author Steven Hollingsworth <[email protected]> | |
*/ | |
public static function info() { | |
return self::Redis()->info(); | |
} | |
/** | |
* Get Redis Interface | |
* @version 2014-10-20 13:28 | |
* @author Steven Hollingsworth <[email protected]> | |
* @return Redis | |
*/ | |
public static function Redis() { | |
$r = self::getInstance()->redis; | |
return $r; | |
} | |
} | |
// Note that it is a good practice to NOT end your PHP files with a closing PHP tag. | |
// This prevents trailing newlines on the file from being included in your output, | |
// which can cause problems with redirecting users. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment