-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathredis-session.php
executable file
·85 lines (60 loc) · 2.16 KB
/
redis-session.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?
namespace RedisSession;
$redisTargetPrefix = "session:php:";
$unpackItems = array();
$redisServer = "redis://127.0.0.1:6379/";
function redis_session_init($unpack = null, $server = null, $prefix = null) {
global $unpackItems, $redisServer, $redisTargetPrefix;
if ($unpack !== null) {
$unpackItems = $unpack;
}
if ($server !== null) {
$redisServer = $server;
}
if ($prefix !== null) {
$redisTargetPrefix = $prefix;
}
session_set_save_handler('RedisSession\redis_session_open',
'RedisSession\redis_session_close',
'RedisSession\redis_session_read',
'RedisSession\redis_session_write',
'RedisSession\redis_session_destroy',
'RedisSession\redis_session_gc');
}
function redis_session_read($id) {
global $redisServer, $redisTargetPrefix;
$redisConnection = new \Predis\Client($redisServer);
return base64_decode($redisConnection->get($redisTargetPrefix . $id));
}
function redis_session_write($id, $data) {
global $unpackItems, $redisServer, $redisTargetPrefix;
$redisConnection = new \Predis\Client($redisServer);
$ttl = ini_get("session.gc_maxlifetime");
$redisConnection->pipeline(function ($r) use (&$id, &$data, &$redisTargetPrefix, &$ttl, &$unpackItems) {
$r->setex($redisTargetPrefix . $id, $ttl,
base64_encode($data));
foreach ($unpackItems as $item) {
$keyname = $redisTargetPrefix . $id . ":" . $item;
if (isset($_SESSION[$item])) {
$r->setex($keyname, $ttl, $_SESSION[$item]);
} else {
$r->del($keyname);
}
}
});
}
function redis_session_destroy($id) {
global $redisServer, $redisTargetPrefix;
$redisConnection = new \Predis\Client($redisServer);
$redisConnection->del($redisTargetPrefix . $id);
$unpacked = $redisConnection->keys($redisTargetPrefix . $id . ":*");
foreach ($unpacked as $unp) {
$redisConnection->del($unp);
}
}
// These functions are all noops for various reasons... opening has no practical meaning in
// terms of non-shared Redis connections, the same for closing. Garbage collection is handled by
// Redis anyway.
function redis_session_open($path, $name) {}
function redis_session_close() {}
function redis_session_gc($age) {}