CacheBasedSessionHandler.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Illuminate\Session;
  3. use SessionHandlerInterface;
  4. use Illuminate\Contracts\Cache\Repository as CacheContract;
  5. class CacheBasedSessionHandler implements SessionHandlerInterface
  6. {
  7. /**
  8. * The cache repository instance.
  9. *
  10. * @var \Illuminate\Contracts\Cache\Repository
  11. */
  12. protected $cache;
  13. /**
  14. * The number of minutes to store the data in the cache.
  15. *
  16. * @var int
  17. */
  18. protected $minutes;
  19. /**
  20. * Create a new cache driven handler instance.
  21. *
  22. * @param \Illuminate\Contracts\Cache\Repository $cache
  23. * @param int $minutes
  24. * @return void
  25. */
  26. public function __construct(CacheContract $cache, $minutes)
  27. {
  28. $this->cache = $cache;
  29. $this->minutes = $minutes;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function open($savePath, $sessionName)
  35. {
  36. return true;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function close()
  42. {
  43. return true;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function read($sessionId)
  49. {
  50. return $this->cache->get($sessionId, '');
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function write($sessionId, $data)
  56. {
  57. return $this->cache->put($sessionId, $data, $this->minutes);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function destroy($sessionId)
  63. {
  64. return $this->cache->forget($sessionId);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function gc($lifetime)
  70. {
  71. return true;
  72. }
  73. /**
  74. * Get the underlying cache repository.
  75. *
  76. * @return \Illuminate\Contracts\Cache\Repository
  77. */
  78. public function getCache()
  79. {
  80. return $this->cache;
  81. }
  82. }