spring集成jedis

一、导入包

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

二、配置参数及含义

 
 
redis.host = 127.0.0.1 ( reids服务器ip
redis.port = 6379reids服务器端口 redis.maxIdle = 10 最大保持空闲状态的连接数 redis.maxActive = 500最大连接数 redis.maxWait = 1000000池内没有连接时最大等待时间 redis.testOnBorrow = true调用borrowObject方法时是否有效性检查 redis.testOnReturn = true调用returnObject方法时是否有效性检查 redis.testWhileIdle = true空闲时检查有效性 redis.timeout =3000 0连接超时时间 redis.password = 12345reids服务器密码

三、连接池配置

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--读取配置项信息 -->
    <context:property-placeholder location="classpath:*.properties" file-encoding="UTF-8" />
   
   <!-- 配置连接池 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxTotal" value="${redis.maxActive}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="${redis.testOnReturn}" />
    </bean>
    <bean id="pool" class="redis.clients.jedis.JedisPool">
        <constructor-arg ref="poolConfig" />
        <constructor-arg value="${redis.host}" />
        <constructor-arg type="int" value="${redis.port}" />
        <constructor-arg type="int" value="${redis.timeout}" />
        <constructor-arg value="${redis.password}" />
    </bean>

    <bean id="redisCachePlugin" class="com.zgl.test.rs.cache.RedisCachePlugin" scope="prototype">
        <property name="pool" ref="pool"></property>
     </bean>

</beans>

四、代码示例

1、获取连接

/**
 * 从连接池获取链接
 *
 * @return
 */
private Jedis getConnect() {
    Jedis jedis = null;
    try {
        jedis = pool.getResource();
    } catch (Exception ex) {
        log.error("jedis get resource from pool throw error :", ex);
    }
    return jedis;
}

2、设置缓存内容

/**
 * Description: 缓存内容为字符串形式的缓存内容设置
 * 
 * @param key
 * @param value
 * @param isCover 已存在键值对时是否覆盖
 */
public void set(String key, String value, boolean isCover) {
    Jedis jedis = this.getConnect();
    try {
        if (isCover) {
            jedis.set(key, value);
        } else {
            jedis.setnx(key, value);
        }
    } catch (Exception ex) {
        log.error("set#Key:" + key + ",Value:" + value + ",isCover:" + isCover);
    } finally {
        jedis.close();
    }
}

3、获取缓存内容

/**
 * Description: 获取字符串缓存内容,如果key不存在则返回null
 * 
 * @param key
 * @return 缓存字符串内容
 */
public String get(String key) {
    Jedis jedis = this.getConnect();
    try {
        return jedis.get(key);
    } catch (Exception ex) {
        log.error("get#key:" + key);
        return null;
    } finally {
        jedis.close();
    }
}

4、通过key删除缓存

/**
 * Description: 删除指定key的缓存字符串
 * 
 * @param key
 */
public boolean delete(String key) {
    Jedis jedis = this.getConnect();
    try {
        jedis.del(key);
        return true;
    } catch (Exception ex) {
        log.error("delete#Key:" + key);
        return false;
    } finally {
        jedis.close();
    }
}


猜你喜欢

转载自blog.csdn.net/long2010110/article/details/80706429