Skip to content

Commit

Permalink
- Change dbSize implements into using scan
Browse files Browse the repository at this point in the history
  • Loading branch information
alexxiyang committed Jun 11, 2018
1 parent 354c6bf commit 16ac6b9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 29 deletions.
15 changes: 12 additions & 3 deletions src/main/java/org/crazycake/shiro/BaseRedisManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,20 @@ public void del(byte[] key) {
* Return the size of redis db.
*/
@Override
public Long dbSize() {
Long dbSize = 0L;
public Long dbSize(byte[] pattern) {
long dbSize = 0L;
Jedis jedis = getJedis();
try {
dbSize = jedis.dbSize();
ScanParams params = new ScanParams();
params.count(count);
params.match(pattern);
byte[] cursor = ScanParams.SCAN_POINTER_START_BINARY;
ScanResult<byte[]> scanResult;
do {
scanResult = jedis.scan(cursor, params);
dbSize++;
cursor = scanResult.getCursorAsBytes();
} while (scanResult.getStringCursor().compareTo(ScanParams.SCAN_POINTER_START) > 0);
} finally {
jedis.close();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/crazycake/shiro/IRedisManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IRedisManager {
/**
* size
*/
Long dbSize();
Long dbSize(byte[] pattern);

/**
* keys
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/crazycake/shiro/RedisCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ public void clear() throws CacheException {

@Override
public int size() {
Long longSize = new Long(redisManager.dbSize());
Long longSize = 0L;
try {
longSize = new Long(redisManager.dbSize(keySerializer.serialize(this.keyPrefix + "*")));
} catch (SerializationException e) {
logger.error("get keys error", e);
}
return longSize.intValue();
}

Expand Down
44 changes: 34 additions & 10 deletions src/main/java/org/crazycake/shiro/RedisClusterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ protected JedisCluster getJedisCluster() {
return jedisCluster;
}

@Override
public byte[] get(byte[] key) {
if (key == null) {
return null;
}
return getJedisCluster().get(key);
}

@Override
public byte[] set(byte[] key, byte[] value, int expireTime) {
if (key == null) {
return null;
Expand All @@ -79,30 +81,31 @@ public byte[] set(byte[] key, byte[] value, int expireTime) {
return value;
}

@Override
public void del(byte[] key) {
if (key == null) {
return;
}
getJedisCluster().del(key);
}

public Long dbSize() {
@Override
public Long dbSize(byte[] pattern) {
Long dbSize = 0L;
Map<String, JedisPool> clusterNodes = getJedisCluster().getClusterNodes();
for (String k : clusterNodes.keySet()) {
JedisPool jp = clusterNodes.get(k);
Jedis connection = jp.getResource();
try {
dbSize += connection.dbSize();
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.close();
Iterator<Map.Entry<String, JedisPool>> nodeIt = clusterNodes.entrySet().iterator();
while (nodeIt.hasNext()) {
Map.Entry<String, JedisPool> node = nodeIt.next();
long nodeDbSize = getDbSizeFromClusterNode(node.getValue(), pattern);
if (nodeDbSize == 0L) {
continue;
}
dbSize += nodeDbSize;
}
return dbSize;
}

@Override
public Set<byte[]> keys(byte[] pattern) {
Set<byte[]> keys = new HashSet<byte[]>();
Map<String, JedisPool> clusterNodes = getJedisCluster().getClusterNodes();
Expand Down Expand Up @@ -140,6 +143,27 @@ private Set<byte[]> getKeysFromClusterNode(JedisPool jedisPool, byte[] pattern)
return keys;
}

private long getDbSizeFromClusterNode(JedisPool jedisPool, byte[] pattern) {
long dbSize = 0L;
Jedis jedis = jedisPool.getResource();

try {
ScanParams params = new ScanParams();
params.count(count);
params.match(pattern);
byte[] cursor = ScanParams.SCAN_POINTER_START_BINARY;
ScanResult<byte[]> scanResult;
do {
scanResult = jedis.scan(cursor, params);
dbSize++;
cursor = scanResult.getCursorAsBytes();
} while (scanResult.getStringCursor().compareTo(ScanParams.SCAN_POINTER_START) > 0);
} finally {
jedis.close();
}
return dbSize;
}

public int getMaxAttempts() {
return maxAttempts;
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/crazycake/shiro/RedisCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public void testRedisCache() {
}

@Test
public void testSize() {
when(redisManager.dbSize()).thenReturn(2L);
public void testSize() throws SerializationException {
when(redisManager.dbSize(keySerializer.serialize(testPrefix + "*"))).thenReturn(2L);
assertThat(redisCache.size(), is(2));
}

Expand Down
12 changes: 0 additions & 12 deletions src/test/java/org/crazycake/shiro/RedisManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -85,17 +84,6 @@ public void testDel() throws SerializationException {
verify(jedis, times(1)).del(any((new byte[0]).getClass()));
}

@Test
public void testDbSize() {
when(jedis.dbSize()).thenReturn(3L);
Long actualDbSize = redisManager.dbSize();
assertThat(actualDbSize, is(3L));

when(jedis.dbSize()).thenReturn(null);
actualDbSize = redisManager.dbSize();
assertThat(actualDbSize, is(nullValue()));
}

@Test
public void testKeys() throws SerializationException {
ScanResult<byte[]> scanResult = mock(ScanResult.class);
Expand Down

0 comments on commit 16ac6b9

Please sign in to comment.