22
33namespace RedisLock ;
44
5- use Predis \Client ;
5+ use Predis \ClientInterface ;
66use RedisLock \LuaScripts ;
77
88/**
@@ -25,7 +25,7 @@ class Processor
2525 // Response string from redis cmd: set
2626 const LOCK_SUCCESS = 'OK ' ;
2727
28- // Response string from redis lua script: eval
28+ // Response int from redis lua script: redis.call('del', KEY)
2929 const UNLOCK_SUCCESS = 1 ;
3030
3131 // Params for cmd: set
@@ -52,39 +52,34 @@ class Processor
5252 private $ expireType ;
5353
5454 /**
55- * How many times do you want to try again.
56- * (milliseconds)
55+ * Number of retry times.
5756 *
58- * @var integer
57+ * @var int
5958 */
60- private $ retryDelay = 200 ;
59+ private $ retryCount = 3 ;
6160
6261 /**
63- * Number of retry times.
62+ * How many times do you want to try again.
63+ * (milliseconds)
6464 *
6565 * @var int
6666 */
67- private $ retryCount = 3 ;
67+ private $ retryDelay = 200 ;
6868
6969 /**
7070 * This params from service provider.
7171 *
72- * @param Predis\Client $client
73- * @param array|null $config
72+ * @param Predis\ClientInterface $client
73+ * @param int $retryCount
74+ * @param int $retryDelay
7475 */
75- public function __construct (Client $ client , array $ config = null )
76+ public function __construct (ClientInterface $ client , int $ retryCount , int $ retryDelay )
7677 {
7778 $ this ->client = $ client ;
7879
79- if (isset ($ config ['retry ' ])) {
80- $ this ->retryCount = $ config ['retry ' ];
81- }
82-
83- if (isset ($ config ['delay ' ])) {
84- $ this ->retryDelay = $ config ['delay ' ];
85- }
86-
8780 $ this ->setExpireType (self ::EXPIRE_TIME_MILLISECONDS );
81+ $ this ->setRetryDelay ($ retryDelay );
82+ $ this ->retryCount = $ retryCount ;
8883 }
8984
9085 /**
@@ -101,14 +96,13 @@ public function setExpireType(string $value): self
10196 }
10297
10398 /**
104- * Set retry number of times .
99+ * Set retry delay time .
105100 *
106- * @param int $retry
107- * @return self
101+ * @param int $milliseconds
108102 */
109- public function setRetry (int $ retry ): self
103+ public function setRetryDelay (int $ milliseconds ): self
110104 {
111- $ this ->retryCount = $ retry ;
105+ $ this ->retryDelay = $ milliseconds ;
112106
113107 return $ this ;
114108 }
@@ -118,17 +112,17 @@ public function setRetry(int $retry): self
118112 *
119113 * @param string $key
120114 * @param int $expire
121- * @param bool $isWaitingMode
115+ * @param int $retry
122116 * @return array
123117 * - Not empty for getted lock.
124118 * - Empty for lock timeout.
125119 */
126- public function lock (string $ key , int $ expire , bool $ isWaitingMode = true ): array
120+ public function lock (string $ key , int $ expire , int $ retry = null ): array
127121 {
128- $ retry = $ isWaitingMode ? $ this ->retryCount : 0 ;
122+ $ retry = $ retry ?? $ this ->retryCount ?? 0 ;
129123
130124 while (! $ result = $ this ->hit ($ key , $ expire )) {
131- if (--$ retry < 0 ) {
125+ if (--$ retry < 1 ) {
132126 return $ result ;
133127 }
134128
0 commit comments