Skip to content

Commit 0dae7f2

Browse files
committed
Merge pull request #164 from yohgaki/php7
Use new session save handler
2 parents 4422e83 + bd32183 commit 0dae7f2

File tree

4 files changed

+159
-4
lines changed

4 files changed

+159
-4
lines changed

php_memcached_session.c

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern ZEND_DECLARE_MODULE_GLOBALS(php_memcached)
2424
#define MEMC_SESS_LOCK_EXPIRATION 30
2525

2626
ps_module ps_mod_memcached = {
27-
PS_MOD(memcached)
27+
PS_MOD_UPDATE_TIMESTAMP(memcached)
2828
};
2929

3030
static int php_memc_sess_lock(memcached_st *memc, const char *key)
@@ -336,6 +336,8 @@ 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) {
340+
*val = ZSTR_EMPTY_ALLOC();
339341
} else {
340342
return FAILURE;
341343
}
@@ -361,8 +363,8 @@ PS_WRITE_FUNC(memcached)
361363
return FAILURE;
362364
}
363365

364-
if (PS(gc_maxlifetime) > 0) {
365-
expiration = PS(gc_maxlifetime);
366+
if (maxlifetime > 0) {
367+
expiration = maxlifetime;
366368
}
367369

368370
/* Set the number of write retry attempts to the number of replicas times the number of attempts to remove a server plus the initial write */
@@ -403,4 +405,60 @@ PS_GC_FUNC(memcached)
403405
{
404406
return SUCCESS;
405407
}
408+
409+
PS_CREATE_SID_FUNC(memcached)
410+
{
411+
zend_string *sid;
412+
int maxfail = 3;
413+
memcached_sess *memc_sess = PS_GET_MOD_DATA();
414+
415+
do {
416+
sid = php_session_create_id((void**)&memc_sess);
417+
if (!sid) {
418+
if (--maxfail < 0) {
419+
return NULL;
420+
} else {
421+
continue;
422+
}
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+
zend_string_release(sid);
429+
sid = NULL;
430+
}
431+
if (--maxfail < 0) {
432+
return NULL;
433+
}
434+
}
435+
} while(!sid);
436+
437+
return sid;
438+
}
439+
440+
PS_VALIDATE_SID_FUNC(memcached)
441+
{
442+
memcached_sess *memc_sess = PS_GET_MOD_DATA();
443+
444+
if (memcached_exist(memc_sess->memc_sess, key->val, key->len) == MEMCACHED_SUCCESS) {
445+
return SUCCESS;
446+
} else {
447+
return FAILURE;
448+
}
449+
}
450+
451+
PS_UPDATE_TIMESTAMP_FUNC(memcached)
452+
{
453+
memcached_sess *memc_sess = PS_GET_MOD_DATA();
454+
time_t expiration = 0;
455+
456+
if (maxlifetime > 0) {
457+
expiration = maxlifetime;
458+
}
459+
if (memcached_touch(memc_sess->memc_sess, key->val, key->len, expiration) == MEMCACHED_FAILURE) {
460+
return FAILURE;
461+
}
462+
return SUCCESS;
463+
}
406464
/* }}} */

php_memcached_session.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
extern ps_module ps_mod_memcached;
2525
#define ps_memcached_ptr &ps_mod_memcached
2626

27-
PS_FUNCS(memcached);
27+
PS_FUNCS_UPDATE_TIMESTAMP(memcached);
2828

2929
PS_OPEN_FUNC(memcached);
3030
PS_CLOSE_FUNC(memcached);
3131
PS_READ_FUNC(memcached);
3232
PS_WRITE_FUNC(memcached);
3333
PS_DESTROY_FUNC(memcached);
3434
PS_GC_FUNC(memcached);
35+
PS_CREATE_SID_FUNC(memcached);
36+
PS_VALIDATE_SID_FUNC(memcached);
37+
PS_UPDATE_TIMESTAMP_FUNC(memcached);
3538

3639
#endif /* PHP_MEMCACHED_SESSION_H */

tests/session_basic2.phpt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
Session basic open, write, destroy
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("memcached")) print "skip";
6+
if (!Memcached::HAVE_SESSION) print "skip";
7+
?>
8+
--INI--
9+
memcached.sess_locking = on
10+
memcached.sess_lock_wait = 150000
11+
memcached.sess_prefix = "memc.sess.key."
12+
session.save_handler = memcached
13+
14+
--FILE--
15+
<?php
16+
include dirname (__FILE__) . '/config.inc';
17+
ini_set ('session.save_path', MEMC_SERVER_HOST . ':' . MEMC_SERVER_PORT);
18+
19+
error_reporting(0);
20+
21+
session_start(['lazy_write'=>TRUE);
22+
$_SESSION['foo'] = 1;
23+
session_write_close();
24+
25+
$_SESSION = NULL;
26+
27+
var_dump($_SESSION);
28+
session_start();
29+
var_dump($_SESSION);
30+
session_write_close();
31+
32+
session_start();
33+
session_destroy();
34+
35+
session_start();
36+
var_dump($_SESSION);
37+
session_write_close();
38+
39+
40+
--EXPECT--
41+
NULL
42+
array(1) {
43+
["foo"]=>
44+
int(1)
45+
}
46+
array(0) {
47+
}

tests/session_basic3.phpt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
Session basic open, write, destroy
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("memcached")) print "skip";
6+
if (!Memcached::HAVE_SESSION) print "skip";
7+
?>
8+
--INI--
9+
memcached.sess_locking = on
10+
memcached.sess_lock_wait = 150000
11+
memcached.sess_prefix = "memc.sess.key."
12+
session.save_handler = memcached
13+
14+
--FILE--
15+
<?php
16+
include dirname (__FILE__) . '/config.inc';
17+
ini_set ('session.save_path', MEMC_SERVER_HOST . ':' . MEMC_SERVER_PORT);
18+
19+
error_reporting(0);
20+
21+
session_start(['read_only'=>TRUE);
22+
$_SESSION['foo'] = 1;
23+
session_write_close();
24+
25+
$_SESSION = NULL;
26+
27+
var_dump($_SESSION);
28+
session_start();
29+
var_dump($_SESSION);
30+
session_write_close();
31+
32+
session_start();
33+
session_destroy();
34+
35+
session_start();
36+
var_dump($_SESSION);
37+
session_write_close();
38+
39+
40+
--EXPECT--
41+
NULL
42+
array(1) {
43+
["foo"]=>
44+
int(1)
45+
}
46+
array(0) {
47+
}

0 commit comments

Comments
 (0)