Redis 验证码读取

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)return lastSendTime;
    }
    return null;
}

}

你可能感兴趣的:(Redis)