redis hget阻塞 使用redis时遇到的问题

发布时间:2017-03-20

来源:服务器之家

1,redis 报异常

redis.clients.jedis.exceptions.JedisDataException: WRONGTYPE Operation against a key holding the wrong kind of value

@Test

public void test_faildTime(){

String identify="13718486139";

int failedTime=WapController.getFailedCount(identify);

System.out.println(failedTime);

}

/***

* 获取失败次数

* 限制IP

* @param httpSession

* @param request

* @param response

* @return

*/

public static int getFailedCount(String identify) {

int count = 0;

String retryString = RedisHelper.getInstance().getKeyCache(identify, "failCount");

if(!StringUtil.isNullOrEmpty(retryString)) {

count = new Integer(retryString).intValue();

}

System.out.println("getFailedCount\tcount:"+count);

return count;

}

原因:jedis.hget(id, k)的第一个参数(id)是"13718486139"

奇怪的是换成"23718486139"就好了,真是诡异

2,保存时设置超时时间

调用的是Jedis类中的:

/**

* Set the string value as value of the key. The string can't be longer than 1073741824 bytes (1

* GB).

* @param key

* @param value

* @param nxxx NX|XX, NX -- Only set the key if it does not already exist. XX -- Only set the key

* if it already exist.

* @param expx EX|PX, expire time units: EX = seconds; PX = milliseconds

* @param time expire time in the units of {@param #expx}

* @return Status code reply

*/

public String set(final String key, final String value, final String nxxx, final String expx,

final long time) {

checkIsInMulti();

client.set(key, value, nxxx, expx, time);

return client.getStatusCodeReply();

}

封装之后:

/***

* Only set the key if it does not already exist

*

* @param k

* @param v

* @param time : second

*/

public void saveExpxKeyCache(String k, String v, long time) {

saveExpxKeyCache(k, v, "NX", time);

}

/***

* @param k

* @param v

* @param nxxx : NX|XX, NX -- Only set the key if it does not already exist. XX -- Only set the key

* if it already exist.

* @param time : second

*/

public void saveExpxKeyCache(String k, String v, String nxxx, long time) {

Jedis jedis = Const.pool.getResource();

try {

jedis.set(k, v, nxxx, "EX"/*seconds*/, time);

} catch (Exception e) {

e.printStackTrace();

logger.error("saveKeyCache", e);

Const.pool.returnBrokenResource(jedis);

jedis = null;

} finally {

if (jedis != null) {

Const.pool.returnResource(jedis);

}

}

}

3,应用

登录或者发送短信验证码,连续失败三次则弹出图形验证码

如何记录失败次数呢?

/***

* 获取失败次数

* 限制登录名(手机号或邮箱)

* @param httpSession

* @param request

* @param response

* @return

*/

public static int getFailedCount(String identify) {

int count = 0;

String retryString = RedisHelper.getInstance().getKeyCache(identify, "failCount");

if(!StringUtil.isNullOrEmpty(retryString)) {

count = new Integer(retryString).intValue();

}

System.out.println("getFailedCount\tcount:"+count);

return count;

}

/***

* 增加失败次数

* @param httpSession

* @param request

* @param response

*/

public static void increaseFailedCount(String identify) {

String retryString = RedisHelper.getInstance().getKeyCache(identify, "failCount");

int count=0;

if(!StringUtil.isNullOrEmpty(retryString)) {

count = new Integer(retryString).intValue();

}

count++;

System.out.println("increaseFailedCount\tcount:"+count);

RedisHelper.getInstance().saveKeyCache(identify, "failCount", String.valueOf(count));

}

/***

* 清空失败次数

* @param httpSession

* @param request

* @param response

*/

public static void clearFailedCount(String identify) {

RedisHelper.getInstance().clearKeyCache(identify, "failCount");

}

优化为:

/***

* 获取失败次数

* 限制登录名(手机号或邮箱)

* @param httpSession

* @param request

* @param response

* @return

*/

public static int getFailedCount(String identify) {

int count = 0;

String retryString = RedisHelper.getInstance().getCache("failCount"+identify);

if(!StringUtil.isNullOrEmpty(retryString)) {

count = new Integer(retryString).intValue();

}

System.out.println("getFailedCount\tcount:"+count);

return count;

}

/***

* 增加失败次数

* @param httpSession

* @param request

* @param response

*/

public static void increaseFailedCount(String identify) {

String retryString = RedisHelper.getInstance().getCache("failCount"+identify);

int count=0;

if(!StringUtil.isNullOrEmpty(retryString)) {

count = new Integer(retryString).intValue();

}

count++;

System.out.println("increaseFailedCount\tcount:"+count);

RedisHelper.getInstance().saveCache("failCount"+identify, String.valueOf(count));

}

/***

* 清空失败次数

* @param httpSession

* @param request

* @param response

*/

public static void clearFailedCount(String identify) {

RedisHelper.getInstance().clearCache("failCount"+identify);

}

hget和get redis_redis hget阻塞 使用redis时遇到的问题 - Redis - 服务器之家相关推荐

  1. zset获取指定score_redis zset更新score redis学习笔记5 - Redis - 服务器之家

    redis zset更新score redis学习笔记5 发布时间:2017-04-03 来源:服务器之家 一:概述 zset全称为sorted-sets类型,和set数据类型有极为相似,都是字符串的 ...

  2. redis查看某一个key的大小_redis查看某个key redis中key的操作命令 - Redis - 服务器之家...

    redis查看某个key redis中key的操作命令 发布时间:2017-04-02 来源:服务器之家 在该系列的前几篇博客中,主要讲述的是与Redis数据类型相关的命令,如String.List. ...

  3. linux 关闭redis 命令_linux关闭redis命令 redis配置redis的服务器启动和关闭 - Redis - 服务器之家...

    linux关闭redis命令 redis配置redis的服务器启动和关闭 发布时间:2017-04-13 来源:服务器之家 # chkconfig: 2345 10 90 # description: ...

  4. redis客户端连接数量_redis设置并发连接数 如何合理设置连接池的大小 - Redis - 服务器之家...

    redis设置并发连接数 如何合理设置连接池的大小 发布时间:2017-04-28 来源:服务器之家 先看几个问题,再看具体内容: 1) 为什么要合理设置连接池的大小 2) 服务器端的连接配置.最大允 ...

  5. 深入学习Redis(1):Redis内存模型

    前言 Redis是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说Redis是实现网站高并发不可或缺的一部分. 我们使用Redis时,会接触Redis的5种对象类型(字符串 ...

  6. 深入学习 Redis(1):Redis 内存模型

    前言 Redis是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说Redis是实现网站高并发不可或缺的一部分. 我们使用Redis时,会接触Redis的5种对象类型(字符串 ...

  7. linux怎么安装redis虚拟机,虚拟机linux安装redis实现过程解析

    安装步骤 1.先按照官网下方的weget命令把redis下载下来 接着如果没猜错,你会在make命令上出错 由于redis是C写的,所以需要装上c的运行环境 yum install gcc-c++ 接 ...

  8. redis 连接池_SpringBoot整合redis

    闲来没事,把之前自己搭建的SpringBoot集成Redis整理了一下,相信网上有很多,我只是写一下搭建的文章,能帮到就好,不能帮助,也是自己整理的文档罢了.文章开始: 开发工具用的IDEA 2018 ...

  9. redis专题:数据库和redis缓存一致性解决方案

    文章目录 1.双写模式 2.失效模式 3.缓存一致性解决方案 redis缓存和数据库都保存了数据信息,当我们更新了数据库的数据时,应该如何保证redis和数据库的数据同步呢?当前比较常用的是双写模式和 ...

  10. redis 数据类型详解 以及 redis适用场景场合

    redis 数据类型详解 以及 redis适用场景场合 1. MySql+Memcached架构的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访 ...

最新文章

  1. 这道「传说级」的数学题,为什么有 3 个正确答案?
  2. uva 1610 聚会游戏
  3. [T-ARA][Goodbye, OK]
  4. CodeDom Assistant CodeDom的强大工具, 有些BUG修正了下,发到CodePlex,大家有需要的可以看看...
  5. R语言-merge和rbind
  6. 转载 - LINUX下查看CPU使用率的命令
  7. backports移植rtlwifi驱动
  8. data=*(vu16*)addr;的理解?
  9. mysql 主从切换_mysql主从切换步骤
  10. cityscape 数据集 mmsegmentation训练记录
  11. 苹果手机升级13无法开机_苹果11更新ios13.7卡在开机页面
  12. AT32 MCU低功耗模式--AT_SURF案例19
  13. 华清远见22071作业端口指令实现灯点亮
  14. HTML中的语义化标签
  15. Pandas.Series的加减乘除数学运算
  16. 李一男离开华为时对下属说的话
  17. qq空间、微信好友、邮件、短信分享
  18. 自定义字体在HTML项目中的使用
  19. Java不定参数Object… obj 和 Object[] 的区别
  20. 基于安卓的移动应用开发

热门文章

  1. 信度spss怎么做_怎么用spss处理信度和效度?
  2. 分布式系统阅读笔记(十九)-----移动计算和无处不在的计算
  3. 《JavaScript 20 年》中文版之语言诞生
  4. 解决Chrome无法翻译此网页
  5. 使用谷歌浏览器自带的谷歌翻译提示“无法翻译此网页”
  6. QT widget宽高比
  7. Vue - 选择器拼音快速检索目标(pinyin-match)
  8. 随手笔记 -- 时间搜索框,默认搜索4天前至今天
  9. TI单节电量计基本介绍及常见问题解答
  10. python中match的六种用法_python re.match()用法相关示例