加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

一份完整的阿里云Redis开发规范,值得收藏!

发布时间:2019-04-16 01:49:34 所属栏目:MySql教程 来源:付磊-起扬
导读:副标题#e# 本文主要介绍在使用阿里云Redis的开发规范,从下面几个方面进行说明。 键值设计 命令使用 客户端使用 相关工具 通过本文的介绍可以减少使用Redis过程带来的问题。 一、键值设计 1、key名设计 可读性和可管理性 以业务名(或数据库名)为前缀(防止ke

其他策略如下:

  •  allkeys-lru:根据LRU算法删除键,不管数据有没有设置超时属性,直到腾出足够空间为止。
  •  allkeys-random:随机删除所有键,直到腾出足够空间为止。
  •  volatile-random:随机删除过期键,直到腾出足够空间为止。
  •  volatile-ttl:根据键值对象的ttl属性,删除最近将要过期数据。如果没有,回退到noeviction策略。
  •  noeviction:不会剔除任何数据,拒绝所有写入操作并返回客户端错误信息"(error) OOM command not allowed when used memory",此时Redis只响应读操作。

四、相关工具

1、数据同步

redis间数据同步可以使用:redis-port

2、big key搜索

redis大key搜索工具

3、热点key寻找

内部实现使用monitor,所以建议短时间使用facebook的redis-faina

阿里云Redis已经在内核层面解决热点key问题

五、删除bigkey

  1.  下面操作可以使用pipeline加速。
  2.  redis 4.0已经支持key的异步删除,欢迎使用。

1、Hash删除: hscan + hdel

  1. public void delBigHash(String host, int port, String password, String bigHashKey) {  
  2.     Jedis jedis = new Jedis(host, port);  
  3.     if (password != null && !"".equals(password)) {  
  4.         jedis.auth(password);  
  5.     }  
  6.     ScanParams scanParams = new ScanParams().count(100);  
  7.     String cursor = "0";  
  8.     do {  
  9.         ScanResult<Entry<String, String>> scanResult = jedis.hscan(bigHashKey, cursor, scanParams);  
  10.         List<Entry<String, String>> entryList = scanResult.getResult();  
  11.         if (entryList != null && !entryList.isEmpty()) {  
  12.             for (Entry<String, String> entry : entryList) {  
  13.                 jedis.hdel(bigHashKey, entry.getKey());  
  14.             }  
  15.         }  
  16.         cursor = scanResult.getStringCursor();  
  17.     } while (!"0".equals(cursor));  
  18.     //删除bigkey  
  19.     jedis.del(bigHashKey);  

2、List删除: ltrim

  1. public void delBigList(String host, int port, String password, String bigListKey) {  
  2.     Jedis jedis = new Jedis(host, port);  
  3.     if (password != null && !"".equals(password)) {  
  4.         jedis.auth(password);  
  5.     }  
  6.     long llen = jedis.llen(bigListKey);  
  7.     int counter = 0;  
  8.     int left = 100;  
  9.     while (counter < llen) {  
  10.         //每次从左侧截掉100个  
  11.         jedis.ltrim(bigListKey, left, llen);  
  12.         counter += left;  
  13.     }  
  14.     //最终删除key  
  15.     jedis.del(bigListKey);  

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读