ARedisStatePersister.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * ARedisStatePersister implements a redis-based persistent data storage.
  4. *
  5. * It can be used to keep data available through multiple requests and sessions.
  6. *
  7. * By default, ARedisStatePersister stores data through the 'redis' application component.
  8. * You may change used connection by setting the {@link connection} property.
  9. *
  10. * To retrieve the data from CStatePersister, call {@link load()}. To save the data,
  11. * call {@link save()}.
  12. *
  13. * Comparison among state persister, session and cache is as follows:
  14. * <ul>
  15. * <li>session: data persisting within a single user session.</li>
  16. * <li>state persister: data persisting through all requests/sessions (e.g. hit counter).</li>
  17. * <li>cache: volatile and fast storage. It may be used as storage medium for session or state persister.</li>
  18. * </ul>
  19. *
  20. * @author Vasily Gudoshnikov <vgoodvin@gmail.com>
  21. * @package packages.redis
  22. */
  23. class ARedisStatePersister extends CApplicationComponent implements IStatePersister
  24. {
  25. /**
  26. * Holds the redis connection
  27. * @var ARedisConnection
  28. */
  29. protected $_connection;
  30. /**
  31. * @var string
  32. * This value will be used as key in a Redis database and in a caching storage.
  33. */
  34. public $key = 'Yii.ARedisStatePersister.key';
  35. /**
  36. * Initializes the application component.
  37. * This method overrides the parent implementation by checking if redis is available.
  38. */
  39. public function init()
  40. {
  41. $this->getConnection();
  42. parent::init();
  43. }
  44. /**
  45. * Sets the redis connection to use for this session handler
  46. * @param ARedisConnection|string $connection the redis connection, if a string is provided, it is presumed to be a the name of an applciation component
  47. */
  48. public function setConnection($connection)
  49. {
  50. if (is_string($connection)) {
  51. $connection = Yii::app()->{$connection};
  52. }
  53. $this->_connection = $connection;
  54. }
  55. /**
  56. * Gets the redis connection to use for this session handler
  57. * @return ARedisConnection
  58. */
  59. public function getConnection()
  60. {
  61. if ($this->_connection === null) {
  62. if (!isset(Yii::app()->redis)) {
  63. throw new CException(get_class($this)." expects a 'redis' application component");
  64. }
  65. $this->_connection = Yii::app()->redis;
  66. }
  67. return $this->_connection;
  68. }
  69. /**
  70. * Loads state data from persistent storage.
  71. * @return mixed state data. Null if no state data available.
  72. */
  73. public function load()
  74. {
  75. $content = $this->_connection->client->get($this->key);
  76. return ($content !== false) ? unserialize($content) : null;
  77. }
  78. /**
  79. * Saves application state in persistent storage.
  80. * @param mixed $state state data (must be serializable).
  81. */
  82. public function save($state)
  83. {
  84. $this->_connection->client->set($this->key, serialize($state));
  85. }
  86. }