Skip to content

Commit 6ec5b04

Browse files
committed
support php74
1 parent e3320ba commit 6ec5b04

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

src/AtomicTimeoutException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Workbunny\WebmanSharedCache;
4+
5+
class AtomicTimeoutException extends \RuntimeException
6+
{
7+
8+
}

src/Traits/BasicMethods.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use APCUIterator;
66
use Closure;
7+
use Workbunny\WebmanSharedCache\AtomicTimeoutException;
78

89
/**
910
* @method static bool Set(string $key, mixed $value, array $optional = []) 设置缓存值
@@ -142,7 +143,9 @@ protected static function _Atomic(string $lockKey, Closure $handler, bool $block
142143
$startTime = time();
143144
while ($blocking) {
144145
// 阻塞保险
145-
if (time() >= $startTime + self::$fuse) {return false;}
146+
if (time() >= $startTime + self::$fuse) {
147+
throw new AtomicTimeoutException('Atomic operation timeout');
148+
}
146149
// 创建锁
147150
apcu_entry($lock = self::GetLockKey($lockKey), function () use (
148151
$lockKey, $handler, $func, &$result, &$blocking
@@ -239,7 +242,7 @@ protected static function _Incr(string $key, $value = 1, int $ttl = 0)
239242
$func = __FUNCTION__;
240243
$result = false;
241244
$params = func_get_args();
242-
$r = self::_Atomic($key, function () use (
245+
self::_Atomic($key, function () use (
243246
$key, $value, $ttl, $func, $params, &$result
244247
) {
245248
$v = self::_Get($key, 0);
@@ -256,7 +259,7 @@ protected static function _Incr(string $key, $value = 1, int $ttl = 0)
256259
'result' => null
257260
];
258261
}, true);
259-
return $r ? $result : false;
262+
return $result;
260263
}
261264

262265
/**
@@ -270,7 +273,7 @@ protected static function _Decr(string $key, $value = 1, int $ttl = 0)
270273
$func = __FUNCTION__;
271274
$result = false;
272275
$params = func_get_args();
273-
$r = self::_Atomic($key, function () use (
276+
self::_Atomic($key, function () use (
274277
$key, $value, $ttl, $func, $params, &$result
275278
) {
276279
$v = self::_Get($key, 0);
@@ -287,7 +290,7 @@ protected static function _Decr(string $key, $value = 1, int $ttl = 0)
287290
'result' => null
288291
];
289292
}, true);
290-
return $r ? $result : false;
293+
return $result;
291294
}
292295

293296
/**

src/Traits/ChannelMethods.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace Workbunny\WebmanSharedCache\Traits;
44

55
use Closure;
6-
use MongoDB\Driver\Exception\RuntimeException;
7-
use Workbunny\WebmanSharedCache\Cache;
86
use Workbunny\WebmanSharedCache\Future;
97
use Error;
108

@@ -107,9 +105,8 @@ protected static function _ChPublish(string $key, $message, bool $store = true,
107105
{
108106
$func = __FUNCTION__;
109107
$params = func_get_args();
110-
$r = false;
111-
return $r && self::_Atomic($key, function () use (
112-
$key, $message, $func, $params, $store, $workerId, &$r
108+
return self::_Atomic($key, function () use (
109+
$key, $message, $func, $params, $store, $workerId
113110
) {
114111
/**
115112
* [
@@ -154,7 +151,7 @@ protected static function _ChPublish(string $key, $message, bool $store = true,
154151
if (self::isChannelUseSignal()) {
155152
$list = self::_Get(self::$_CHANNEL_PID_LIST, []);
156153
foreach ($list as $pid) {
157-
$r = self::_Atomic(self::$_CHANNEL_EVENT_LIST, function () use ($pid) {
154+
self::_Atomic(self::$_CHANNEL_EVENT_LIST, function () use ($pid) {
158155
// 设置通道事件标记
159156
$channelEventList = self::_Get(self::$_CHANNEL_EVENT_LIST, []);
160157
$channelEventList[$pid][] = 1;
@@ -192,7 +189,7 @@ protected static function _ChCreateListener(string $key, $workerId, Closure $lis
192189
if (isset(self::$_listeners[$key])) {
193190
throw new Error("Channel $key listener already exist. ");
194191
}
195-
$r = self::_Atomic($key, function () use (
192+
self::_Atomic($key, function () use (
196193
$key, $workerId, $func, $params, $listener, &$result
197194
) {
198195
// 信号监听则注册pid
@@ -213,7 +210,7 @@ protected static function _ChCreateListener(string $key, $workerId, Closure $lis
213210
// 监听器回调函数
214211
$callback = function () use ($key, $workerId, $listener) {
215212
// 原子性执行
216-
$r = self::_Atomic($key, function () use ($key, $workerId, $listener) {
213+
self::_Atomic($key, function () use ($key, $workerId, $listener) {
217214
// 信号监听
218215
if (self::isChannelUseSignal()) {
219216
$pid = posix_getpid();
@@ -239,9 +236,6 @@ protected static function _ChCreateListener(string $key, $workerId, Closure $lis
239236
}
240237

241238
});
242-
if (!$r) {
243-
throw new \RuntimeException('Channel callback failed.');
244-
}
245239
};
246240
// 设置回调
247241
$channel[$workerId]['futureId'] = self::$_listeners[$key] = $result = Future::add($callback, [], self::$interval);
@@ -261,7 +255,7 @@ protected static function _ChCreateListener(string $key, $workerId, Closure $lis
261255
'result' => null
262256
];
263257
}, true);
264-
return $r ? $result : false;
258+
return $result;
265259
}
266260

267261
/**
@@ -276,7 +270,7 @@ protected static function _ChRemoveListener(string $key, $workerId, bool $remove
276270
{
277271
$func = __FUNCTION__;
278272
$params = func_get_args();
279-
$r = self::_Atomic($key, function () use (
273+
self::_Atomic($key, function () use (
280274
$key, $workerId, $func, $params, $remove
281275
) {
282276
if ($id = self::$_listeners[$key] ?? null) {
@@ -320,8 +314,5 @@ protected static function _ChRemoveListener(string $key, $workerId, bool $remove
320314
'result' => null
321315
];
322316
}, true);
323-
if (!$r) {
324-
throw new RuntimeException("Channel {$key} listener remove failed");
325-
}
326317
}
327318
}

0 commit comments

Comments
 (0)