赞
踩
import java.util.Date;
import javax.annotation.Resource;
import org.apache.commons.lang3.time.DateUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class OTPRedisImpl implements OTPRedis {
@Resource
private JedisPool jedisPool;
private final static int JEDIS_DATABASE=11;
private final static long VALID_PERIOD=60*5;
@Override
public void saveValue(String key, String value) {
delValue(key);
try (Jedis jedis = jedisPool.getResource()) {
jedis.select(JEDIS_DATABASE);
jedis.set(key.getBytes(), value.getBytes(), "nx".getBytes(), "EX".getBytes(),
VALID_PERIOD);
}
}
@Override
public String getValue(String key) {
try (Jedis jedis = jedisPool.getResource()) {
jedis.select(JEDIS_DATABASE);
String value = jedis.get(key);
return value;
}
}
@Override
public void delValue(String key) {
try (Jedis jedis = jedisPool.getResource()) {
jedis.select(JEDIS_DATABASE);
jedis.del(key.getBytes());
}
}
@Override
public Long checkRepeat(String key) {
String value = getValue(key);
if(value==null){
return null;
}
Long lastSendTime=Long.parseLong(value.split("-")[1]);
if((new Date().getTime()-lastSendTime)<DateUtils.MILLIS_PER_MINUTE){
return lastSendTime;
}
return null;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。