@@ -131,46 +131,40 @@ public function rawcommand(string $command, mixed ...$arguments): mixed {
131131 * @return array<string, mixed>
132132 */
133133 public function pipelineKeys (array $ keys ): array {
134- $ results = $ this ->pipeline (function ($ pipe ) use ($ keys ): void {
134+ $ lua_memory = 'return redis.call("MEMORY", "USAGE", KEYS[1]) ' ;
135+ $ lua_type = /* @lang Lua */
136+ <<<LUA
137+ local type = redis.call("TYPE", KEYS[1])["ok"]
138+ if type == "set" then return redis.call("SCARD", KEYS[1])
139+ elseif type == "list" then return redis.call("LLEN", KEYS[1])
140+ elseif type == "zset" then return redis.call("ZCARD", KEYS[1])
141+ elseif type == "hash" then return redis.call("HLEN", KEYS[1])
142+ elseif type == "stream" then return redis.call("XLEN", KEYS[1])
143+ else return nil end
144+ LUA ;
145+
146+ $ results = $ this ->pipeline (function ($ pipe ) use ($ keys , $ lua_memory , $ lua_type ): void {
135147 foreach ($ keys as $ key ) {
136148 $ pipe ->ttl ($ key );
137149 $ pipe ->type ($ key );
138- $ pipe ->eval ('return redis.call("MEMORY", "USAGE", KEYS[1]) ' , 1 , $ key );
150+ $ pipe ->eval ($ lua_memory , 1 , $ key );
151+ $ pipe ->eval ($ lua_type , 1 , $ key );
139152 }
140153 });
141154
142155 $ data = [];
143156
144157 foreach ($ keys as $ i => $ key ) {
145- $ index = $ i * 3 ;
158+ $ index = $ i * 4 ;
159+
146160 $ data [$ key ] = [
147161 'ttl ' => $ results [$ index ],
148162 'type ' => (string ) $ results [$ index + 1 ],
149- 'size ' => is_int ( $ results [$ index + 2 ]) ? $ results [ $ index + 2 ] : 0 ,
150- 'count ' => null ,
163+ 'size ' => $ results [$ index + 2 ] ?? 0 ,
164+ 'count ' => is_int ( $ results [ $ index + 3 ]) ? $ results [ $ index + 3 ] : null ,
151165 ];
152166 }
153167
154- $ count_results = $ this ->pipeline (function ($ pipe ) use ($ keys , $ data ): void {
155- foreach ($ keys as $ key ) {
156- $ lua = match ($ data [$ key ]['type ' ]) {
157- 'set ' => 'return redis.call("SCARD", KEYS[1]) ' ,
158- 'list ' => 'return redis.call("LLEN", KEYS[1]) ' ,
159- 'zset ' => 'return redis.call("ZCARD", KEYS[1]) ' ,
160- 'hash ' => 'return redis.call("HLEN", KEYS[1]) ' ,
161- 'stream ' => 'return redis.call("XLEN", KEYS[1]) ' ,
162- default => 'return nil ' ,
163- };
164-
165- $ pipe ->eval ($ lua , 1 , $ key );
166- }
167- });
168-
169- foreach ($ keys as $ i => $ key ) {
170- $ count = $ count_results [$ i ] ?? null ;
171- $ data [$ key ]['count ' ] = $ data [$ key ]['type ' ] !== 'none ' && is_int ($ count ) ? $ count : null ;
172- }
173-
174168 return $ data ;
175169 }
176170
0 commit comments