Reputation: 275
i am useing spring redisTemplate and redis scan
it works find in condition of single node.
but in cluster enviroment it could not work
i can't get a data.
is there way to get a scan data in clust enviroment?
here is my spring redisTemplate code.
//String key="products:aa";
//String key="products:aac";
//String key="products:ab";
//String key="products:ac";
String workKey="products:aa*";
ScanOptions options = ScanOptions.scanOptions().match(workKey).count(100).build();
ScanOptions options1 = ScanOptions.scanOptions().build();
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
RedisConnection conn = factory.getConnection();
Cursor<byte[]> cursor = conn.scan(options);
List<Product> result = new ArrayList<Product>();
while(cursor.hasNext()){
String key=new String((byte[]) cursor.next());
Product pa=getById(key.replace("products:",""));
result.add(pa);
}
//result
//String key="products:aa";
//String key="products:aac";
Upvotes: 6
Views: 14805
Reputation: 11
Yes agreed, Scan used for single node.You have to scan for each node [master] of cluster. Below are example to delete all keys with matched string in cluster mode with predis.
/* Remove Multiple Keys with matched text */
public function removeMatchedKeys($keyName){
$allRedisHost = array(host1,host2)
if(!empty($allRedisHost ) && is_array($allRedisHost )) {
foreach($allRedisHost as $key=>$host) {
$singleRedisNode = new Predis\Client("tcp://$host:6379");
$it = NULL;
$arr_keys_arr = array();
$searchKeyName= $prefix.":".$keyName.":*";
$i=0;
foreach (new Iterator\Keyspace($singleRedisNode, $searchKeyName) as $actualKey) {
$actualKey = str_replace($prefix . ':', '', $actualKey);
$this->predis->del($actualKey);
$i++;
}
}
}
}
Here "Iterator\Keyspace" internally use scan method. Only need to include Iterator class [Predis\Collection\Iterator].
Upvotes: 1
Reputation: 3305
Scan is a command for single redis node. If you do want to use it in cluster, first get nodes list in the cluster, and run scan for each node.
Upvotes: 3