使用redis时遇到的问题

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.     }  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值