3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Capability\ICanCache;
25 use Friendica\Core\Cache\Exception\CachePersistenceException;
26 use Psr\Log\LoggerInterface;
29 * SessionHandler using Friendica Cache
31 class Cache extends AbstractSessionHandler
35 /** @var LoggerInterface */
38 public function __construct(ICanCache $cache, LoggerInterface $logger)
40 $this->cache = $cache;
41 $this->logger = $logger;
44 public function open($path, $name): bool
49 public function read($id)
56 $data = $this->cache->get('session:' . $id);
60 } catch (CachePersistenceException $exception) {
61 $this->logger->warning('Cannot read session.', ['id' => $id, 'exception' => $exception]);
69 * Standard PHP session write callback
71 * This callback updates the stored session data and/or the expiration depending
72 * on the case. Uses the Session::expire for existing session, 5 minutes
73 * for newly created session.
75 * @param string $id Session ID with format: [a-z0-9]{26}
76 * @param string $data Serialized session data
78 * @return bool Returns false if parameters are missing, true otherwise
80 public function write($id, $data): bool
87 return $this->destroy($id);
91 return $this->cache->set('session:' . $id, $data, static::EXPIRE);
92 } catch (CachePersistenceException $exception) {
93 $this->logger->warning('Cannot write session', ['id' => $id, 'exception' => $exception]);
98 public function close(): bool
103 public function destroy($id): bool
106 return $this->cache->delete('session:' . $id);
107 } catch (CachePersistenceException $exception) {
108 $this->logger->warning('Cannot destroy session', ['id' => $id, 'exception' => $exception]);
113 public function gc($max_lifetime): bool