Spring整合Redis作为缓存

jar包准备:jedis-2.8.0.jar

redis.properties配置文件

#redis中心 
redis.host=127.0.0.1
redis.port=6379
redis.password=root
redis.maxIdle=100
redis.maxActive=300
redis.maxWait=1000
redis.minIdle=50
redis.testOnBorrow=true
redis.timeout=100000
 redis.maxTotal=100

app-context-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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
       http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
    <!-- properties config -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:/com/springmvc/config/redis.properties</value>
            </list>
        </property>
    </bean>
    <!-- Redis配置 -->
    <!-- redis连接池的配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal">
            <value>${redis.maxTotal}</value>
        </property>
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="minIdle" value="${redis.minIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
        <!-- <property name="testOnReturn" value="${redis.testOnReturn}"/> -->
    </bean>
    <!-- redis的连接池pool,不是必选项:timeout/password -->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1" value="${redis.host}" />
        <constructor-arg index="2" value="${redis.port}" type="int" />
        <constructor-arg index="3" value="${redis.timeout}" type="int" />
        <!-- <constructor-arg index="4" value="${redis.password}"/> -->
    </bean>
</beans>

测试:

/*
 * 文件名:RedisTest.java
 * 版权:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述: RedisTest.java
 * 修改人:peiyu
 * 修改时间:2016年8月3日
 * 修改内容:新增
 */
package com.springmvc.imooc.test;

import java.io.Serializable;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import sun.tools.jar.resources.jar;

/**
 * 
 * @author peiyu
 */
public class RedisTest extends BaseTest {
    private ApplicationContext app;
    private JedisPool jedisPool;
    @Before
    public void init() {
        app = new ClassPathXmlApplicationContext("com/springmvc/config/app-context.xml");
        jedisPool=(JedisPool) app.getBean("jedisPool");
    }
    @Test
    public void test() {
        Jedis resource = jedisPool.getResource();
        System.out.println("set前name=" + resource.get("name"));
        resource.set("name", "zhangsan");
        System.out.println("set后name=" + resource.get("name"));
        resource.del("name");
        System.out.println("delete后name=" + resource.get("name"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值