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); }
/*** * 获取失败次数<br> * 限制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,应用
登录或者发送短信验证码,连续失败三次则弹出图形验证码
如何记录失败次数呢?
/*** * 获取失败次数<br> * 限制登录名(手机号或邮箱) * @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"); }
优化为:
/*** * 获取失败次数<br> * 限制登录名(手机号或邮箱) * @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); }