阿里官方 Redis 開發(fā)規(guī)范
本文主要介紹在使用阿里云 Redis 的開發(fā)規(guī)范,從下面幾個方面進(jìn)行說明。
鍵值設(shè)計 命令使用 客戶端使用 相關(guān)工具
一、鍵值設(shè)計
1、key 名設(shè)計
可讀性和可管理性
ugc:video:1簡潔性
user:{uid}:friends:messages:{mid}簡化為u:{uid}:fr:m:{mid}。不要包含特殊字符
2、value 設(shè)計
拒絕 bigkey
選擇適合的數(shù)據(jù)類型
set user:1:name tom
set user:1:age 19
set user:1:favor footballhmset user:1 name tom age 19 favor football控制 key 的生命周期
二、命令使用
1、O(N) 命令關(guān)注 N 的數(shù)量
2、禁用命令
3、合理使用 select
4、使用批量操作提高效率
原生命令:例如 mget、mset。 非原生命令:可以使用 pipeline 提高效率。
原生是原子操作,pipeline 是非原子操作。 pipeline 可以打包不同的命令,原生做不到 pipeline 需要客戶端和服務(wù)端同時支持。
5、不建議過多使用 Redis 事務(wù)功能
6、Redis 集群版本在使用 Lua 上有特殊要求
7、monitor 命令
三、客戶端使用
1、避免多個應(yīng)用使用一個 Redis 實例
2、使用連接池
執(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();
}3、熔斷功能
4、合理的加密
5、淘汰策略
allkeys-lru:根據(jù) LRU 算法刪除鍵,不管數(shù)據(jù)有沒有設(shè)置超時屬性,直到騰出足夠空間為止。 allkeys-random:隨機(jī)刪除所有鍵,直到騰出足夠空間為止。 volatile-random: 隨機(jī)刪除過期鍵,直到騰出足夠空間為止。 volatile-ttl:根據(jù)鍵值對象的 ttl 屬性,刪除最近將要過期數(shù)據(jù)。如果沒有,回退到 noeviction 策略。 noeviction:不會剔除任何數(shù)據(jù),拒絕所有寫入操作并返回客戶端錯誤信息 "(error) OOM command not allowed when used memory",此時 Redis 只響應(yīng)讀操作。
四、相關(guān)工具
1、數(shù)據(jù)同步
2、big key 搜索
3、熱點 key 尋找
五、刪除 bigkey
下面操作可以使用 pipeline 加速。 redis 4.0 已經(jīng)支持 key 的異步刪除,歡迎使用。
1、Hash 刪除: hscan + hdel
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);
}2、List 刪除: ltrim
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);
}3、Set 刪除: sscan + srem
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);
}4、SortedSet 刪除: zscan + zrem
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);
}胖虎聯(lián)合一線大廠朋友花費8個月的時間,錄制了一份Java入門+進(jìn)階視頻教程
課程特色:
總共88G,時常高達(dá)365小時,覆蓋所有主流技術(shù)棧
均為同一人錄制,不是東拼西湊的
對標(biāo)線下T0級別的培訓(xùn)課,講師大廠架構(gòu)師,多年授課經(jīng)驗,通俗易懂
內(nèi)容豐富,每一個技術(shù)點除了視頻,還有課堂源碼、筆記、PPT、圖解
五大實戰(zhàn)項目(視頻+源碼+筆記+SQL+軟件)
一次付費,持續(xù)更新,永無二次費用
點擊下方超鏈接查看詳情

(或者點擊文末閱讀原文):

評論
圖片
表情

