Skip to content

Commit 7cb0c48

Browse files
committed
Fixes to session
1 parent 7ac4e83 commit 7cb0c48

File tree

4 files changed

+62
-23
lines changed

4 files changed

+62
-23
lines changed

config.m4

+22
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,28 @@ if test "$PHP_MEMCACHED" != "no"; then
296296
AC_MSG_RESULT([no])
297297
fi
298298

299+
ORIG_CFLAGS="$CFLAGS"
300+
ORIG_LIBS="$LIBS"
301+
302+
CFLAGS="$CFLAGS $PHP_LIBMEMCACHED_INCLUDES"
303+
LIBS="$LIBS $PHP_LIBMEMCACHED_LIBS"
304+
305+
AC_CACHE_CHECK([whether memcached_exist is defined], ac_cv_have_memcached_exist, [
306+
AC_TRY_LINK(
307+
[ #include <libmemcached/memcached.h> ],
308+
[ memcached_exist (NULL, NULL, 0); ],
309+
[ ac_cv_have_memcached_exist="yes" ],
310+
[ ac_cv_have_memcached_exist="no" ]
311+
)
312+
])
313+
314+
CFLAGS="$ORIG_CFLAGS"
315+
LIBS="$ORIG_LIBS"
316+
317+
if test "$ac_cv_have_memcached_exist" = "yes"; then
318+
AC_DEFINE(HAVE_MEMCACHED_EXIST, [1], [Whether memcached_exist is defined])
319+
fi
320+
299321
PHP_MEMCACHED_FILES="php_memcached.c php_libmemcached_compat.c g_fmt.c"
300322

301323
if test "$PHP_SYSTEM_FASTLZ" != "no"; then

php_memcached.c

+19
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,25 @@ static void php_memc_destroy(struct memc_obj *m_obj, zend_bool persistent);
332332
****************************************/
333333

334334

335+
memcached_return php_memcached_exist (memcached_st *memc, zend_string *key)
336+
{
337+
#ifdef HAVE_MEMCACHED_EXIST
338+
return memcached_exist (memc, key->val, key->len);
339+
#else
340+
memcached_return rc = MEMCACHED_SUCCESS;
341+
uint32_t flags = 0;
342+
size_t value_length = 0;
343+
char *value = NULL;
344+
345+
value = memcached_get (memc, key->val, key->len, &value_length, &flags, &rc);
346+
if (value) {
347+
free (value);
348+
}
349+
return rc;
350+
#endif
351+
}
352+
353+
335354
char *php_memc_printable_func (zend_fcall_info *fci, zend_fcall_info_cache *fci_cache)
336355
{
337356
char *buffer = NULL;

php_memcached_private.h

+2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ int php_memc_sess_list_entry(void);
192192

193193
char *php_memc_printable_func (zend_fcall_info *fci, zend_fcall_info_cache *fci_cache TSRMLS_DC);
194194

195+
memcached_return php_memcached_exist (memcached_st *memc, zend_string *key);
196+
195197
#endif /* PHP_MEMCACHED_PRIVATE_H */
196198

197199
/*

php_memcached_session.c

+19-23
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ PS_OPEN_FUNC(memcached)
277277
PS_CLOSE_FUNC(memcached)
278278
{
279279
memcached_sess *memc_sess = PS_GET_MOD_DATA();
280-
280+
281281
if (!memc_sess) {
282282
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session is not allocated, check session.save_path value");
283283
return FAILURE;
@@ -336,8 +336,9 @@ PS_READ_FUNC(memcached)
336336
*val = zend_string_init(payload, payload_len, 1);
337337
free(payload);
338338
return SUCCESS;
339-
} else if (status = MEMCACHED_NOTFOUND) {
339+
} else if (status == MEMCACHED_NOTFOUND) {
340340
*val = ZSTR_EMPTY_ALLOC();
341+
return SUCCESS;
341342
} else {
342343
return FAILURE;
343344
}
@@ -351,7 +352,7 @@ PS_WRITE_FUNC(memcached)
351352
memcached_return status;
352353
memcached_sess *memc_sess = PS_GET_MOD_DATA();
353354
size_t key_length;
354-
355+
355356
if (!memc_sess) {
356357
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Session is not allocated, check session.save_path value");
357358
return FAILURE;
@@ -409,39 +410,34 @@ PS_GC_FUNC(memcached)
409410
PS_CREATE_SID_FUNC(memcached)
410411
{
411412
zend_string *sid;
412-
int maxfail = 3;
413+
int retries = 3;
413414
memcached_sess *memc_sess = PS_GET_MOD_DATA();
415+
time_t expiration = PS(gc_maxlifetime);
414416

415-
do {
417+
if (!memc_sess) {
418+
return NULL;
419+
}
420+
421+
while (retries-- > 0) {
416422
sid = php_session_create_id((void**)&memc_sess);
417-
if (!sid) {
418-
if (--maxfail < 0) {
419-
return NULL;
420-
} else {
421-
continue;
423+
424+
if (sid) {
425+
if (memcached_add(memc_sess->memc_sess, sid->val, sid->len, "0", 0, expiration, 0) == MEMCACHED_SUCCESS) {
426+
return sid;
422427
}
423-
}
424-
/* Check collision */
425-
/* FIXME: mod_data(memc_sess) should not be NULL (User handler could be NULL) */
426-
if (memc_sess && memcached_exist(memc_sess->memc_sess, sid->val, sid->len) == MEMCACHED_SUCCESS) {
427-
if (sid) {
428+
else {
428429
zend_string_release(sid);
429-
sid = NULL;
430-
}
431-
if (--maxfail < 0) {
432-
return NULL;
433430
}
434431
}
435-
} while(!sid);
436-
437-
return sid;
432+
}
433+
return NULL;
438434
}
439435

440436
PS_VALIDATE_SID_FUNC(memcached)
441437
{
442438
memcached_sess *memc_sess = PS_GET_MOD_DATA();
443439

444-
if (memcached_exist(memc_sess->memc_sess, key->val, key->len) == MEMCACHED_SUCCESS) {
440+
if (php_memcached_exist(memc_sess->memc_sess, key) == MEMCACHED_SUCCESS) {
445441
return SUCCESS;
446442
} else {
447443
return FAILURE;

0 commit comments

Comments
 (0)