Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Introspection methods

This commit adds methods to get information about the state
of our phpredis object, such as what host/port we are connected
to, our timeout, etc...

The following methods have been added:

getHost()
getPort()
getDBNum()
getTimeout()
getReadTimeout()
isConnected()
getPersistentID()
getAuth()

In addition, there is a small memory leak fix when a persistent id
was specifically passed to connect() (it wasn't beeing freed).

Addresses issue #320
  • Loading branch information...
commit ef792320e7a5acbb7361ab4dfcb9624c115b7e39 1 parent b2ffb7a
@michael-grunder michael-grunder authored
View
86 README.markdown
@@ -26,6 +26,7 @@ You can send comments, patches, questions [here on github](https://github.com/ni
* [Pub/sub](#pubsub)
* [Transactions](#transactions)
* [Scripting](#scripting)
+ * [Introspection](#introspection)
-----
@@ -2958,3 +2959,88 @@ serializing values, and you return something from redis in a LUA script that is
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
$redis->_unserialize('a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}'); // Will return Array(1,2,3)
~~~
+
+
+
+## Introspection
+
+### IsConnected
+-----
+_**Description**_: A method to determine if a phpredis object thinks it's connected to a server
+
+##### *Parameters*
+None
+
+##### *Return value*
+*Boolean* Returns TRUE if phpredis thinks it's connected and FALSE if not
+
+### GetHost
+-----
+_**Description**_: Retreive our host or unix socket that we're connected to
+
+##### *Parameters*
+None
+
+##### *Return value*
+*Mixed* The host or unix socket we're connected to or FALSE if we're not connected
+
+
+### GetPort
+-----
+_**Description**_: Get the port we're connected to
+
+##### *Parameters*
+None
+
+##### *Return value*
+*Mixed* Returns the port we're connected to or FALSE if we're not connected
+
+### getDBNum
+-----
+_**Description**_: Get the database number phpredis is pointed to
+
+##### *Parameters*
+None
+
+##### *Return value*
+*Mixed* Returns the database number (LONG) phpredis thinks it's pointing to or FALSE if we're not connected
+
+### GetTimeout
+-----
+_**Description**_: Get the (write) timeout in use for phpreids
+
+##### *Parameters*
+None
+
+##### *Return value*
+*Mixed* The timeout (DOUBLE) specified in our connect call or FALSE if we're not connected
+
+### GetReadTimeout
+_**Description**_: Get the read timeout specified to phpredis or FALSE if we're not connected
+
+##### *Parameters*
+None
+
+##### *Return value*
+*Mixed* Returns the read timeout (which can be set using setOption and Redis::OPT_READ_TIMOUT) or FALSE if we're not connected
+
+### GetPersistentID
+-----
+_**Description**_: Gets the persistent ID that phpredis is using
+
+##### *Parameters*
+None
+
+##### *Return value*
+*Mixed* Returns the persistent id phpredis is using (which will only be set if connected with pconnect), NULL if we're not
+using a persistent ID, and FALSE if we're not connected
+
+### GetAuth
+-----
+_**Description**_: Get the password used to authenticate the phpredis connection
+
+### *Parameters*
+None
+
+### *Return value*
+*Mixed* Returns the password used to authenticate a phpredis session or NULL if none was used, and FALSE if we're not connected
View
1  common.h
@@ -157,6 +157,7 @@ typedef struct {
php_stream *stream;
char *host;
short port;
+ char *auth;
double timeout;
double read_timeout;
int failed;
View
6 library.c
@@ -1283,6 +1283,12 @@ PHPAPI void redis_free_socket(RedisSock *redis_sock)
if(redis_sock->err) {
efree(redis_sock->err);
}
+ if(redis_sock->auth) {
+ efree(redis_sock->auth);
+ }
+ if(redis_sock->persistent_id) {
+ efree(redis_sock->persistent_id);
+ }
efree(redis_sock->host);
efree(redis_sock);
}
View
9 php_redis.h
@@ -181,6 +181,15 @@ PHP_METHOD(Redis, setOption);
PHP_METHOD(Redis, config);
+PHP_METHOD(Redis, getHost);
+PHP_METHOD(Redis, getPort);
+PHP_METHOD(Redis, getDBNum);
+PHP_METHOD(Redis, getTimeout);
+PHP_METHOD(Redis, getReadTimeout);
+PHP_METHOD(Redis, isConnected);
+PHP_METHOD(Redis, getPersistentID);
+PHP_METHOD(Redis, getAuth);
+
#ifdef PHP_WIN32
#define PHP_REDIS_API __declspec(dllexport)
#else
View
152 redis.c
@@ -235,6 +235,16 @@ static zend_function_entry redis_functions[] = {
/* config */
PHP_ME(Redis, config, NULL, ZEND_ACC_PUBLIC)
+ /* introspection */
+ PHP_ME(Redis, getHost, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(Redis, getPort, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(Redis, getDBNum, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(Redis, getTimeout, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(Redis, getReadTimeout, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(Redis, getPersistentID, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(Redis, getAuth, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(Redis, isConnected, NULL, ZEND_ACC_PUBLIC)
+
/* aliases */
PHP_MALIAS(Redis, open, connect, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(Redis, popen, pconnect, NULL, ZEND_ACC_PUBLIC)
@@ -385,6 +395,26 @@ PHPAPI int redis_sock_get(zval *id, RedisSock **redis_sock TSRMLS_DC, int no_thr
}
/**
+ * redis_sock_get_direct
+ * Returns our attached RedisSock pointer if we're connected
+ */
+PHPAPI RedisSock *redis_sock_get_connected(INTERNAL_FUNCTION_PARAMETERS TSRMLS_DC) {
+ zval *object;
+ RedisSock *redis_sock;
+
+ // If we can't grab our object, or get a socket, or we're not connected, return NULL
+ if((zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, redis_ce) == FAILURE) ||
+ (redis_sock_get(object, &redis_sock TSRMLS_CC, 1) < 0) || redis_sock->status != REDIS_SOCK_STATUS_CONNECTED)
+ {
+ return NULL;
+ }
+
+ // Return our socket
+ return redis_sock;
+}
+
+
+/**
* PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(redis)
@@ -3289,6 +3319,10 @@ PHP_METHOD(Redis, auth) {
cmd_len = redis_cmd_format_static(&cmd, "AUTH", "s", password, password_len);
+ // Free previously stored auth if we have one, and store this password
+ if(redis_sock->auth) efree(redis_sock->auth);
+ redis_sock->auth = estrndup(password, password_len);
+
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
IF_ATOMIC() {
redis_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL, NULL);
@@ -6304,5 +6338,123 @@ PHP_METHOD(Redis, time) {
REDIS_PROCESS_RESPONSE(redis_sock_read_multibulk_reply_raw);
}
+/*
+ * Introspection stuff
+ */
+
+/*
+ * {{{ proto Redis::IsConnected
+ */
+PHP_METHOD(Redis, isConnected) {
+ RedisSock *redis_sock;
+
+ if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
+ RETURN_TRUE;
+ } else {
+ RETURN_FALSE;
+ }
+}
+
+/*
+ * {{{ proto Redis::getHost()
+ */
+PHP_METHOD(Redis, getHost) {
+ RedisSock *redis_sock;
+
+ if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
+ RETURN_STRING(redis_sock->host, 1);
+ } else {
+ RETURN_FALSE;
+ }
+}
+
+/*
+ * {{{ proto Redis::getPort()
+ */
+PHP_METHOD(Redis, getPort) {
+ RedisSock *redis_sock;
+
+ if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
+ // Return our port
+ RETURN_LONG(redis_sock->port);
+ } else {
+ RETURN_FALSE;
+ }
+}
+
+/*
+ * {{{ proto Redis::getDBNum
+ */
+PHP_METHOD(Redis, getDBNum) {
+ RedisSock *redis_sock;
+
+ if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
+ // Return our db number
+ RETURN_LONG(redis_sock->dbNumber);
+ } else {
+ RETURN_FALSE;
+ }
+}
+
+/*
+ * {{{ proto Redis::getTimeout
+ */
+PHP_METHOD(Redis, getTimeout) {
+ RedisSock *redis_sock;
+
+ if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
+ RETURN_DOUBLE(redis_sock->timeout);
+ } else {
+ RETURN_FALSE;
+ }
+}
+
+/*
+ * {{{ proto Redis::getReadTimeout
+ */
+PHP_METHOD(Redis, getReadTimeout) {
+ RedisSock *redis_sock;
+
+ if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
+ RETURN_DOUBLE(redis_sock->read_timeout);
+ } else {
+ RETURN_FALSE;
+ }
+}
+
+/*
+ * {{{ proto Redis::getPersistentID
+ */
+PHP_METHOD(Redis, getPersistentID) {
+ RedisSock *redis_sock;
+
+ if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
+ if(redis_sock->persistent_id != NULL) {
+ RETURN_STRING(redis_sock->persistent_id, 1);
+ } else {
+ RETURN_NULL();
+ }
+ } else {
+ RETURN_FALSE;
+ }
+}
+
+/*
+ * {{{ proto Redis::getAuth
+ */
+PHP_METHOD(Redis, getAuth) {
+ RedisSock *redis_sock;
+
+ if((redis_sock = redis_sock_get_connected(INTERNAL_FUNCTION_PARAM_PASSTHRU TSRMLS_CC))) {
+ if(redis_sock->auth != NULL) {
+ RETURN_STRING(redis_sock->auth, 1);
+ } else {
+ RETURN_NULL();
+ }
+ } else {
+ RETURN_FALSE;
+ }
+}
+
/* vim: set tabstop=4 softtabstop=4 noexpandtab shiftwidth=4: */
View
7 tests/TestRedis.php
@@ -4301,6 +4301,13 @@ public function testReadTimeoutOption() {
$this->assertEquals(12.3, $this->redis->getOption(Redis::OPT_READ_TIMEOUT));
}
+ public function testIntrospection() {
+ // Simple introspection tests
+ $this->assertTrue($this->redis->getHost() === self::HOST);
+ $this->assertTrue($this->redis->getPort() === self::PORT);
+ $this->assertTrue($this->redis->getAuth() === self::AUTH);
+ }
+
}
exit(TestSuite::run("Redis_Test"));
Please sign in to comment.
Something went wrong with that request. Please try again.