123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- /**
- * A base class for redis entities.
- * Extends CBehavior to allow entities to be attached to models and components.
- * <pre>
- * $user = User::model()->findByPk(1);
- * $counter = new ARedisCounter("totalLogins");
- * $user->attachBehavior("totalLogins", $counter);
- * echo $user->totalLogins."\n"; // 0
- * $user->totalLogins->increment();
- * echo $user->totalLogins."\n"; // 1
- *
- * $friends = new ARedisSet("friendIds");
- * $user->attachBehavior("friendIds",$friends);
- * foreach($user->friendIds as $id) {
- * echo "User ".$user->id." is friends with user ".$id."\n";
- * }
- *
- * </pre>
- * @package packages.redis
- * @author Charles Pick
- */
- abstract class ARedisEntity extends CBehavior {
- /**
- * The name of the redis entity (key)
- * @var string
- */
- public $name;
- /**
- * Holds the redis connection
- * @var ARedisConnection
- */
- protected $_connection;
- /**
- * The old name of this entity
- * @var string
- */
- protected $_oldName;
- /**
- * Constructor
- * @param string $name the name of the entity
- * @param ARedisConnection|string $connection the redis connection to use with this entity
- */
- public function __construct($name = null, $connection = null) {
- if ($name !== null) {
- $this->name = $name;
- }
- if ($connection !== null) {
- $this->setConnection($connection);
- }
- }
- /**
- * Attaches the entity to a component
- * @throws CException if no name is set
- * @param CComponent $owner the owner component
- */
- public function attach($owner) {
- parent::attach($owner);
- if ($this->name === null) {
- throw new CException("No name specified for ".get_class($this));
- }
- if (method_exists($owner, "getPrimaryKey")) {
- $this->_oldName = $this->name;
- $pk = $owner->getPrimaryKey();
- if (is_array($pk)) {
- foreach($pk as $key => $value) {
- $pk[$key] = $key.":".$value;
- }
- $pk = implode(":",$pk);
- }
- $this->name = get_class($owner).":".$pk.":".$this->name;
- }
- }
- /**
- * Detaches the entity from a component
- * @param CComponent $owner the owner component
- */
- public function detach($owner) {
- parent::detach($owner);
- if (method_exists($owner, "getPrimaryKey")) {
- $this->name = $this->_oldName;
- }
- }
- /**
- * Sets the redis connection to use for this entity
- * @param ARedisConnection|string $connection the redis connection, if a string is provided, it is presumed to be a the name of an applciation component
- */
- public function setConnection($connection)
- {
- if (is_string($connection)) {
- $connection = Yii::app()->{$connection};
- }
- $this->_connection = $connection;
- }
- /**
- * Gets the redis connection to use for this entity
- * @return ARedisConnection
- */
- public function getConnection()
- {
- if ($this->_connection === null) {
- if (!isset(Yii::app()->redis)) {
- throw new CException(get_class($this)." expects a 'redis' application component");
- }
- $this->_connection = Yii::app()->redis;
- }
- return $this->_connection;
- }
- }
|