CacheSession.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.libs
  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. /**
  21. * CacheSession provides method for saving sessions into a Cache engine. Used with CakeSession
  22. *
  23. * @package cake.libs.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. * @access private
  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. * @access private
  41. */
  42. public function close() {
  43. $probability = mt_rand(1, 150);
  44. if ($probability <= 3) {
  45. Cache::gc();
  46. }
  47. return true;
  48. }
  49. /**
  50. * Method used to read from a database session.
  51. *
  52. * @param mixed $id The key of the value to read
  53. * @return mixed The value of the key or false if it does not exist
  54. * @access private
  55. */
  56. public function read($id) {
  57. return Cache::read($id, Configure::read('Session.handler.config'));
  58. }
  59. /**
  60. * Helper function called on write for database sessions.
  61. *
  62. * @param integer $id ID that uniquely identifies session in database
  63. * @param mixed $data The value of the data to be saved.
  64. * @return boolean True for successful write, false otherwise.
  65. * @access private
  66. */
  67. public function write($id, $data) {
  68. return Cache::write($id, $data, Configure::read('Session.handler.config'));
  69. }
  70. /**
  71. * Method called on the destruction of a database session.
  72. *
  73. * @param integer $id ID that uniquely identifies session in database
  74. * @return boolean True for successful delete, false otherwise.
  75. * @access private
  76. */
  77. public function destroy($id) {
  78. return Cache::delete($id, Configure::read('Session.handler.config'));
  79. }
  80. /**
  81. * Helper function called on gc for database sessions.
  82. *
  83. * @param integer $expires Timestamp (defaults to current time)
  84. * @return boolean Success
  85. * @access private
  86. */
  87. public function gc($expires = null) {
  88. return Cache::gc();
  89. }
  90. }