@@ -74,6 +74,16 @@ class Redis implements IHandler
7474 */
7575 private int $ masterPID = 0 ;
7676
77+ /**
78+ * 销毁数据的定时器ID.
79+ */
80+ private ?int $ destroyTimerId = null ;
81+
82+ /**
83+ * 要销毁的键名数组.
84+ */
85+ private array $ destroyKeys = [];
86+
7787 public function __init (): void
7888 {
7989 if ('' === $ this ->key )
@@ -90,6 +100,8 @@ public function __init(): void
90100 $ this ->startPing ($ redis );
91101 });
92102 }
103+
104+ $ this ->startDestroyTimer ();
93105 }
94106
95107 /**
@@ -194,6 +206,10 @@ public function __destruct()
194206 {
195207 Timer::del ($ this ->timerId );
196208 }
209+ if (null !== $ this ->destroyTimerId )
210+ {
211+ Timer::del ($ this ->destroyTimerId );
212+ }
197213 }
198214
199215 /**
@@ -250,9 +266,7 @@ public function destroy(string $key): void
250266 */
251267 public function delayDestroy (string $ key , int $ ttl ): void
252268 {
253- $ this ->useRedis (function (RedisHandler $ redis ) use ($ ttl ) {
254- $ redis ->expire ($ this ->getStoreKey (), $ ttl );
255- });
269+ Timer::after ($ ttl * 1000 , fn () => $ this ->destroyKeys [] = $ key );
256270 }
257271
258272 /**
@@ -428,4 +442,21 @@ public function getOldClientIdByFlag(string $flag): ?int
428442 {
429443 return $ this ->useRedis (fn (RedisHandler $ redis ) => $ redis ->get ($ this ->key . ':binder:old: ' . $ flag ) ?: null );
430444 }
445+
446+ private function startDestroyTimer (): void
447+ {
448+ Timer::tick (1000 , function () {
449+ if ($ keys = $ this ->destroyKeys )
450+ {
451+ $ this ->destroyKeys = [];
452+ $ storeKey = $ this ->getStoreKey ();
453+ $ this ->useRedis (static function (RedisHandler $ redis ) use ($ keys , $ storeKey ) {
454+ foreach (array_chunk ($ keys , 1000 ) as $ keysChunk )
455+ {
456+ $ redis ->hDel ($ storeKey , ...$ keysChunk );
457+ }
458+ });
459+ }
460+ });
461+ }
431462}
0 commit comments