CacheSession.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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-2012, 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-2012, 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. App::uses('Cache', 'Cache');
  20. App::uses('CakeSessionHandlerInterface', 'Model/Datasource/Session');
  21. /**
  22. * CacheSession provides method for saving sessions into a Cache engine. Used with CakeSession
  23. *
  24. * @package Cake.Model.Datasource.Session
  25. * @see CakeSession for configuration information.
  26. */
  27. class CacheSession implements CakeSessionHandlerInterface {
  28. /**
  29. * Method called on open of a database session.
  30. *
  31. * @return boolean Success
  32. */
  33. public function open() {
  34. return true;
  35. }
  36. /**
  37. * Method called on close of a database session.
  38. *
  39. * @return boolean Success
  40. */
  41. public function close() {
  42. return true;
  43. }
  44. /**
  45. * Method used to read from a database session.
  46. *
  47. * @param string $id The key of the value to read
  48. * @return mixed The value of the key or false if it does not exist
  49. */
  50. public function read($id) {
  51. return Cache::read($id, Configure::read('Session.handler.config'));
  52. }
  53. /**
  54. * Helper function called on write for database sessions.
  55. *
  56. * @param integer $id ID that uniquely identifies session in database
  57. * @param mixed $data The value of the data to be saved.
  58. * @return boolean True for successful write, false otherwise.
  59. */
  60. public function write($id, $data) {
  61. return Cache::write($id, $data, Configure::read('Session.handler.config'));
  62. }
  63. /**
  64. * Method called on the destruction of a database session.
  65. *
  66. * @param integer $id ID that uniquely identifies session in cache
  67. * @return boolean True for successful delete, false otherwise.
  68. */
  69. public function destroy($id) {
  70. return Cache::delete($id, Configure::read('Session.handler.config'));
  71. }
  72. /**
  73. * Helper function called on gc for cache sessions.
  74. *
  75. * @param integer $expires Timestamp (defaults to current time)
  76. * @return boolean Success
  77. */
  78. public function gc($expires = null) {
  79. return Cache::gc(Configure::read('Session.handler.config'), $expires);
  80. }
  81. }