阿里官方 Redis 開發(fā)規(guī)范
大家好,我是寶哥!
鍵值設(shè)計 命令使用 客戶端使用 相關(guān)工具
1
ugc:video:1
簡潔性
user:{uid}:friends:messages:{mid}簡化為u:{uid}:fr:m:{mid}set user:1:name tom
set user:1:age 19
set user:1:favor footballhmset user:1 name tom age 19 favor football
2
原生命令:例如 mget、mset。 非原生命令:可以使用 pipeline 提高效率。
原生是原子操作,pipeline 是非原子操作。 pipeline 可以打包不同的命令,原生做不到 pipeline 需要客戶端和服務(wù)端同時支持。
3
執(zhí)行命令如下:
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//具體的命令
jedis.executeCommand()
} catch (Exception e) {
logger.error("op key {} error: " + e.getMessage(), key, e);
} finally {
//注意這里不是關(guān)閉連接,在JedisPool模式下,Jedis會被歸還給資源池。
if (jedis != null)
jedis.close();
}allkeys-lru:根據(jù) LRU 算法刪除鍵,不管數(shù)據(jù)有沒有設(shè)置超時屬性,直到騰出足夠空間為止。 allkeys-random:隨機刪除所有鍵,直到騰出足夠空間為止。 volatile-random: 隨機刪除過期鍵,直到騰出足夠空間為止。 volatile-ttl:根據(jù)鍵值對象的 ttl 屬性,刪除最近將要過期數(shù)據(jù)。如果沒有,回退到 noeviction 策略。 noeviction:不會剔除任何數(shù)據(jù),拒絕所有寫入操作并返回客戶端錯誤信息 "(error) OOM command not allowed when used memory",此時 Redis 只響應(yīng)讀操作。
4
5
下面操作可以使用 pipeline 加速。 redis 4.0 已經(jīng)支持 key 的異步刪除,歡迎使用。
public void delBigHash(String host, int port, String password, String bigHashKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(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));
//刪除bigkey
jedis.del(bigHashKey);
}public void delBigList(String host, int port, String password, String bigListKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
long llen = jedis.llen(bigListKey);
int counter = 0;
int left = 100;
while (counter < llen) {
//每次從左側(cè)截掉100個
jedis.ltrim(bigListKey, left, llen);
counter += left;
}
//最終刪除key
jedis.del(bigListKey);
}public void delBigSet(String host, int port, String password, String bigSetKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(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));
//刪除bigkey
jedis.del(bigSetKey);
}public void delBigZset(String host, int port, String password, String bigZsetKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(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));
//刪除bigkey
jedis.del(bigZsetKey);
}來源:yq.aliyun.com/articles/531067
參考資料
Release: https://github.com/zu1k/nali/releases
精彩推薦:
基于 SpringBoot + MyBatis-Plus 的公眾號管理系統(tǒng)
SpringBoot+ Dubbo + Mybatis + Nacos +Seata整合來實現(xiàn)Dubbo分布式事務(wù)
SpringBoot 啟動時自動執(zhí)行代碼的幾種方式,還有誰不會??
史上最全的 IDEA Debug 調(diào)試技巧(超詳細案例)
評論
圖片
表情
