3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Session\Handler;
24 use Friendica\Core\Cache\ICache;
25 use Friendica\Core\Session;
26 use Psr\Log\LoggerInterface;
27 use SessionHandlerInterface;
30 * SessionHandler using Friendica Cache
32 class Cache implements SessionHandlerInterface
36 /** @var LoggerInterface */
38 /** @var array The $_SERVER array */
41 public function __construct(ICache $cache, LoggerInterface $logger, array $server)
43 $this->cache = $cache;
44 $this->logger = $logger;
45 $this->server = $server;
48 public function open($save_path, $session_name)
53 public function read($session_id)
55 if (empty($session_id)) {
59 $data = $this->cache->get('session:' . $session_id);
61 Session::$exists = true;
65 $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
71 * Standard PHP session write callback
73 * This callback updates the stored session data and/or the expiration depending
74 * on the case. Uses the Session::expire for existing session, 5 minutes
75 * for newly created session.
77 * @param string $session_id Session ID with format: [a-z0-9]{26}
78 * @param string $session_data Serialized session data
80 * @return boolean Returns false if parameters are missing, true otherwise
83 public function write($session_id, $session_data)
93 return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
96 public function close()
101 public function destroy($id)
103 return $this->cache->delete('session:' . $id);
106 public function gc($maxlifetime)