3 namespace Friendica\Core\Session;
5 use Friendica\Core\Cache\ICache;
6 use Friendica\Core\Config\Configuration;
7 use Friendica\Core\Session;
8 use Friendica\Model\User\Cookie;
9 use Psr\Log\LoggerInterface;
10 use SessionHandlerInterface;
13 * SessionHandler using Friendica Cache
15 * @author Hypolite Petovan <hypolite@mrpetovan.com>
17 final class Cache extends Native implements SessionHandlerInterface
21 /** @var LoggerInterface */
23 /** @var array The $_SERVER array */
26 public function __construct(Configuration $config, Cookie $cookie, ICache $cache, LoggerInterface $logger, array $server)
28 parent::__construct($config, $cookie);
30 $this->cache = $cache;
31 $this->logger = $logger;
32 $this->server = $server;
34 session_set_save_handler($this);
37 public function open($save_path, $session_name)
42 public function read($session_id)
44 if (empty($session_id)) {
48 $data = $this->cache->get('session:' . $session_id);
50 Session::$exists = true;
54 $this->logger->notice('no data for session', ['session_id' => $session_id, 'uri' => $this->server['REQUEST_URI'] ?? '']);
60 * @brief Standard PHP session write callback
62 * This callback updates the stored session data and/or the expiration depending
63 * on the case. Uses the Session::expire for existing session, 5 minutes
64 * for newly created session.
66 * @param string $session_id Session ID with format: [a-z0-9]{26}
67 * @param string $session_data Serialized session data
69 * @return boolean Returns false if parameters are missing, true otherwise
72 public function write($session_id, $session_data)
82 return $this->cache->set('session:' . $session_id, $session_data, Session::$expire);
85 public function close()
90 public function destroy($id)
92 return $this->cache->delete('session:' . $id);
95 public function gc($maxlifetime)