CacheSession.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Cache Session save handler. Allows saving session information into Cache.
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Model.Datasource.Session
  15. * @since CakePHP(tm) v 2.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Cache', 'Cache');
  19. App::uses('CakeSessionHandlerInterface', 'Model/Datasource/Session');
  20. /**
  21. * CacheSession provides method for saving sessions into a Cache engine. Used with CakeSession
  22. *
  23. * @package Cake.Model.Datasource.Session
  24. * @see CakeSession for configuration information.
  25. */
  26. class CacheSession implements CakeSessionHandlerInterface {
  27. /**
  28. * Method called on open of a database session.
  29. *
  30. * @return boolean Success
  31. */
  32. public function open() {
  33. return true;
  34. }
  35. /**
  36. * Method called on close of a database session.
  37. *
  38. * @return boolean Success
  39. */
  40. public function close() {
  41. return true;
  42. }
  43. /**
  44. * Method used to read from a database session.
  45. *
  46. * @param string $id The key of the value to read
  47. * @return mixed The value of the key or false if it does not exist
  48. */
  49. public function read($id) {
  50. return Cache::read($id, Configure::read('Session.handler.config'));
  51. }
  52. /**
  53. * Helper function called on write for database sessions.
  54. *
  55. * @param integer $id ID that uniquely identifies session in database
  56. * @param mixed $data The value of the data to be saved.
  57. * @return boolean True for successful write, false otherwise.
  58. */
  59. public function write($id, $data) {
  60. return Cache::write($id, $data, Configure::read('Session.handler.config'));
  61. }
  62. /**
  63. * Method called on the destruction of a database session.
  64. *
  65. * @param integer $id ID that uniquely identifies session in cache
  66. * @return boolean True for successful delete, false otherwise.
  67. */
  68. public function destroy($id) {
  69. return Cache::delete($id, Configure::read('Session.handler.config'));
  70. }
  71. /**
  72. * Helper function called on gc for cache sessions.
  73. *
  74. * @param integer $expires Timestamp (defaults to current time)
  75. * @return boolean Success
  76. */
  77. public function gc($expires = null) {
  78. return Cache::gc(Configure::read('Session.handler.config'), $expires);
  79. }
  80. }