Permalink
Browse files

Added support for persistance inside session that can be turned on by…

… starting

the session.save_path INI setting with PERSISTENT=[persistent id].

Added support for memcached server config string
  • Loading branch information...
1 parent 09d5c51 commit f733d2d8059f8b3b84ca83766033103a02684648 @iliaal iliaal committed Jun 13, 2011
Showing with 88 additions and 31 deletions.
  1. +0 −13 php_memcached.c
  2. +7 −0 php_memcached.h
  3. +81 −18 php_memcached_session.c
View
@@ -207,8 +207,6 @@ enum {
MEMC_OP_PREPEND
};
-static int le_memc;
-
static zend_class_entry *memcached_ce = NULL;
static zend_class_entry *memcached_exception_ce = NULL;
@@ -299,7 +297,6 @@ PHP_INI_END()
/****************************************
Forward declarations
****************************************/
-static int php_memc_list_entry(void);
static int php_memc_handle_error(php_memc_t *i_obj, memcached_return status TSRMLS_DC);
static char *php_memc_zval_to_payload(zval *value, size_t *payload_len, uint32_t *flags, enum memcached_serializer serializer, enum memcached_compression_type compression_type TSRMLS_DC);
static int php_memc_zval_from_payload(zval *value, char *payload, size_t payload_len, uint32_t flags, enum memcached_serializer serializer TSRMLS_DC);
@@ -386,11 +383,6 @@ static PHP_METHOD(Memcached, __construct)
plist_key_len = spprintf(&plist_key, 0, "memcached:id=%s", persistent_id);
plist_key_len += 1;
- if (plist_key == NULL) {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "out of memory: cannot allocate persistent list handler");
- /* not reached */
- }
-
if (zend_hash_find(&EG(persistent_list), plist_key, plist_key_len, (void *)&le) == SUCCESS) {
if (le->type == php_memc_list_entry()) {
m_obj = (struct memc_obj *) le->ptr;
@@ -2900,11 +2892,6 @@ static int php_memc_zval_from_payload(zval *value, char *payload, size_t payload
return -1;
}
-static int php_memc_list_entry(void)
-{
- return le_memc;
-}
-
static void php_memc_init_globals(zend_php_memcached_globals *php_memcached_globals_p TSRMLS_DC)
{
#ifdef HAVE_MEMCACHED_SESSION
View
@@ -98,6 +98,13 @@ PHP_MINFO_FUNCTION(memcached);
#define MEMC_G(v) (php_memcached_globals.v)
#endif
+static int le_memc;
+
+static int php_memc_list_entry(void)
+{
+ return le_memc;
+}
+
#endif /* PHP_MEMCACHED_H */
@@ -99,35 +99,98 @@ static void php_memc_sess_unlock(memcached_st *memc TSRMLS_DC)
PS_OPEN_FUNC(memcached)
{
memcached_st *memc_sess = PS_GET_MOD_DATA();
- memcached_server_st *servers;
memcached_return status;
+ char *p, *plist_key = NULL;
+ int plist_key_len;
- servers = memcached_servers_parse((char *)save_path);
- if (servers) {
- memc_sess = memcached_create(NULL);
- if (memc_sess) {
- status = memcached_server_push(memc_sess, servers);
- memcached_server_list_free(servers);
-
- if (memcached_callback_set(memc_sess, MEMCACHED_CALLBACK_PREFIX_KEY, MEMC_G(sess_prefix)) != MEMCACHED_SUCCESS) {
- PS_SET_MOD_DATA(NULL);
- memcached_free(memc_sess);
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "bad memcached key prefix in memcached.sess_prefix");
- return FAILURE;
- }
+ if (!strncmp((char *)save_path, "PERSISTENT=", sizeof("PERSISTENT=") - 1)) {
+ zend_rsrc_list_entry *le = NULL;
+ char *e;
- if (status == MEMCACHED_SUCCESS) {
+ p = (char *)save_path + sizeof("PERSISTENT=") - 1;
+ if (!*p) {
+error:
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid persistent id for session storage");
+ return FAILURE;
+ }
+ if ((e = strchr(p, ' '))) {
+ plist_key_len = spprintf(&plist_key, 0, "memcached_sessions:id=%.*s", (int)(e - p), p);
+ } else {
+ goto error;
+ }
+ 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;
PS_SET_MOD_DATA(memc_sess);
return SUCCESS;
}
+ }
+ p = e + 1;
+ } else {
+ p = (char *)save_path;
+ }
+
+ 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);
+ memcached_server_list_free(servers);
+
+ if (memcached_callback_set(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);
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "bad memcached key prefix in memcached.sess_prefix");
+ return FAILURE;
+ }
+
+ if (status == MEMCACHED_SUCCESS) {
+ goto success;
+ }
+ } else {
+ memcached_server_list_free(servers);
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not allocate libmemcached structure");
+ }
} else {
- memcached_server_list_free(servers);
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not allocate libmemcached structure");
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to parse session.save_path");
}
} else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to parse session.save_path");
+ memc_sess = memcached(p, strlen(p));
+ if (!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);
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to initialize memcached session storage");
+ }
+ } else {
+success:
+ PS_SET_MOD_DATA(memc_sess);
+
+ if (plist_key) {
+ zend_rsrc_list_entry le;
+
+ le.type = php_memc_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) {
+ efree(plist_key);
+ php_error_docref(NULL TSRMLS_CC, E_ERROR, "could not register persistent entry");
+ }
+ efree(plist_key);
+ }
+ return SUCCESS;
+ }
}
+ if (plist_key) {
+ efree(plist_key);
+ }
PS_SET_MOD_DATA(NULL);
return FAILURE;
}

0 comments on commit f733d2d

Please sign in to comment.