ehcache实现验证码五分钟有效一分钟不重复发送

ehcache 配置


<!-- 缓存配置 
       name:缓存名称。 
       maxElementsInMemory:缓存最大个数。 
       eternal:对象是否永久有效,一但设置了,timeout将不起作用。 
       timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 
       timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 
       overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 
       diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 
       maxElementsOnDisk:硬盘最大缓存个数。 
       diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. 
       diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 
       memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 
       clearOnFlush:内存数量最大时是否清除。  -->
       
       <cache name="systemBaseCache" maxElementsInMemory="500"  maxElementsOnDisk="500" diskSpoolBufferSizeMB="5" 
			overflowToDisk="true" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LFU"/>
<defaultCache maxElementsInMemory="1000" overflowToDisk="true"
		eternal="false" memoryStoreEvictionPolicy="LRU" maxElementsOnDisk="10000"
		diskExpiryThreadIntervalSeconds="600" timeToIdleSeconds="120" 
		timeToLiveSeconds="120" diskPersistent="false" />

从上面的配置我们可以看出,ehcache设置失效时间五分钟,当放入元素超过五分钟,ehcache会根据自己的清理策略将失效元素清理。

后台代码

		//从缓存中获取验证码
		String oldCode = (String) cacheService.get(CacheServiceI.SYSTEM_BASE_CACHE, paras.getPhoneNum());
		//如果没有,则发送
		if (oldCode == null ) {
			sendCode(paras.getPhoneNum());
		}else {
			//获取上次验证码时间
			String oldCodeDate = (String) cacheService.get(CacheServiceI.SYSTEM_BASE_CACHE, paras.getPhoneNum()+SafetyTrainingUtils.codeDate);
			//比较短信发送时差,小于一分钟不允许再次发送
			if (DateUtils.getDatePoor('s', new Date(),DateUtils.datetimeFormat.parse(oldCodeDate) )>60) {
				sendCode(paras.getPhoneNum());
			}else {
				j.setMsg(AppMsgUtil.refuse_send_code);
			}
		}
		
private void sendCode(String phoneNum){
		String code = SendMessageUtil.code();
		String msg=SafetyTrainingUtils.messageTemplate.replace("{code}", code);
		String sendNote = SendMessageUtil.sendNote(phoneNum, msg);
		//加入缓存
		cacheService.put(CacheServiceI.SYSTEM_BASE_CACHE, phoneNum,code);
		cacheService.put(CacheServiceI.SYSTEM_BASE_CACHE, phoneNum+SafetyTrainingUtils.codeDate,DateUtils.datetimeFormat.format(new Date()));
		
	}

逻辑如下:当用户请求获取验证码,从缓存中根据手机号为key取验证码,如果没有,则发送验证码,如果有,根据手机号+自定义key取出验证码的放入时间,判断时间是否大于一分钟,如果大于一分钟则发送,反之不发送。在发送验证码的时候将验证码和时间同时存入缓存。

附计算两个时间差代码

/**
	 * 计算两个日期的差
	 * @param endDate
	 * @param nowDate
	 * @return
	 * @description
	 * @author fenglu
	 * @date 2019年7月2日 下午3:40:35
	 */
	public static int getDatePoor(char flag, Date endDate, Date nowDate) {
		long millisDiff = endDate.getTime() - nowDate.getTime();
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	   //差多少天
		if (flag == 'd') {
			return (int) (millisDiff / DAY_IN_MILLIS);
		}
		//小时差
		if (flag == 'h') {
			return (int) (millisDiff / HOUR_IN_MILLIS);
		}
		//分钟差
		if (flag == 'm') {
			return (int) (millisDiff / MINUTE_IN_MILLIS);
		}
		//秒差
		if (flag == 's') {
			return (int) (millisDiff / SECOND_IN_MILLIS);
		}
		return 0;

	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    //long diff = endDate.getTime() - nowDate.getTime();
	    // 计算差多少天
	    //long day = diff / nd;
	    // 计算差多少小时
	    //long hour = diff % nd / nh;
	    // 计算差多少分钟
	    //long min = diff % nd % nh / nm;
	    // 计算差多少秒//输出结果
	    // long sec = diff % nd % nh % nm / ns;
	    //return day + "天" + hour + "小时" + min + "分钟";
	}

短信发送功能暂无

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值