Skip to content

Instantly share code, notes, and snippets.

@shollingsworth
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shollingsworth/de0049b0c57961df1703 to your computer and use it in GitHub Desktop.
Save shollingsworth/de0049b0c57961df1703 to your computer and use it in GitHub Desktop.
initial Redis Thinger (RedCache)
<?php
/**
* RedCache : Redis Cacher Interface
* @version 2014-10-20 13:16
* @author Steven Hollingsworth <steven.hollingsworth@fresno.gov>
*/
/** 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 <ubuntu-devel-discuss@lists.ubuntu.com>
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 <pkg-php-pecl@lists.alioth.debian.org>
Homepage: http://pecl.php.net/package/redis
*/
/**
* RedCache : Redis Cacher Interface
* @author Steven Hollingsworth <steven.hollingsworth@fresno.gov>
* @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 <steven.hollingsworth@fresno.gov>
* @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 <steven.hollingsworth@fresno.gov>
* @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 <steven.hollingsworth@fresno.gov>
* @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 <steven.hollingsworth@fresno.gov>
* @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 <steven.hollingsworth@fresno.gov>
*/
public static function info() {
return self::Redis()->info();
}
/**
* Get Redis Interface
* @version 2014-10-20 13:28
* @author Steven Hollingsworth <steven.hollingsworth@fresno.gov>
* @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