redis的学习知识点

redis安装参考文档:
 
redis的介绍:
 
 
 
redis中命令行窗口列出所有key的命令:redis-cli keys  '*'    //keys和*之间是有一个空格的,亲~~~一定要注意哦!

配置文件:
data-source.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:property-placeholder location="classpath:spring/redis.properties"/>
    <context:component-scan base-package="com.bestpay.redis">
    </context:component-scan>
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxActive" value="50"/>
        <property name="maxIdle" value="8"/>
        <property name="maxWait" value="1000"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
        <!-- <property name="testWhileIdle" value="true"/> -->
    </bean>
    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
        <constructor-arg index="0" ref="jedisPoolConfig"/>
        <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg name="host" value="${redis.host}"/>
                    <constructor-arg name="port" value="${redis.port}"/>
                    <constructor-arg name="timeout" value="${redis.timeout}"/>
                    <constructor-arg name="weight" value="1"/>
                </bean>
      ............................... 配置多个redis服务器(保证redis服务的正常提供)
 <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg name="host" value="${redis.host}"/>
                    <constructor-arg name="port" value="${redis.port}"/>
                    <constructor-arg name="timeout" value="${redis.timeout}"/>
                    <constructor-arg name="weight" value="1"/>
  </bean>

            </list>
        </constructor-arg>
    </bean>
</beans>

redis.properties:

 

redis.host=127.0.0.1
redis.port=6379
redis.pass=foobared
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=300
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;
import redis.clients.jedis.ShardedJedis;
import java.util.Scanner;

@Repository("redisClientTemplate")
public class RedisClientTemplate {
private static final Logger log = LoggerFactory.getLogger(RedisClientTemplate.class);
private final static int seconds = 1 * 60; //超时失效时间:5秒钟
@Autowired
private RedisDataSource redisDataSource;
static RedisClientTemplate redisClient = null;
static{
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/data-source.xml");
redisClient = (RedisClientTemplate)ac.getBean("redisClientTemplate");
}
public void disconnect() {
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
shardedJedis.disconnect();
}
/**
* 设置单个值
* @param key
* @param value
* @return
*/
public String set(String key ,String value){
String result = null;
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean broken = false;
try {
result = shardedJedis.set(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
broken = true;
} finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 获取单个值
*
* @param key
* @return
*/
public String get(String key) {
String result = null;
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean broken = false;
try {
result = shardedJedis.get(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
broken = true;
} finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 在某段时间后实现
*
* @param key
* @param seconds
* @return
*/
public Long expire(String key, int seconds) {
Long result = null;
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean broken = false;
try {
result = shardedJedis.expire(key, seconds);
} catch (Exception e) {
log.error(e.getMessage(), e);
broken = true;
} finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 判断一个KEY是否存在
* @param key
* @return
*/
public boolean exists(String key) {
boolean result = false;
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
if (shardedJedis == null) {
return false;
}
boolean broken = false;
try {
result = shardedJedis.exists(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
broken = true;
} finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
* 获得生存时间
* @param key
* @return
*/
public Long tes(String key) {
Long result = null;
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean broken = false;
try {
result = shardedJedis.tes(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
broken = true;
} finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}

public static void main(String[] args) throws Exception {
// redisClient.saveValue2Redis("authCodeKey","123456");
// System.out.println("5秒钟之前;"+redisClient.1getRedisValue("bbb"));
// Thread.sleep(6000);
// System.out.println("5秒钟之后;"+redisClient.getRedisValue("bbb"));
while(true) {
System.out.println("请输入验证码...");
Scanner scanner = new Scanner(System.in);
String authCode = scanner.nextLine();
boolean flg = isLockedCardByAuthCode(authCode,"authCodeKey","1111111111");
if(flg){
log.debug("您的验证码正确,请继续操作后续流程");
break;
}else{
log.debug("您的验证码有问题,或者卡号被锁定");
}
}
}
/**
*
* @param key
* @param value
* @return
*/
public void saveValue2Redis(String key,String value){
redisClient.set(key, value);
redisClient.expire(key,seconds);
}
public void saveValue2Redis(String key,String value,int time ){
redisClient.set(key, value);
redisClient.expire(key,time);
}
/**
*
* @param key
* @return
*/
public String getRedisValue(String key) {
boolean exists =redisClient.exists(key);
if(exists) {
//log.debug("key对应的value未失效");
return redisClient.get(key);
}else{
//log.debug("key对应的value已失效");
return null;
}
}
/**
* 删除key值
* @param key
*/
public Long delKey(String key) {
Long result = null;
ShardedJedis shardedJedis = redisDataSource.getRedisClient();
if (shardedJedis == null) {
return result;
}
boolean broken = false;
try {
result = shardedJedis.del(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
broken = true;
} finally {
redisDataSource.returnResource(shardedJedis, broken);
}
return result;
}
/**
*
* @param authCode
* @return
*/
public static boolean isLockedCardByAuthCode(String authCode,String authKey,String cardNo){
log.debug("输入验证码;{}",authCode);
String redisAuthCode = redisClient.getRedisValue(authKey);
log.debug("redis验证码;{}",redisAuthCode);
if(redisAuthCode==null){
log.debug("验证码失效");
return false;
}
//判断此卡是否被锁定了
String isExistLockedCard = redisClient.get(cardNo.substring(cardNo.length()-4));
if(isExistLockedCard!=null) {
log.debug("此卡:{},已经被锁定,请在1分钟之后再做支付操作",cardNo);
return false;
}
if(!redisAuthCode.equals(authCode)) {
String cardNoTimes = redisClient.getRedisValue(cardNo);
int times = (cardNoTimes==null? 0:Integer.parseInt(redisClient.getRedisValue(cardNo)));
if(times != 0) {
if(times >= 5) {
log.debug("输入验证码错误次数超过5次,当前次数:{}",times+1);
log.debug("锁定此卡1分钟,卡号:{}:",cardNo);
//用卡号后4位做为存储被锁定卡的key
redisClient.saveValue2Redis(cardNo.substring(cardNo.length()-4),cardNo,1);
return false;
}
log.debug("累计验证码错误次数:{},卡号:{}",String.valueOf(times+1),cardNo);
//用卡号做为存储被锁定卡的key
redisClient.saveValue2Redis(cardNo,String.valueOf(times+1));
return false;
}else{
log.debug("首次输入验证码错误,开始次数入库");
//用卡号做为存储被锁定卡的key
redisClient.saveValue2Redis(cardNo,"1");
return false;
}
}else{
log.debug("验证码正确,通过");
redisClient.delKey(cardNo.substring(cardNo.length()-4));
redisClient.delKey(cardNo);
}
return true;
}

 

自我步骤,欢迎拍砖!

 
 
posted @ 2015-10-28 11:19  ron_seekers  阅读(193)  评论(0编辑  收藏  举报