ARedisEntity.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * A base class for redis entities.
  4. * Extends CBehavior to allow entities to be attached to models and components.
  5. * <pre>
  6. * $user = User::model()->findByPk(1);
  7. * $counter = new ARedisCounter("totalLogins");
  8. * $user->attachBehavior("totalLogins", $counter);
  9. * echo $user->totalLogins."\n"; // 0
  10. * $user->totalLogins->increment();
  11. * echo $user->totalLogins."\n"; // 1
  12. *
  13. * $friends = new ARedisSet("friendIds");
  14. * $user->attachBehavior("friendIds",$friends);
  15. * foreach($user->friendIds as $id) {
  16. * echo "User ".$user->id." is friends with user ".$id."\n";
  17. * }
  18. *
  19. * </pre>
  20. * @package packages.redis
  21. * @author Charles Pick
  22. */
  23. abstract class ARedisEntity extends CBehavior {
  24. /**
  25. * The name of the redis entity (key)
  26. * @var string
  27. */
  28. public $name;
  29. /**
  30. * Holds the redis connection
  31. * @var ARedisConnection
  32. */
  33. protected $_connection;
  34. /**
  35. * The old name of this entity
  36. * @var string
  37. */
  38. protected $_oldName;
  39. /**
  40. * Constructor
  41. * @param string $name the name of the entity
  42. * @param ARedisConnection|string $connection the redis connection to use with this entity
  43. */
  44. public function __construct($name = null, $connection = null) {
  45. if ($name !== null) {
  46. $this->name = $name;
  47. }
  48. if ($connection !== null) {
  49. $this->setConnection($connection);
  50. }
  51. }
  52. /**
  53. * Attaches the entity to a component
  54. * @throws CException if no name is set
  55. * @param CComponent $owner the owner component
  56. */
  57. public function attach($owner) {
  58. parent::attach($owner);
  59. if ($this->name === null) {
  60. throw new CException("No name specified for ".get_class($this));
  61. }
  62. if (method_exists($owner, "getPrimaryKey")) {
  63. $this->_oldName = $this->name;
  64. $pk = $owner->getPrimaryKey();
  65. if (is_array($pk)) {
  66. foreach($pk as $key => $value) {
  67. $pk[$key] = $key.":".$value;
  68. }
  69. $pk = implode(":",$pk);
  70. }
  71. $this->name = get_class($owner).":".$pk.":".$this->name;
  72. }
  73. }
  74. /**
  75. * Detaches the entity from a component
  76. * @param CComponent $owner the owner component
  77. */
  78. public function detach($owner) {
  79. parent::detach($owner);
  80. if (method_exists($owner, "getPrimaryKey")) {
  81. $this->name = $this->_oldName;
  82. }
  83. }
  84. /**
  85. * Sets the redis connection to use for this entity
  86. * @param ARedisConnection|string $connection the redis connection, if a string is provided, it is presumed to be a the name of an applciation component
  87. */
  88. public function setConnection($connection)
  89. {
  90. if (is_string($connection)) {
  91. $connection = Yii::app()->{$connection};
  92. }
  93. $this->_connection = $connection;
  94. }
  95. /**
  96. * Gets the redis connection to use for this entity
  97. * @return ARedisConnection
  98. */
  99. public function getConnection()
  100. {
  101. if ($this->_connection === null) {
  102. if (!isset(Yii::app()->redis)) {
  103. throw new CException(get_class($this)." expects a 'redis' application component");
  104. }
  105. $this->_connection = Yii::app()->redis;
  106. }
  107. return $this->_connection;
  108. }
  109. }