88use Hypervel \Redis \Operations \SafeScan ;
99use Hypervel \Redis \RedisConnection ;
1010use Redis ;
11- use RedisCluster ;
1211
1312/**
1413 * Prune stale and orphaned entries from all tag sorted sets.
@@ -48,10 +47,7 @@ public function __construct(
4847 */
4948 public function execute (int $ scanCount = self ::DEFAULT_SCAN_COUNT ): array
5049 {
51- $ isCluster = $ this ->context ->isCluster ();
52-
53- return $ this ->context ->withConnection (function (RedisConnection $ conn ) use ($ scanCount , $ isCluster ) {
54- $ client = $ conn ->client ();
50+ return $ this ->context ->withConnection (function (RedisConnection $ conn ) use ($ scanCount ) {
5551 $ pattern = $ this ->context ->tagScanPattern ();
5652 $ optPrefix = $ this ->context ->optPrefix ();
5753 $ prefix = $ this ->context ->prefix ();
@@ -66,23 +62,23 @@ public function execute(int $scanCount = self::DEFAULT_SCAN_COUNT): array
6662 ];
6763
6864 // Use SafeScan to handle OPT_PREFIX correctly
69- $ safeScan = new SafeScan ($ client , $ optPrefix );
65+ $ safeScan = new SafeScan ($ conn , $ optPrefix );
7066
7167 foreach ($ safeScan ->execute ($ pattern , $ scanCount ) as $ tagKey ) {
7268 ++$ stats ['tags_scanned ' ];
7369
7470 // Step 1: Remove TTL-expired entries (stale by time)
75- $ staleRemoved = $ client ->zRemRangeByScore ($ tagKey , '0 ' , (string ) $ now );
71+ $ staleRemoved = $ conn ->zRemRangeByScore ($ tagKey , '0 ' , (string ) $ now );
7672 $ stats ['stale_entries_removed ' ] += is_int ($ staleRemoved ) ? $ staleRemoved : 0 ;
7773
7874 // Step 2: Remove orphaned entries (cache key doesn't exist)
79- $ orphanResult = $ this ->removeOrphanedEntries ($ client , $ tagKey , $ prefix , $ scanCount, $ isCluster );
75+ $ orphanResult = $ this ->removeOrphanedEntries ($ conn , $ tagKey , $ prefix , $ scanCount );
8076 $ stats ['entries_checked ' ] += $ orphanResult ['checked ' ];
8177 $ stats ['orphans_removed ' ] += $ orphanResult ['removed ' ];
8278
8379 // Step 3: Delete if empty
84- if ($ client ->zCard ($ tagKey ) === 0 ) {
85- $ client ->del ($ tagKey );
80+ if ($ conn ->zCard ($ tagKey ) === 0 ) {
81+ $ conn ->del ($ tagKey );
8682 ++$ stats ['empty_sets_deleted ' ];
8783 }
8884
@@ -100,18 +96,17 @@ public function execute(int $scanCount = self::DEFAULT_SCAN_COUNT): array
10096 * @param string $tagKey The tag sorted set key (without OPT_PREFIX, phpredis auto-adds it)
10197 * @param string $prefix The cache prefix (e.g., "cache:")
10298 * @param int $scanCount Number of members per ZSCAN iteration
103- * @param bool $isCluster Whether we're connected to a Redis Cluster
10499 * @return array{checked: int, removed: int}
105100 */
106101 private function removeOrphanedEntries (
107- Redis | RedisCluster $ client ,
102+ RedisConnection $ conn ,
108103 string $ tagKey ,
109104 string $ prefix ,
110105 int $ scanCount ,
111- bool $ isCluster ,
112106 ): array {
113107 $ checked = 0 ;
114108 $ removed = 0 ;
109+ $ isCluster = $ conn ->isCluster ();
115110
116111 // phpredis 6.1.0+ uses null as initial cursor, older versions use 0
117112 $ iterator = match (true ) {
@@ -121,7 +116,7 @@ private function removeOrphanedEntries(
121116
122117 do {
123118 // ZSCAN returns [member => score, ...] array
124- $ members = $ client ->zScan ($ tagKey , $ iterator , '* ' , $ scanCount );
119+ $ members = $ conn ->zScan ($ tagKey , $ iterator , '* ' , $ scanCount );
125120
126121 if ($ members === false || ! is_array ($ members ) || empty ($ members )) {
127122 break ;
@@ -133,7 +128,7 @@ private function removeOrphanedEntries(
133128 // Check which keys exist:
134129 // - Standard Redis: pipeline() batches commands with less overhead
135130 // - Cluster: multi() handles cross-slot commands (pipeline not supported)
136- $ batch = $ isCluster ? $ client ->multi () : $ client ->pipeline ();
131+ $ batch = $ isCluster ? $ conn ->multi () : $ conn ->pipeline ();
137132
138133 foreach ($ memberKeys as $ key ) {
139134 $ batch ->exists ($ prefix . $ key );
@@ -153,7 +148,7 @@ private function removeOrphanedEntries(
153148
154149 // Remove orphaned members from the sorted set
155150 if (! empty ($ orphanedMembers )) {
156- $ client ->zRem ($ tagKey , ...$ orphanedMembers );
151+ $ conn ->zRem ($ tagKey , ...$ orphanedMembers );
157152 $ removed += count ($ orphanedMembers );
158153 }
159154 } while ($ iterator > 0 );
0 commit comments