ARedisSession.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * A simple redis based session handler
  4. * @author Charles Pick
  5. * @package packages.redis
  6. */
  7. class ARedisSession extends CHttpSession
  8. {
  9. /**
  10. * The prefix to use when storing and retrieving sessions
  11. * @var string
  12. */
  13. public $keyPrefix = "Yii.ARedisSession.";
  14. /**
  15. * The suffix to use when storing and retrieving sessions
  16. * @var string
  17. */
  18. public $keySuffix = '';
  19. /**
  20. * Holds the redis connection
  21. * @var ARedisConnection
  22. */
  23. protected $_connection;
  24. /**
  25. * Initializes the application component.
  26. * This method overrides the parent implementation by checking if redis is available.
  27. */
  28. public function init()
  29. {
  30. $this->getConnection();
  31. parent::init();
  32. }
  33. /**
  34. * Returns a value indicating whether to use custom session storage.
  35. * @return boolean whether to use custom storage.
  36. */
  37. public function getUseCustomStorage()
  38. {
  39. return true;
  40. }
  41. /**
  42. * Session read handler.
  43. * Do not call this method directly.
  44. * @param string $id session ID
  45. * @return string the session data
  46. */
  47. public function readSession($id)
  48. {
  49. $data=$this->_connection->getClient()->get($this->calculateKey($id));
  50. return $data===false?'':$data;
  51. }
  52. /**
  53. * Session write handler.
  54. * Do not call this method directly.
  55. * @param string $id session ID
  56. * @param string $data session data
  57. * @return boolean whether session write is successful
  58. */
  59. public function writeSession($id, $data)
  60. {
  61. $key = $this->calculateKey($id);
  62. $this->_connection->getClient()->set($key,$data);
  63. $this->_connection->getClient()->expire($key,$this->getTimeout());
  64. return true;
  65. }
  66. /**
  67. * Session destroy handler.
  68. * Do not call this method directly.
  69. * @param string $id session ID
  70. * @return boolean whether session is destroyed successfully
  71. */
  72. public function destroySession($id)
  73. {
  74. $this->_connection->getClient()->delete($this->calculateKey($id));
  75. return true;
  76. }
  77. /**
  78. * Sets the redis connection to use for this session handler
  79. * @param ARedisConnection|string $connection the redis connection, if a string is provided, it is presumed to be a the name of an applciation component
  80. */
  81. public function setConnection($connection)
  82. {
  83. if (is_string($connection))
  84. $connection = Yii::app()->{$connection};
  85. $this->_connection = $connection;
  86. }
  87. /**
  88. * Gets the redis connection to use for this session handler
  89. * @return ARedisConnection
  90. */
  91. public function getConnection()
  92. {
  93. if ($this->_connection === null) {
  94. if (!isset(Yii::app()->redis))
  95. throw new CException(get_class($this)." expects a 'redis' application component");
  96. $this->_connection = Yii::app()->redis;
  97. }
  98. return $this->_connection;
  99. }
  100. /**
  101. * Generates a unique key used for storing session data in cache.
  102. * @param string $id session variable name
  103. * @return string a safe cache key associated with the session variable name
  104. */
  105. protected function calculateKey($id)
  106. {
  107. return $this->keyPrefix.$id.$this->keySuffix;
  108. }
  109. }