MultiExecState.php
2.74 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php namespace common\components\redis\transaction;
/**
* Class MultiExecState
* 管理事务的状态
* 一个事务可以同时存在多个状态
* 比如同时存在INITIALIZED,INSIDEBLOCK,DISCARDED,WATCH等四个状态
* 注意:该类原型来自 \Predis\Transaction\MultiExecState
* @package common\components\redis\transaction
*/
class MultiExecState
{
const INITIALIZED = 1; // 0b00001
const INSIDEBLOCK = 2; // 0b00010
const DISCARDED = 4; // 0b00100
const CAS = 8; // 0b01000
const WATCH = 16; // 0b10000
public $flags;
public function __construct()
{
$this->flags = 0;
}
/**
* Sets the internal state flags.
*
* @param int $flags Set of flags
*/
public function set($flags)
{
$this->flags = $flags;
}
/**
* Gets the internal state flags.
*
* @return int
*/
public function get()
{
return $this->flags;
}
/**
* 设置一个状态
* @param int $flags Set of flags
*/
public function flag($flags)
{
$this->flags |= $flags;
}
/**
* 移除一个存在的状态
* @param int $flags Set of flags
*/
public function unflag($flags)
{
$this->flags &= ~$flags;
}
/**
* 当前状态,或者多个状态是否存在
* @param int $flags Flag
* @return bool
*/
public function check($flags)
{
return ($this->flags & $flags) === $flags;
}
/**
* 重置事务的状态
*/
public function reset()
{
$this->flags = 0;
}
/**
* 状态是否被重置了
* @return bool
*/
public function isReset()
{
return $this->flags === 0;
}
/**
* 返回INITIALIZED状态标识
* @return bool
*/
public function isInitialized()
{
return $this->check(self::INITIALIZED);
}
/**
* 返回INSIDEBLOCK状态标识
* @return bool
*/
public function isExecuting()
{
return $this->check(self::INSIDEBLOCK);
}
/**
* 返回CAS状态标识
* @return bool
*/
public function isCAS()
{
return $this->check(self::CAS);
}
/**
* 返回INITIALIZED状态标识,是否执行WATCH命令
* @return bool
*/
public function isWatchAllowed()
{
return $this->check(self::INITIALIZED) && !$this->check(self::CAS);
}
/**
* 返回WATCH状态标识
* @return bool
*/
public function isWatching()
{
return $this->check(self::WATCH);
}
/**
* 返回DISCARDED状态标识
* @return bool
*/
public function isDiscarded()
{
return $this->check(self::DISCARDED);
}
}