Jedis 设置key的超时时间

一分钟之内只能发送一次短信,

若用户刷新页面,然后输入原来的手机号,则继续计时

方案:服务器端要记录时间戳

方法名:sMSWaitingTime

功能:返回倒计时剩余时间,单位秒

 

Java代码   收藏代码
  1. /*** 
  2.      * 倒计时还剩余多长时间 
  3.      * @param mobile : 手机号 
  4.      * @return : second 
  5.      */  
  6.     public int sMSWaitingTime(String mobile) {  
  7.         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();  
  8.         HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();  
  9.         RedisHelper rdsHelper = RedisHelper.getInstance();  
  10.         String cid = getCid(request, response);  
  11.           
  12.         String lastSendSMSTime = rdsHelper.getCache(cid+mobile);//getKeyCache(request, response, mobile) ;  
  13.   
  14.         if(StringUtil.isNullOrEmpty(lastSendSMSTime)) {  
  15.             saveExpxKeyCache(request, response, mobile, String.valueOf(DateTimeUtil.getCurrentTimeSecond()),60);  
  16.             return 0;  
  17.         } else {  
  18.             long lastSendSMSTimeSecond=Long.parseLong(lastSendSMSTime);  
  19.             long currentTimeSecond=DateTimeUtil.getCurrentTimeSecond();  
  20.             int delter=(int) (currentTimeSecond-lastSendSMSTimeSecond);  
  21.             System.out.println("delter:"+delter);  
  22.             if(delter>=60){  
  23.                 return 0;  
  24.             }else{  
  25.                 return 60-delter;  
  26.             }  
  27.         }  
  28.     }  

 

jedis 可以设置key的超时时间:

Java代码   收藏代码
  1. public /*synchronized*/ void saveExpxKeyCache(HttpServletRequest request, HttpServletResponse response, String k, String v,long time) {  
  2.   
  3.         String cid = getCid(request, response);  
  4.   
  5. //        logger.debug("cid is " + cid);  
  6. //        logger.debug("set " + k + " to value : " + v);  
  7.   
  8.         RedisHelper helper = RedisHelper.getInstance();  
  9.         helper.saveExpxKeyCache(cid+k,  v,time);  
  10.   
  11. //        jedis.hset(httpSession.getId(), k, v);  
  12. //        jedis.expire(httpSession.getId(), 3600);  
  13.   
  14.   
  15.     }  
  16.   
  17.  /*** 
  18.      * Only set the key if it does not already exist 
  19.      * 
  20.      * @param k 
  21.      * @param v 
  22.      * @param time : second 
  23.      */  
  24.     public void saveExpxKeyCache(String k, String v, long time) {  
  25.         saveExpxKeyCache(k, v, "NX", time);  
  26.     }  
  27.   
  28.     /*** 
  29.      * @param k 
  30.      * @param v 
  31.      * @param nxxx :  NX|XX, NX -- Only set the key if it does not already exist. XX -- Only set the key 
  32.      *             if it already exist. 
  33.      * @param time : second 
  34.      */  
  35.     public void saveExpxKeyCache(String k, String v, String nxxx, long time) {  
  36.         Jedis jedis = Const.pool.getResource();  
  37.         try {  
  38.             jedis.set(k, v, nxxx, "EX"/*seconds*/, time);  
  39.         } catch (Exception e) {  
  40.             e.printStackTrace();  
  41.             logger.error("saveKeyCache", e);  
  42.             Const.pool.returnBrokenResource(jedis);  
  43.             jedis = null;  
  44.         } finally {  
  45.             if (jedis != null) {  
  46.                 Const.pool.returnResource(jedis);  
  47.             }  
  48.         }  
  49.     }  

 

 

参考:

http://hw1287789687.iteye.com/blog/2225130

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值