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