1: <?php
2: /**
3: * Cache Session save handler. Allows saving session information into Cache.
4: *
5: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7: *
8: * Licensed under The MIT License
9: * For full copyright and license information, please see the LICENSE.txt
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13: * @link https://cakephp.org CakePHP(tm) Project
14: * @since 2.0.0
15: * @license https://opensource.org/licenses/mit-license.php MIT License
16: */
17: namespace Cake\Http\Session;
18:
19: use Cake\Cache\Cache;
20: use InvalidArgumentException;
21: use SessionHandlerInterface;
22:
23: /**
24: * CacheSession provides method for saving sessions into a Cache engine. Used with Session
25: *
26: * @see \Cake\Model\Datasource\Session for configuration information.
27: */
28: class CacheSession implements SessionHandlerInterface
29: {
30:
31: /**
32: * Options for this session engine
33: *
34: * @var array
35: */
36: protected $_options = [];
37:
38: /**
39: * Constructor.
40: *
41: * @param array $config The configuration to use for this engine
42: * It requires the key 'config' which is the name of the Cache config to use for
43: * storing the session
44: *
45: * @throws \InvalidArgumentException if the 'config' key is not provided
46: */
47: public function __construct(array $config = [])
48: {
49: if (empty($config['config'])) {
50: throw new InvalidArgumentException('The cache configuration name to use is required');
51: }
52: $this->_options = $config;
53: }
54:
55: /**
56: * Method called on open of a database session.
57: *
58: * @param string $savePath The path where to store/retrieve the session.
59: * @param string $name The session name.
60: * @return bool Success
61: */
62: public function open($savePath, $name)
63: {
64: return true;
65: }
66:
67: /**
68: * Method called on close of a database session.
69: *
70: * @return bool Success
71: */
72: public function close()
73: {
74: return true;
75: }
76:
77: /**
78: * Method used to read from a cache session.
79: *
80: * @param string|int $id ID that uniquely identifies session in cache.
81: * @return string Session data or empty string if it does not exist.
82: */
83: public function read($id)
84: {
85: $value = Cache::read($id, $this->_options['config']);
86:
87: if (empty($value)) {
88: return '';
89: }
90:
91: return $value;
92: }
93:
94: /**
95: * Helper function called on write for cache sessions.
96: *
97: * @param string|int $id ID that uniquely identifies session in cache.
98: * @param mixed $data The data to be saved.
99: * @return bool True for successful write, false otherwise.
100: */
101: public function write($id, $data)
102: {
103: if (!$id) {
104: return false;
105: }
106:
107: return (bool)Cache::write($id, $data, $this->_options['config']);
108: }
109:
110: /**
111: * Method called on the destruction of a cache session.
112: *
113: * @param string|int $id ID that uniquely identifies session in cache.
114: * @return bool Always true.
115: */
116: public function destroy($id)
117: {
118: Cache::delete($id, $this->_options['config']);
119:
120: return true;
121: }
122:
123: /**
124: * Helper function called on gc for cache sessions.
125: *
126: * @param int $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
127: * @return bool Always true.
128: */
129: public function gc($maxlifetime)
130: {
131: Cache::gc($this->_options['config'], time() - $maxlifetime);
132:
133: return true;
134: }
135: }
136: