Skip to content
Browse files

Finalize persistent session handling

  • Loading branch information...
1 parent f733d2d commit 4c3045fbfe9ef0fd854128fe622d45fa30a4422d @iliaal iliaal committed Jun 13, 2011
Showing with 66 additions and 33 deletions.
  1. +31 −4 php_memcached.c
  2. +5 −6 php_memcached.h
  3. +30 −23 php_memcached_session.c
View
35 php_memcached.c
@@ -350,6 +350,13 @@ static zend_bool php_memcached_on_new_callback(zval *object, zend_fcall_info *fc
return 1;
}
+static int le_memc, le_memc_sess;
+
+static int php_memc_list_entry(void)
+{
+ return le_memc;
+}
+
/* {{{ Memcached::__construct([string persistent_id[, callback on_new]]))
Creates a Memcached object, optionally using persistent memcache connection */
static PHP_METHOD(Memcached, __construct)
@@ -2444,6 +2451,16 @@ ZEND_RSRC_DTOR_FUNC(php_memc_dtor)
rsrc->ptr = NULL;
}
}
+
+ZEND_RSRC_DTOR_FUNC(php_memc_sess_dtor)
+{
+ if (rsrc->ptr) {
+ memcached_sess *memc_sess = (memcached_sess *)rsrc->ptr;
+ memcached_free(memc_sess->memc_sess);
+ pefree(rsrc->ptr, 1);
+ rsrc->ptr = NULL;
+ }
+}
/* }}} */
/* {{{ internal API functions */
@@ -3447,8 +3464,13 @@ zend_module_entry memcached_module_entry = {
NULL,
PHP_MINIT(memcached),
PHP_MSHUTDOWN(memcached),
+#if HAVE_MEMCACHED_SASL
PHP_RINIT(memcached),
PHP_RSHUTDOWN(memcached),
+#else
+ NULL,
+ NULL,
+#endif
PHP_MINFO(memcached),
PHP_MEMCACHED_VERSION,
STANDARD_MODULE_PROPERTIES
@@ -3619,27 +3641,31 @@ static void php_memc_register_constants(INIT_FUNC_ARGS)
}
/* }}} */
+#if HAVE_MEMCACHED_SASL
PHP_RINIT_FUNCTION(memcached)
{
-#if HAVE_MEMCACHED_SASL
if (MEMC_G(use_sasl)) {
if (sasl_client_init(NULL) != SASL_OK) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to initialize SASL library");
}
}
-#endif
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(memcached)
{
-#if HAVE_MEMCACHED_SASL
if (MEMC_G(use_sasl)) {
sasl_done();
}
-#endif
+
return SUCCESS;
}
+#endif
+
+int php_memc_sess_list_entry(void)
+{
+ return le_memc_sess;
+}
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(memcached)
@@ -3650,6 +3676,7 @@ PHP_MINIT_FUNCTION(memcached)
memcached_object_handlers.clone_obj = NULL;
le_memc = zend_register_list_destructors_ex(NULL, php_memc_dtor, "Memcached persistent connection", module_number);
+ le_memc_sess = zend_register_list_destructors_ex(NULL, php_memc_sess_dtor, "Memcached Sessions persistent connection", module_number);
INIT_CLASS_ENTRY(ce, "Memcached", memcached_class_methods);
memcached_ce = zend_register_internal_class(&ce TSRMLS_CC);
View
11 php_memcached.h
@@ -98,16 +98,15 @@ PHP_MINFO_FUNCTION(memcached);
#define MEMC_G(v) (php_memcached_globals.v)
#endif
-static int le_memc;
+typedef struct {
+ memcached_st *memc_sess;
+ zend_bool is_persisent;
+} memcached_sess;
-static int php_memc_list_entry(void)
-{
- return le_memc;
-}
+int php_memc_sess_list_entry(void);
#endif /* PHP_MEMCACHED_H */
-
/*
* Local variables:
* tab-width: 4
View
53 php_memcached_session.c
@@ -98,7 +98,7 @@ static void php_memc_sess_unlock(memcached_st *memc TSRMLS_DC)
PS_OPEN_FUNC(memcached)
{
- memcached_st *memc_sess = PS_GET_MOD_DATA();
+ memcached_sess *memc_sess = PS_GET_MOD_DATA();
memcached_return status;
char *p, *plist_key = NULL;
int plist_key_len;
@@ -120,31 +120,35 @@ PS_OPEN_FUNC(memcached)
}
plist_key_len++;
if (zend_hash_find(&EG(persistent_list), plist_key, plist_key_len, (void *)&le) == SUCCESS) {
- if (le->type == php_memc_list_entry()) {
- memc_sess = (struct memc_obj *) le->ptr;
+ if (le->type == php_memc_sess_list_entry()) {
+ memc_sess = (struct memcached_sess *) le->ptr;
PS_SET_MOD_DATA(memc_sess);
return SUCCESS;
}
}
p = e + 1;
+ memc_sess = pecalloc(sizeof(*memc_sess), 1, 1);
+ memc_sess->is_persisent = 1;
} else {
p = (char *)save_path;
+ memc_sess = ecalloc(sizeof(*memc_sess), 1);
+ memc_sess->is_persisent = 0;
}
if (!strstr(p, "--SERVER")) {
memcached_server_st *servers = memcached_servers_parse(p);
if (servers) {
- memc_sess = memcached_create(NULL);
- if (memc_sess) {
- status = memcached_server_push(memc_sess, servers);
+ memc_sess->memc_sess = memcached_create(NULL);
+ if (memc_sess->memc_sess) {
+ status = memcached_server_push(memc_sess->memc_sess, servers);
memcached_server_list_free(servers);
- if (memcached_callback_set(memc_sess, MEMCACHED_CALLBACK_PREFIX_KEY, MEMC_G(sess_prefix)) != MEMCACHED_SUCCESS) {
+ if (memcached_callback_set(memc_sess->memc_sess, MEMCACHED_CALLBACK_PREFIX_KEY, MEMC_G(sess_prefix)) != MEMCACHED_SUCCESS) {
PS_SET_MOD_DATA(NULL);
if (plist_key) {
efree(plist_key);
}
- memcached_free(memc_sess);
+ memcached_free(memc_sess->memc_sess);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "bad memcached key prefix in memcached.sess_prefix");
return FAILURE;
}
@@ -160,8 +164,8 @@ PS_OPEN_FUNC(memcached)
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to parse session.save_path");
}
} else {
- memc_sess = memcached(p, strlen(p));
- if (!memc_sess) {
+ memc_sess->memc_sess = memcached(p, strlen(p));
+ if (!memc_sess->memc_sess) {
char error_buffer[1024];
if (libmemcached_check_configuration(p, strlen(p), error_buffer, sizeof(error_buffer)) != MEMCACHED_SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "session.save_path configuration error %s", error_buffer);
@@ -175,7 +179,7 @@ PS_OPEN_FUNC(memcached)
if (plist_key) {
zend_rsrc_list_entry le;
- le.type = php_memc_list_entry();
+ le.type = php_memc_sess_list_entry();
le.ptr = memc_sess;
if (zend_hash_update(&EG(persistent_list), (char *)plist_key, plist_key_len, (void *)&le, sizeof(le), NULL) == FAILURE) {
@@ -197,13 +201,16 @@ PS_OPEN_FUNC(memcached)
PS_CLOSE_FUNC(memcached)
{
- memcached_st *memc_sess = PS_GET_MOD_DATA();
+ memcached_sess *memc_sess = PS_GET_MOD_DATA();
if (MEMC_G(sess_locking_enabled)) {
- php_memc_sess_unlock(memc_sess TSRMLS_CC);
+ php_memc_sess_unlock(memc_sess->memc_sess TSRMLS_CC);
}
- if (memc_sess) {
- memcached_free(memc_sess);
+ if (memc_sess->memc_sess) {
+ if (!memc_sess->is_persisent) {
+ memcached_free(memc_sess->memc_sess);
+ efree(memc_sess);
+ }
PS_SET_MOD_DATA(NULL);
}
@@ -217,7 +224,7 @@ PS_READ_FUNC(memcached)
int key_len = strlen(key);
uint32_t flags = 0;
memcached_return status;
- memcached_st *memc_sess = PS_GET_MOD_DATA();
+ memcached_sess *memc_sess = PS_GET_MOD_DATA();
size_t key_length;
key_length = strlen(MEMC_G(sess_prefix)) + key_len + 5; // prefix + "lock."
@@ -228,12 +235,12 @@ PS_READ_FUNC(memcached)
}
if (MEMC_G(sess_locking_enabled)) {
- if (php_memc_sess_lock(memc_sess, key TSRMLS_CC) < 0) {
+ if (php_memc_sess_lock(memc_sess->memc_sess, key TSRMLS_CC) < 0) {
return FAILURE;
}
}
- payload = memcached_get(memc_sess, key, key_len, &payload_len, &flags, &status);
+ payload = memcached_get(memc_sess->memc_sess, key, key_len, &payload_len, &flags, &status);
if (status == MEMCACHED_SUCCESS) {
*val = estrndup(payload, payload_len);
@@ -250,7 +257,7 @@ PS_WRITE_FUNC(memcached)
int key_len = strlen(key);
time_t expiration = 0;
memcached_return status;
- memcached_st *memc_sess = PS_GET_MOD_DATA();
+ memcached_sess *memc_sess = PS_GET_MOD_DATA();
size_t key_length;
key_length = strlen(MEMC_G(sess_prefix)) + key_len + 5; // prefix + "lock."
@@ -263,7 +270,7 @@ PS_WRITE_FUNC(memcached)
if (PS(gc_maxlifetime) > 0) {
expiration = PS(gc_maxlifetime);
}
- status = memcached_set(memc_sess, key, key_len, val, vallen, expiration, 0);
+ status = memcached_set(memc_sess->memc_sess, key, key_len, val, vallen, expiration, 0);
if (status == MEMCACHED_SUCCESS) {
return SUCCESS;
@@ -274,11 +281,11 @@ PS_WRITE_FUNC(memcached)
PS_DESTROY_FUNC(memcached)
{
- memcached_st *memc_sess = PS_GET_MOD_DATA();
+ memcached_sess *memc_sess = PS_GET_MOD_DATA();
- memcached_delete(memc_sess, key, strlen(key), 0);
+ memcached_delete(memc_sess->memc_sess, key, strlen(key), 0);
if (MEMC_G(sess_locking_enabled)) {
- php_memc_sess_unlock(memc_sess TSRMLS_CC);
+ php_memc_sess_unlock(memc_sess->memc_sess TSRMLS_CC);
}
return SUCCESS;

0 comments on commit 4c3045f

Please sign in to comment.
Something went wrong with that request. Please try again.