码迷,mamicode.com
首页 > 编程语言 > 详细

redis+spring

时间:2018-06-05 15:41:53      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:redis

redis.properties

#redis连接池配置参数
redis.pool.maxTotal=200
redis.pool.maxIdle=50
redis.pool.minIdle=10
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.pool.fairness=false
redis.pool.maxWaitMillis=20000
redis.pool.blockWhenExhausted=true

#格式:redis://:[密码]@[服务器地址]:[端口]/[db index]
redis.uri = redis://:12345@127.0.0.1:6379/0

redis.host = 172.28.1.239
redis.port = 20000
redis.timeout=30000
redis.password = 12345
redis.database = 0

spring-jedis.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"
       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">
     <!-- 引入jedis的properties配置文件 -->
    <!--如果你有多个数据源需要通过<context:property-placeholder管理,且不愿意放在一个配置文件里,那么一定要加上ignore-unresolvable=“true"-->
    <context:property-placeholder location="classpath:redis/redis.properties" ignore-unresolvable="true" />

        <!--shardedJedisPool的相关配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.pool.maxTotal}"/>
        <property name="maxIdle" value="${redis.pool.maxIdle}"/>
        <property name="minIdle" value="${redis.pool.minIdle}"/>
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
        <property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
        <property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
        <property name="fairness" value="${redis.pool.fairness}"/>
        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
    </bean>

    <!--集群redis配置-->
    <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.uri}" />
                </bean>
            </list>
        </constructor-arg>
    </bean>

    <!--单机redis配置-->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
        <constructor-arg name="host" value="${redis.host}" />
        <constructor-arg name="port" value="${redis.port}" type="int" />
        <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
        <!--<constructor-arg name="password" value="${redis.password}" />-->
        <!--<constructor-arg name="database" value="${redis.database}" type="int" />-->
    </bean>
</beans>

测试

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:redis/spring-jedis.xml");
        JedisPool jedisPool = (JedisPool) context.getBean("jedisPool");
        Jedis jedis = jedisPool.getResource();
        String setVal = jedis.set("key123", "value123");
        System.out.println(setVal);
        jedis.del("key123");
        // 事物操作
        Transaction multi = jedis.multi();
        multi.set("key2","val2");
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("name","ttest");
        hashMap.put("age","123");
        multi.hmset("hash1", hashMap);
        multi.exec();
    }

redis+spring

标签:redis

原文地址:http://blog.51cto.com/881206524/2125005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!