spring-data-redis

Spring-Data-Redis简介
 SpringDataRedis是Spring大家族中的一个成员,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
spring-data-redis针对jedis提供了如下功能:

1.连接池自动管理,提供了一个高度封装的“RedisTemplate”类
2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
ValueOperations :简单key-value操作
HashOperations : 针对map类型数据操作
ListOperations : 针对list类型数据操作
SetOperations : set类型数据操作
ZsetOperations : zset类型数据操作
3.提供有序列化器

spring-data-redis_第1张图片
使用
1.创建maven项目并加入依赖

<dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.21.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.4</version>
            </dependency>
            <dependency>
                <groupId>aopalliance</groupId>
                <artifactId>aopalliance</artifactId>
                <version>1.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.6.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.7.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>4.3.21.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.3.21.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>2.3</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.8.5</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.8.5</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.5</version>
            </dependency>

redis.properties

redis.pool.maxTotal=1000
redis.pool.maxIdle=500
redis.pool.minIdle=50
#连接地址(本地 localhost 远程对外开发的公网ip)
redis.conn.hostName= xxxxxxxxx
redis.conn.port=6379

applicationContext.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">
    <!-- 配置读取 properties 文件的工具类 -->
    <context:property-placeholder
            location="classpath:redis.properties"/>
    <!-- Jedis 连接池 -->
    <bean id="poolConfig" 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="timeBetweenEvictionRunsMillis" value="6000" />
        <property name="minEvictableIdleTimeMillis" value="3000" />
    </bean>
    <!-- Jedis 的连接工厂 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.conn.hostName}"/>
        <property name="port" value="${redis.conn.port}"/>
        <property name="poolConfig" ref="poolConfig"/>
    </bean>
    <!-- Redis 模板对象 -->
    <bean id="redisTemplate"
          class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory"
                  ref="jedisConnectionFactory"/>
        <!-- 序列化器:能够把我们储存的 key 与 value 做序列化处理的对象 -->
        <!-- 配置默认的序列化器 -->
        <!-- keySerializer、valueSerializer 配置 Redis 中的 String 类型 key与 value 的序列化器 -->
        <!-- HashKeySerializer、HashValueSerializer 配置 Redis 中的 Hash类型 key 与 value 的序列化器 -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean  class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
    </bean>
</beans>

test

package com.example.springdataredisdemo2.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class RedisTest {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 存储键值对
     */
    @Test
    public void test1(){
        this.redisTemplate.opsForValue().set("key","bobo kaoyao");
    }

    /**
     * 获取信息
     */
    @Test
    public void test2(){
        String msg = (String) this.redisTemplate.opsForValue().get("key");
        System.out.println("获取的值:"+msg);
    }

    /**
     * 测试连接
     */
    @Test
    public void test3(){
        Jedis jedis = new Jedis("xxxxxxxxxxxx",6379);
        System.out.println(jedis.ping());
    }
}

到这里spring-data-redis简单实现完成了,不足之处望指出。
参考自SpringDataRedis

你可能感兴趣的:(SpringBoot,java,redis,java,redis,spring,boot)