redis
前面有简单学习过redis,此处扩展之.. 阿里云redis开发规范
redis工具
数据同步:redis-port
big-key搜索
参考:工具汇总
命令行
redis-cli --bigkeys
find_bigkeys.py
阿里云基于python工具,按key值长度
python find_bigkey.py host port password
rdb_bigkeys
基于go实现的分析rdb文件查找big-key工具
big-key删除
//1、Hash删除: hscan + hdel
public void delBigHash(String host, int port, String password, String bigHashKey) {
Jedis jedis = new Jedis(host, port);
if (!StringUtils.isBlank(password)) { jedis.auth(password); }
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<Entry<String, String>> scanResult = jedis.hscan(bigHashKey, cursor, scanParams);
List<Entry<String, String>> entryList = scanResult.getResult();
if (entryList != null && !entryList.isEmpty()) {
for (Entry<String, String> entry : entryList) { jedis.hdel(bigHashKey, entry.getKey()); }
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
jedis.del(bigHashKey); //删除bigkey
}
//2、List删除: ltrim
public void delBigList(String host, int port, String password, String bigListKey) {
Jedis jedis = new Jedis(host, port);
if (!StringUtils.isBlank(password)) { jedis.auth(password); }
long llen = jedis.llen(bigListKey);
int counter = 0, left = 100;
while (counter < llen) {
jedis.ltrim(bigListKey, left, llen); //每次从左侧截掉100个
counter += left;
}
jedis.del(bigListKey); //最终删除key
}
//3、Set删除: sscan + srem
public void delBigSet(String host, int port, String password, String bigSetKey) {
Jedis jedis = new Jedis(host, port);
if (!StringUtils.isBlank(password)) { jedis.auth(password); }
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<String> scanResult = jedis.sscan(bigSetKey, cursor, scanParams);
List<String> memberList = scanResult.getResult();
if (memberList != null && !memberList.isEmpty()) {
for (String member : memberList) { jedis.srem(bigSetKey, member); }
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
jedis.del(bigSetKey);//删除bigkey
}
//4、SortedSet删除: zscan + zrem
public void delBigZset(String host, int port, String password, String bigZsetKey) {
Jedis jedis = new Jedis(host, port);
if (!StringUtils.isBlank(password)) { jedis.auth(password); }
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<Tuple> scanResult = jedis.zscan(bigZsetKey, cursor, scanParams);
List<Tuple> tupleList = scanResult.getResult();
if (tupleList != null && !tupleList.isEmpty()) {
for (Tuple tuple : tupleList) { jedis.zrem(bigZsetKey, tuple.getElement()); }
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));
jedis.del(bigZsetKey); //删除bigkey
}
UNLINK
Redis 4.0.0已提供UNLINK命令异步删除大key
命令行:UNLINK key [key ...]
热点key搜索
命令行
redis-cli --hotkeys
monitor
redis-faina
---
纵使山重水复,亦会柳暗花明
sunqh1991@163.com
欢迎关注,互相交流