1: <?php
2: /**
3: * Cache Session save handler. Allows saving session information into Cache.
4: *
5: * PHP 5
6: *
7: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8: * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
9: *
10: * Licensed under The MIT License
11: * Redistributions of files must retain the above copyright notice.
12: *
13: * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
14: * @link http://cakephp.org CakePHP(tm) Project
15: * @package Cake.Model.Datasource.Session
16: * @since CakePHP(tm) v 2.0
17: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
18: */
19:
20: App::uses('Cache', 'Cache');
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: * Method called on open of a database session.
31: *
32: * @return boolean Success
33: */
34: public function open() {
35: return true;
36: }
37:
38: /**
39: * Method called on close of a database session.
40: *
41: * @return boolean Success
42: */
43: public function close() {
44: $probability = mt_rand(1, 150);
45: if ($probability <= 3) {
46: Cache::gc();
47: }
48: return true;
49: }
50:
51: /**
52: * Method used to read from a database session.
53: *
54: * @param mixed $id The key of the value to read
55: * @return mixed The value of the key or false if it does not exist
56: */
57: public function read($id) {
58: return Cache::read($id, Configure::read('Session.handler.config'));
59: }
60:
61: /**
62: * Helper function called on write for database sessions.
63: *
64: * @param integer $id ID that uniquely identifies session in database
65: * @param mixed $data The value of the data to be saved.
66: * @return boolean True for successful write, false otherwise.
67: */
68: public function write($id, $data) {
69: return Cache::write($id, $data, Configure::read('Session.handler.config'));
70: }
71:
72: /**
73: * Method called on the destruction of a database session.
74: *
75: * @param integer $id ID that uniquely identifies session in database
76: * @return boolean True for successful delete, false otherwise.
77: */
78: public function destroy($id) {
79: return Cache::delete($id, Configure::read('Session.handler.config'));
80: }
81:
82: /**
83: * Helper function called on gc for database sessions.
84: *
85: * @param integer $expires Timestamp (defaults to current time)
86: * @return boolean Success
87: */
88: public function gc($expires = null) {
89: return Cache::gc();
90: }
91:
92: /**
93: * Closes the session before the objects handling it become unavailable
94: *
95: * @return void
96: */
97: public function __destruct() {
98: session_write_close();
99: }
100: }