Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/phpredis/phpredis.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormichael-grunder <michael.grunder@gmail.com>2013-03-27 21:21:18 +0400
committermichael-grunder <michael.grunder@gmail.com>2013-03-27 21:21:18 +0400
commitef792320e7a5acbb7361ab4dfcb9624c115b7e39 (patch)
tree481464ba1e55555ecde7f616b60526f184fba102
parentb2ffb7ac753f5fa94d808dbf684a64e9f0e6f8c5 (diff)
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
-rw-r--r--README.markdown86
-rw-r--r--common.h1
-rw-r--r--library.c6
-rw-r--r--php_redis.h9
-rw-r--r--redis.c152
-rw-r--r--tests/TestRedis.php7
6 files changed, 261 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index bd5a067b..bb403d2f 100644
--- a/README.markdown
+++ b/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
diff --git a/common.h b/common.h
index 1b9e97f5..33d60ce6 100644
--- a/common.h
+++ b/common.h
@@ -157,6 +157,7 @@ typedef struct {
php_stream *stream;
char *host;
short port;
+ char *auth;
double timeout;
double read_timeout;
int failed;
diff --git a/library.c b/library.c
index 856a8055..32f77cd6 100644
--- a/library.c
+++ b/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);
}
diff --git a/php_redis.h b/php_redis.h
index 24f00215..6ed4babe 100644
--- a/php_redis.h
+++ b/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
diff --git a/redis.c b/redis.c
index bf92513e..d274e678 100644
--- a/redis.c
+++ b/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: */
diff --git a/tests/TestRedis.php b/tests/TestRedis.php
index d4b512ef..db9099ba 100644
--- a/tests/TestRedis.php
+++ b/tests/TestRedis.php
@@ -4301,6 +4301,13 @@ class Redis_Test extends TestSuite
$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"));