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\Http\Session for configuration information.
27: */
28: class CacheSession implements SessionHandlerInterface
29: {
30: /**
31: * Options for this session engine
32: *
33: * @var array
34: */
35: protected $_options = [];
36:
37: /**
38: * Constructor.
39: *
40: * @param array $config The configuration to use for this engine
41: * It requires the key 'config' which is the name of the Cache config to use for
42: * storing the session
43: *
44: * @throws \InvalidArgumentException if the 'config' key is not provided
45: */
46: public function __construct(array $config = [])
47: {
48: if (empty($config['config'])) {
49: throw new InvalidArgumentException('The cache configuration name to use is required');
50: }
51: $this->_options = $config;
52: }
53:
54: /**
55: * Method called on open of a database session.
56: *
57: * @param string $savePath The path where to store/retrieve the session.
58: * @param string $name The session name.
59: * @return bool Success
60: */
61: public function open($savePath, $name)
62: {
63: return true;
64: }
65:
66: /**
67: * Method called on close of a database session.
68: *
69: * @return bool Success
70: */
71: public function close()
72: {
73: return true;
74: }
75:
76: /**
77: * Method used to read from a cache session.
78: *
79: * @param string|int $id ID that uniquely identifies session in cache.
80: * @return string Session data or empty string if it does not exist.
81: */
82: public function read($id)
83: {
84: $value = Cache::read($id, $this->_options['config']);
85:
86: if (empty($value)) {
87: return '';
88: }
89:
90: return $value;
91: }
92:
93: /**
94: * Helper function called on write for cache sessions.
95: *
96: * @param string|int $id ID that uniquely identifies session in cache.
97: * @param mixed $data The data to be saved.
98: * @return bool True for successful write, false otherwise.
99: */
100: public function write($id, $data)
101: {
102: if (!$id) {
103: return false;
104: }
105:
106: return (bool)Cache::write($id, $data, $this->_options['config']);
107: }
108:
109: /**
110: * Method called on the destruction of a cache session.
111: *
112: * @param string|int $id ID that uniquely identifies session in cache.
113: * @return bool Always true.
114: */
115: public function destroy($id)
116: {
117: Cache::delete($id, $this->_options['config']);
118:
119: return true;
120: }
121:
122: /**
123: * Helper function called on gc for cache sessions.
124: *
125: * @param int $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
126: * @return bool Always true.
127: */
128: public function gc($maxlifetime)
129: {
130: Cache::gc($this->_options['config'], time() - $maxlifetime);
131:
132: return true;
133: }
134: }
135: