Srping使用缓存机制整合redis单机版

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38111957/article/details/82714705

一、引言

之前几篇文章,讲解了redis五种数据类型的常用方法。是实话确实有点枯燥,而且每一种的数据类型的方法都有点相似,容易混淆。那么今天我们来点干货吧,使用单机版的redis整合spring框架。在我们实际开发调试的过程中,一般都是使用redis单机版的,在上线的时候,只需要切换成redis集群版。集群咱们后面在说,今天来看看spring整合

二、使用Jedis客户端连接

在整合spring之前,我们要确保我们redis能使用java进行连接,保证我们的redis服务没问题。

java是使用Jedis客户端来连接我们redis,所以我们得引入jar先。

    <!-- Redis客户端 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.2</version>
        </dependency>

以下方法测试redis是否能够正常访问,如果能够正常返回redis中的数据,则表示redis能够正常使用。

  /**
     * 普通连接redis方法
     */
    @Test
    public void testJedisClient(){
        //创建jedis对象
        Jedis jedis = new Jedis("111.231.110.81",6379);
        //输入redis连接密码,没有密码可忽略
        //jedis.auth("123456");
        //调用jedis常用方法,方法名和redis命令一致
        String str = jedis.get("name");
        System.out.println(str);
        //关闭jedis
        jedis.close();
    }

    /**
     * 使用常量池连接
     */
    @Test
    public void testJedisClientPool(){
        //创建jedis连接池
        JedisPool pool = new JedisPool("111.231.51.81",6379);
        //获取jedis对象
        Jedis jedis = pool.getResource();
        //输入redis连接密码
        //jedis.auth("123456");
        //调用redis常用方法
        String str = jedis.get("key1");
        System.out.println(str);
        //关闭jedis对象以及连接池
        jedis.close();
        pool.close();
    }

三、整合srping

1、创建redis的配置文件applicationContext-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   <!--连接池配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大连接数 -->
        <property name="maxTotal" value="100" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="50" />
        <!-- 每次释放连接的最大数目 -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- 释放连接的扫描间隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- 连接最小空闲时间 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- 在获取连接的时候检查有效性, 默认false -->
        <property name="testOnBorrow" value="true" />
        <!-- 在空闲时检查有效性, 默认false -->
        <property name="testWhileIdle" value="true" />
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="false" />
    </bean>


    <!-- reids baen 注入-->
    <bean id="redisClient" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="111.231.110.81"></constructor-arg>
        <constructor-arg name="port" value="6379"></constructor-arg>
        <!--这里一般使用默认配置也行,如果有特殊需要,可以自己自定义配置-->
       <!-- <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg> -->
    </bean>

</beans>

2、测试配置文件是否正常,以上配置文件,将我们的JedisPool对象交给了Spring容器处理,我们在项目实际过程中,只需要从Srping容器里面注入就行,就不需要在创建一个对象。

   /**
     * 单机版整合spring测试
     */
    @Test
    public void testSpringJedisSingle(){
        //启动srping容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
        //从spring容器中获取bean
        JedisPool jedisPool = (JedisPool) applicationContext.getBean("redisClient");
        //获取jedis对象
        Jedis jedis = jedisPool.getResource();
        //设置登录密码,没有则忽视
        //jedis.auth("123456");
        //读取redis中的内容
        System.out.println(jedis.get("name"));
        //关闭jedis
        jedis.close();
    }

3、在我们项目当中使用,使用spring直接使用@Autowired注解,来讲我们的JedisPool对象注入即可使用。

    @Autowired
    private JedisPool jedisPool;

    @Override
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        jedis.auth(REDIS_PASSWORD);
        String result = jedis.get(key);
        jedis.close();
        return result;
    }

四、最后说两句

本章只是将我们redis整合到spring框架中,那么具体需要怎么在实际项目当中,合理的去运用呢?这个我们下一篇博客再讲。因为我们需要考虑到上线期间需要切换redis集群,所以我们当然得考虑怎么更合理的去使用redis。

猜你喜欢

转载自blog.csdn.net/weixin_38111957/article/details/82714705