RedisSession.Class.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Mall\Framework\Session\SaveHandler;
  3. use Mall\Framework\Factory;
  4. class RedisSession implements SaveHandlerInterface
  5. {
  6. /**
  7. * Session Save Path
  8. *
  9. * @var string
  10. */
  11. protected $sessionSavePath;
  12. /**
  13. * Session Name
  14. *
  15. * @var string
  16. */
  17. protected $sessionName;
  18. /**
  19. * The cache storage
  20. *
  21. * @var CacheStorage
  22. */
  23. protected $cacheStorage;
  24. public function __construct($storage = '')
  25. {
  26. $storage = $storage ?: Factory::cache();
  27. $this->setCacheStorage($storage);
  28. }
  29. /**
  30. * Open Session
  31. *
  32. * @param string $savePath
  33. * @param string $name
  34. * @return bool
  35. */
  36. public function open($savePath, $name)
  37. {
  38. $this->sessionSavePath = $savePath;
  39. $this->sessionName = $name;
  40. return true;
  41. }
  42. /**
  43. * Close session
  44. *
  45. * @return bool
  46. */
  47. public function close()
  48. {
  49. return true;
  50. }
  51. /**
  52. * Read session data
  53. *
  54. * @param string $id
  55. * @return string
  56. */
  57. public function read($id)
  58. {
  59. return $this->getCacheStorage()->get($id) ?: '';
  60. }
  61. /**
  62. * Write session data
  63. *
  64. * @param string $id
  65. * @param string $data
  66. * @return bool
  67. */
  68. public function write($id, $data)
  69. {
  70. return $this->getCacheStorage()->set($id, $data);
  71. }
  72. /**
  73. * Destroy session
  74. *
  75. * @param string $id
  76. * @return bool
  77. */
  78. public function destroy($id)
  79. {
  80. return $this->getCacheStorage()->delete($id);
  81. }
  82. /**
  83. * Garbage Collection
  84. *
  85. * @param int $maxlifetime
  86. * @return bool
  87. */
  88. public function gc($maxlifetime)
  89. {
  90. return true;
  91. }
  92. /**
  93. * Set cache storage
  94. *
  95. * @param CacheStorage $cacheStorage
  96. * @return Cache
  97. */
  98. public function setCacheStorage($cacheStorage)
  99. {
  100. $this->cacheStorage = $cacheStorage;
  101. return $this;
  102. }
  103. /**
  104. * Get cache storage
  105. *
  106. * @return CacheStorage
  107. */
  108. public function getCacheStorage()
  109. {
  110. return $this->cacheStorage;
  111. }
  112. }