CacheSession.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Model.Datasource.Session
  17. * @since CakePHP(tm) v 2.0
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('Cache', 'Cache');
  21. App::uses('CakeSessionHandlerInterface', 'Model/Datasource/Session');
  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. * Method called on close of a database session.
  39. *
  40. * @return boolean Success
  41. */
  42. public function close() {
  43. return true;
  44. }
  45. /**
  46. * Method used to read from a database session.
  47. *
  48. * @param string $id The key of the value to read
  49. * @return mixed The value of the key or false if it does not exist
  50. */
  51. public function read($id) {
  52. return Cache::read($id, Configure::read('Session.handler.config'));
  53. }
  54. /**
  55. * Helper function called on write for database sessions.
  56. *
  57. * @param integer $id ID that uniquely identifies session in database
  58. * @param mixed $data The value of the data to be saved.
  59. * @return boolean True for successful write, false otherwise.
  60. */
  61. public function write($id, $data) {
  62. return Cache::write($id, $data, Configure::read('Session.handler.config'));
  63. }
  64. /**
  65. * Method called on the destruction of a database session.
  66. *
  67. * @param integer $id ID that uniquely identifies session in cache
  68. * @return boolean True for successful delete, false otherwise.
  69. */
  70. public function destroy($id) {
  71. return Cache::delete($id, Configure::read('Session.handler.config'));
  72. }
  73. /**
  74. * Helper function called on gc for cache sessions.
  75. *
  76. * @param integer $expires Timestamp (defaults to current time)
  77. * @return boolean Success
  78. */
  79. public function gc($expires = null) {
  80. return Cache::gc(Configure::read('Session.handler.config'), $expires);
  81. }
  82. }