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:  * @package       Cake.Model.Datasource.Session
15:  * @since         CakePHP(tm) v 2.0
16:  * @license       https://opensource.org/licenses/mit-license.php MIT License
17:  */
18: 
19: App::uses('Cache', 'Cache');
20: App::uses('CakeSessionHandlerInterface', 'Model/Datasource/Session');
21: 
22: /**
23:  * CacheSession provides method for saving sessions into a Cache engine. Used with CakeSession
24:  *
25:  * @package       Cake.Model.Datasource.Session
26:  * @see CakeSession for configuration information.
27:  */
28: class CacheSession implements CakeSessionHandlerInterface {
29: 
30: /**
31:  * Method called on open of a database session.
32:  *
33:  * @return bool Success
34:  */
35:     public function open() {
36:         return true;
37:     }
38: 
39: /**
40:  * Method called on close of a database session.
41:  *
42:  * @return bool Success
43:  */
44:     public function close() {
45:         return true;
46:     }
47: 
48: /**
49:  * Method used to read from a database session.
50:  *
51:  * @param string $id The key of the value to read
52:  * @return mixed The value of the key or false if it does not exist
53:  */
54:     public function read($id) {
55:         $data = Cache::read($id, Configure::read('Session.handler.config'));
56: 
57:         if (!is_numeric($data) && empty($data)) {
58:             return '';
59:         }
60:         return $data;
61:     }
62: 
63: /**
64:  * Helper function called on write for database sessions.
65:  *
66:  * @param int $id ID that uniquely identifies session in database
67:  * @param mixed $data The value of the data to be saved.
68:  * @return bool True for successful write, false otherwise.
69:  */
70:     public function write($id, $data) {
71:         return (bool)Cache::write($id, $data, Configure::read('Session.handler.config'));
72:     }
73: 
74: /**
75:  * Method called on the destruction of a database session.
76:  *
77:  * @param int $id ID that uniquely identifies session in cache
78:  * @return bool True for successful delete, false otherwise.
79:  */
80:     public function destroy($id) {
81:         return (bool)Cache::delete($id, Configure::read('Session.handler.config'));
82:     }
83: 
84: /**
85:  * Helper function called on gc for cache sessions.
86:  *
87:  * @param int $expires Timestamp (defaults to current time)
88:  * @return bool Success
89:  */
90:     public function gc($expires = null) {
91:         return (bool)Cache::gc(Configure::read('Session.handler.config'), $expires);
92:     }
93: 
94: }
95: