]> git.mxchange.org Git - friendica.git/blob - src/Core/Session/MemcacheSessionHandler.php
Line endings are converted to unix style
[friendica.git] / src / Core / Session / MemcacheSessionHandler.php
1 <?php
2
3 namespace Friendica\Core\Session;
4
5 use Friendica\BaseObject;
6 use Friendica\Core\Session;
7 use SessionHandlerInterface;
8 use Memcache;
9
10 require_once 'boot.php';
11 require_once 'include/text.php';
12
13 /**
14  * SessionHandler using Memcache
15  *
16  * @author Hypolite Petovan <mrpetovan@gmail.com>
17  */
18 class MemcacheSessionHandler extends BaseObject implements SessionHandlerInterface
19 {
20         /**
21          * @var Memcache
22          */
23         private $memcache = null;
24
25         /**
26          *
27          * @param Memcache $memcache
28          */
29         public function __construct(Memcache $memcache)
30         {
31                 $this->memcache = $memcache;
32         }
33
34         public function open($save_path, $session_name)
35         {
36                 return true;
37         }
38
39         public function read($session_id)
40         {
41                 if (!x($session_id)) {
42                         return '';
43                 }
44
45                 $data = $this->memcache->get(self::getApp()->get_hostname() . ":session:" . $session_id);
46                 if (!is_bool($data)) {
47                         Session::$exists = true;
48                         return $data;
49                 }
50                 logger("no data for session $session_id", LOGGER_TRACE);
51                 return '';
52         }
53
54         /**
55          * @brief Standard PHP session write callback
56          *
57          * This callback updates the stored session data and/or the expiration depending
58          * on the case. Uses the Session::expire for existing session, 5 minutes
59          * for newly created session.
60          *
61          * @param  string $session_id   Session ID with format: [a-z0-9]{26}
62          * @param  string $session_data Serialized session data
63          * @return boolean Returns false if parameters are missing, true otherwise
64          */
65         public function write($session_id, $session_data)
66         {
67                 if (!$session_id) {
68                         return false;
69                 }
70
71                 if (!$session_data) {
72                         return true;
73                 }
74
75                 $expire = time() + Session::$expire;
76
77                 $this->memcache->set(
78                         self::getApp()->get_hostname() . ":session:" . $session_id,
79                         $session_data,
80                         MEMCACHE_COMPRESSED,
81                         $expire
82                 );
83
84                 return true;
85         }
86
87         public function close()
88         {
89                 return true;
90         }
91
92         public function destroy($id)
93         {
94                 $this->memcache->delete(self::getApp()->get_hostname() . ":session:" . $id);
95                 return true;
96         }
97
98         public function gc($maxlifetime)
99         {
100                 return true;
101         }
102 }