使用redis时遇到的问题

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

1,redis 报异常

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

 

Java代码   收藏代码
  1. @Test  
  2.     public void test_faildTime(){  
  3.         String identify="13718486139";  
  4.         int failedTime=WapController.getFailedCount(identify);  
  5.         System.out.println(failedTime);  
  6.     }  

 

 

 

Java代码   收藏代码
  1. /*** 
  2.      * 获取失败次数<br> 
  3.      * 限制IP 
  4.      * @param httpSession 
  5.      * @param request 
  6.      * @param response 
  7.      * @return 
  8.      */  
  9.     public static int getFailedCount(String identify) {  
  10.         int count = 0;  
  11.         String retryString = RedisHelper.getInstance().getKeyCache(identify, "failCount");    
  12.   
  13.         if(!StringUtil.isNullOrEmpty(retryString)) {  
  14.             count = new Integer(retryString).intValue();  
  15.         }  
  16.         System.out.println("getFailedCount\tcount:"+count);  
  17.         return count;  
  18.     }  

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

 

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

 

 

2,保存时设置超时时间

调用的是Jedis类中的:

 

Java代码   收藏代码
  1. /** 
  2.    * Set the string value as value of the key. The string can't be longer than 1073741824 bytes (1 
  3.    * GB). 
  4.    * @param key 
  5.    * @param value 
  6.    * @param nxxx NX|XX, NX -- Only set the key if it does not already exist. XX -- Only set the key 
  7.    *          if it already exist. 
  8.    * @param expx EX|PX, expire time units: EX = seconds; PX = milliseconds 
  9.    * @param time expire time in the units of {@param #expx} 
  10.    * @return Status code reply 
  11.    */  
  12.   public String set(final String key, final String value, final String nxxx, final String expx,  
  13.       final long time) {  
  14.     checkIsInMulti();  
  15.     client.set(key, value, nxxx, expx, time);  
  16.     return client.getStatusCodeReply();  
  17.   }  

 

 

封装之后:

 

Java代码   收藏代码
  1. /*** 
  2.      * Only set the key if it does not already exist 
  3.      * 
  4.      * @param k 
  5.      * @param v 
  6.      * @param time : second 
  7.      */  
  8.     public void saveExpxKeyCache(String k, String v, long time) {  
  9.         saveExpxKeyCache(k, v, "NX", time);  
  10.     }  
  11.   
  12.     /*** 
  13.      * @param k 
  14.      * @param v 
  15.      * @param nxxx :  NX|XX, NX -- Only set the key if it does not already exist. XX -- Only set the key 
  16.      *             if it already exist. 
  17.      * @param time : second 
  18.      */  
  19.     public void saveExpxKeyCache(String k, String v, String nxxx, long time) {  
  20.         Jedis jedis = Const.pool.getResource();  
  21.         try {  
  22.             jedis.set(k, v, nxxx, "EX"/*seconds*/, time);  
  23.         } catch (Exception e) {  
  24.             e.printStackTrace();  
  25.             logger.error("saveKeyCache", e);  
  26.             Const.pool.returnBrokenResource(jedis);  
  27.             jedis = null;  
  28.         } finally {  
  29.             if (jedis != null) {  
  30.                 Const.pool.returnResource(jedis);  
  31.             }  
  32.         }  
  33.     }  

 

 

 

 

3,应用

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

如何记录失败次数呢?

 

Java代码   收藏代码
  1. /*** 
  2.  * 获取失败次数<br> 
  3.  * 限制登录名(手机号或邮箱) 
  4.  * @param httpSession 
  5.  * @param request 
  6.  * @param response 
  7.  * @return 
  8.  */  
  9. public static int getFailedCount(String identify) {  
  10.     int count = 0;  
  11.     String retryString = RedisHelper.getInstance().getKeyCache(identify, "failCount");    
  12.   
  13.     if(!StringUtil.isNullOrEmpty(retryString)) {  
  14.         count = new Integer(retryString).intValue();  
  15.     }  
  16.     System.out.println("getFailedCount\tcount:"+count);  
  17.     return count;  
  18. }  
  19.   
  20. /*** 
  21.  * 增加失败次数 
  22.  * @param httpSession 
  23.  * @param request 
  24.  * @param response 
  25.  */  
  26. public static void increaseFailedCount(String identify) {  
  27.     String retryString = RedisHelper.getInstance().getKeyCache(identify, "failCount");    
  28.     int count=0;  
  29.     if(!StringUtil.isNullOrEmpty(retryString)) {  
  30.         count = new Integer(retryString).intValue();  
  31.     }  
  32.     count++;  
  33.     System.out.println("increaseFailedCount\tcount:"+count);  
  34.     RedisHelper.getInstance().saveKeyCache(identify, "failCount", String.valueOf(count));  
  35. }  
  36.   
  37. /*** 
  38.  * 清空失败次数 
  39.  * @param httpSession 
  40.  * @param request 
  41.  * @param response 
  42.  */  
  43. public static void clearFailedCount(String identify) {  
  44.     RedisHelper.getInstance().clearKeyCache(identify, "failCount");  
  45. }  

 

 

优化为:

Java代码   收藏代码
  1. /*** 
  2.      * 获取失败次数<br> 
  3.      * 限制登录名(手机号或邮箱) 
  4.      * @param httpSession 
  5.      * @param request 
  6.      * @param response 
  7.      * @return 
  8.      */  
  9.     public static int getFailedCount(String identify) {  
  10.         int count = 0;  
  11.         String retryString = RedisHelper.getInstance().getCache("failCount"+identify);    
  12.   
  13.         if(!StringUtil.isNullOrEmpty(retryString)) {  
  14.             count = new Integer(retryString).intValue();  
  15.         }  
  16.         System.out.println("getFailedCount\tcount:"+count);  
  17.         return count;  
  18.     }  
  19.   
  20.     /*** 
  21.      * 增加失败次数 
  22.      * @param httpSession 
  23.      * @param request 
  24.      * @param response 
  25.      */  
  26.     public static void increaseFailedCount(String identify) {  
  27.         String retryString = RedisHelper.getInstance().getCache("failCount"+identify);    
  28.         int count=0;  
  29.         if(!StringUtil.isNullOrEmpty(retryString)) {  
  30.             count = new Integer(retryString).intValue();  
  31.         }  
  32.         count++;  
  33.         System.out.println("increaseFailedCount\tcount:"+count);  
  34.         RedisHelper.getInstance().saveCache("failCount"+identify, String.valueOf(count));  
  35.     }  
  36.   
  37.     /*** 
  38.      * 清空失败次数 
  39.      * @param httpSession 
  40.      * @param request 
  41.      * @param response 
  42.      */  
  43.     public static void clearFailedCount(String identify) {  
  44.         RedisHelper.getInstance().clearCache("failCount"+identify);  
  45.     }  

 

 

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
2月前
|
缓存 NoSQL Redis
Redis雪崩问题
Redis雪崩问题
27 1
|
1月前
|
存储 SQL NoSQL
[Redis]——初识Redis
[Redis]——初识Redis
|
8月前
|
存储 NoSQL Java
Redis3
Redis3
38 0
|
6月前
|
消息中间件 缓存 NoSQL
Redis总结
Redis总结
21 0
|
7月前
|
缓存 NoSQL 关系型数据库
|
7月前
|
存储 NoSQL 算法
|
7月前
|
NoSQL Redis
|
7月前
|
NoSQL API Redis
Redis
Redis 是一个开源的、支持网络、可基于内存亦可持久化的日志型、key-value 数据库,它支持多种数据类型,如字符串(string)、哈希(hash)、列表(list)、集合(set)和有序集合(sorted set)等。Redis 提供了多种语言的 API,通常被称为数据结构服务器。
73 0
|
11月前
|
存储 消息中间件 NoSQL
|
消息中间件 缓存 NoSQL
Redis还可以做哪些事?
Redis还可以做哪些事?
76 0