Redis测试

1.Test类

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisTest {
    static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath:spring-context.xml");
    static ShardedJedisPool pool;
    static ShardedJedis jedis;
    static {
        context.start();
        pool = (ShardedJedisPool) context.getBean("shardedJedisPool");
        jedis = pool.getResource();
    }
    /**
     * 添加数据
     */
    @Test
    public void testAdd() {
        jedis.set("1", "222");
        System.out.println(jedis.get("1"));
    }
    /**
     * 追加数据
     */
    @Test
    public void testAppend() {
        jedis.append("1", "333");
        System.out.println(jedis.get("1"));
    }
    /**
     * 修改数据
     */
    @Test
    public void testUpdate()
    {       
        jedis.set("1", "444");
        System.out.println(jedis.get("1"));
    }
    @Test
    public void testDel()
    {
        jedis.del("1");
        System.out.println(jedis.get("1"));     
    }



}

spring-context配置

<?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" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
    default-autowire="byName" default-lazy-init="false">

    <!-- 采用注释的方式配置bean -->
    <context:annotation-config />


    <!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->
    <aop:aspectj-autoproxy proxy-target-class="true" /> 

    <import resource="spring-redis.xml" />
</beans>

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

    <!-- Jedis链接池配置 -->

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="testWhileIdle" value="true" />
        <property name="minEvictableIdleTimeMillis" value="60000" />
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <property name="numTestsPerEvictionRun" value="-1" />
        <property name="maxTotal" value="8" />
        <property name="maxIdle" value="8" />
        <property name="minIdle" value="0" />
    </bean>

    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg index="0" value="127.0.0.1" />
                    <constructor-arg index="1" value="6379" type="int" />
                </bean>
            </list>
        </constructor-arg>
    </bean>







</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值