forked from phpnode/YiiRedis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARedisEntity.php
executable file
·124 lines (117 loc) · 3.1 KB
/
ARedisEntity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?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;
}
/**
* Sets the expiration time in seconds to this entity
* @param integer number of expiration for this entity in seconds
*/
public function expire($seconds)
{
return $this->getConnection()->expire($this->name, $seconds);
}
}