Skip to content

Commit aefe3d8

Browse files
committed
Add sInterCard methods to JedisClusterSetCommands and LettuceClusterSetCommands
1 parent 7fb5cef commit aefe3d8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterSetCommands.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
/**
4141
* @author Christoph Strobl
4242
* @author Mark Paluch
43+
* @author Mingi Lee
4344
* @since 2.0
4445
*/
4546
class JedisClusterSetCommands implements RedisSetCommands {
@@ -230,6 +231,25 @@ public Long sInterStore(byte[] destKey, byte[]... keys) {
230231
return sAdd(destKey, result.toArray(new byte[result.size()][]));
231232
}
232233

234+
@Override
235+
public Long sInterCard(byte[]... keys) {
236+
237+
Assert.notNull(keys, "Keys must not be null");
238+
Assert.noNullElements(keys, "Keys must not contain null elements");
239+
240+
if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) {
241+
try {
242+
return connection.getCluster().sintercard(keys);
243+
} catch (Exception ex) {
244+
throw convertJedisAccessException(ex);
245+
}
246+
}
247+
248+
// For multi-slot clusters, calculate intersection cardinality by performing intersection
249+
Set<byte[]> result = sInter(keys);
250+
return (long) result.size();
251+
}
252+
233253
@Override
234254
public Set<byte[]> sUnion(byte[]... keys) {
235255

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClusterSetCommands.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/**
3232
* @author Christoph Strobl
3333
* @author Mark Paluch
34+
* @author Mingi Lee
3435
* @since 2.0
3536
*/
3637
class LettuceClusterSetCommands extends LettuceSetCommands {
@@ -118,6 +119,21 @@ public Long sInterStore(byte[] destKey, byte[]... keys) {
118119
return sAdd(destKey, result.toArray(new byte[result.size()][]));
119120
}
120121

122+
@Override
123+
public Long sInterCard(byte[]... keys) {
124+
125+
Assert.notNull(keys, "Keys must not be null");
126+
Assert.noNullElements(keys, "Keys must not contain null elements");
127+
128+
if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) {
129+
return super.sInterCard(keys);
130+
}
131+
132+
// For multi-slot clusters, calculate intersection cardinality by performing intersection
133+
Set<byte[]> result = sInter(keys);
134+
return (long) result.size();
135+
}
136+
121137
@Override
122138
public Set<byte[]> sUnion(byte[]... keys) {
123139

0 commit comments

Comments
 (0)