CacheSession.php 2.7 KB

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