Skip to content

Commit 0e43d46

Browse files
author
David Alger
committed
Added chunking logic to the SUNION call to avoid lengthy blocking.
1 parent a2d6065 commit 0e43d46

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Cm/Cache/Backend/Redis.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
8080
/** @var string */
8181
protected $_compressionLib;
8282

83+
/**
84+
* On large data sets SUNION slows down considerably when used with too many arguments
85+
* so this is used to chunk the SUNION into a few commands where the number of set ids
86+
* exceeds this setting.
87+
*
88+
* @var int
89+
*/
90+
protected $_sunionChunkSize = 500;
91+
8392
/** @var bool */
8493
protected $_useLua = false;
8594

@@ -177,6 +186,10 @@ public function __construct($options = array())
177186
}
178187
$this->_compressPrefix = substr($this->_compressionLib,0,2).self::COMPRESS_PREFIX;
179188

189+
if ( isset($options['sunion_chunk_size']) && $options['sunion_chunk_size'] > 0) {
190+
$this->_sunionChunkSize = (int) $options['sunion_chunk_size'];
191+
}
192+
180193
if (isset($options['use_lua'])) {
181194
$this->_useLua = (bool) $options['use_lua'];
182195
}
@@ -757,10 +770,15 @@ public function getIdsNotMatchingTags($tags = array())
757770
*/
758771
public function getIdsMatchingAnyTags($tags = array())
759772
{
773+
$result = array();
760774
if ($tags) {
761-
return (array) $this->_redis->sUnion( $this->_preprocessTagIds($tags));
775+
$chunks = array_chunk($tags, $this->_sunionChunkSize);
776+
foreach ($chunks as $chunk) {
777+
$result = array_merge($result, (array) $this->_redis->sUnion( $this->_preprocessTagIds($chunk)));
778+
}
779+
$result = array_unique($result); // since we are chunking requests, we must de-duplicate member names
762780
}
763-
return array();
781+
return $result;
764782
}
765783

766784
/**

0 commit comments

Comments
 (0)