SpringDataRedis

SpringData-Redis概述

SpringData-Redis是Spring大家族的一部分,提供了在Srping应用中通过简单的配置访问Redis服务,对Reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装。RedisTemplate提供了Redis各种操作、异常处理及序列化,支持发布订阅。

SpringData-Redis的功能

连接池自动管理,提供了一个高度封装的RedisTemplate类, 针对jedis客户端中大量api进行了归类封装。

类名 作用
SetOperations set类型数据操作
ZSetOperations zset类型数据操作
HashOperations 针对map类型的数据操作
ListOperations 针对list类型的数据操作
ValueOperations 简单Key-Value操作

SpringData-Redis入门程序

首先构建一个Maven工程,SpringDataRedisDemo,jar工程,紧接着引入Spring和Jedis和SpringDataRedis的依赖,如下:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
    <!-- 缓存 -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.8.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.7.2.RELEASE</version>
    </dependency>
</dependencies>

在src/main/resources建立redis-config.properties,内容如下:

redis.host=localhost
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true

在src/main/resources创建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"
       xmlns:p="http://www.springframework.org/schema/p"
       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">

    <context:property-placeholder location="classpath*:*.properties"/>

    <!-- redis相关配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
    </bean>

    <bean id="JedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="${redis.host}"
          p:port="${redis.port}"
          p:password="${redis.pass}"
          p:pool-config-ref="poolConfig"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="JedisConnectionFactory"/>
    </bean>
</beans>

建立TestString测试字符串操作方法

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestString {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testSet() {
        redisTemplate.boundValueOps("name").set("BNTang");
    }

    @Test
    public void testGet() {
        String name = (String) redisTemplate.boundValueOps("name").get();
        System.out.println("name=" + name);
    }

    @Test
    public void testDelete() {
        redisTemplate.delete("name");
    }
}

建立TestList测试List操作方法

lpush

/**
 * @author BNTang
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testLpush() {
        redisTemplate.boundListOps("myListkey").leftPush("001");
        redisTemplate.boundListOps("myListkey").leftPush("002");
        redisTemplate.boundListOps("myListkey").leftPush("003");
        redisTemplate.boundListOps("myListkey").leftPush("004");
    }
}

rpush

/**
 * @author BNTang
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testRpush() {
        redisTemplate.boundListOps("myListkey").rightPush("001");
        redisTemplate.boundListOps("myListkey").rightPush("002");
        redisTemplate.boundListOps("myListkey").rightPush("003");
        redisTemplate.boundListOps("myListkey").rightPush("004");
    }
}

range

/**
 * @author BNTang
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testRange() {
        List<String> myListKey = redisTemplate.boundListOps("myListkey").range(0, -1);
        for (String s : myListKey) {
            System.out.println("value = " + s);
        }
    }
}

delete

建立TestHash测试hash

hash格式

put

/**
 * @author BNTang
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testPut() {
        redisTemplate.boundHashOps("keyName1").put("name", "zs");
        redisTemplate.boundHashOps("keyName1").put("age", "20");
        redisTemplate.boundHashOps("keyName1").put("phone", "1374578547");
        redisTemplate.boundHashOps("keyName1").put("email", "303158131@qq.com");
    }
}

get

/**
 * @author BNTang
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testGetOne() {
        String name = (String) redisTemplate.boundHashOps("keyName1").get("name");

        System.out.println("name = " + name);
    }
}

entries

/**
 * @author BNTang
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testGetAll() {
        Map<String, String> testHash = (Map<String, String>) redisTemplate.boundHashOps("keyName1").entries();

        Set<Map.Entry<String, String>> entries = testHash.entrySet();

        for (Map.Entry<String, String> entry : entries) {
            System.out.println("key = " + entry.getKey());
            System.out.println("value = " + entry.getValue());
        }
    }
}

delete

/**
 * @author BNTang
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testDeleteOne() {
        redisTemplate.boundHashOps("keyName1").delete("name");
    }
}

deleteAll

/**
 * @author BNTang
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-redis.xml"})
public class TestList {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testDeleteOne() {
        redisTemplate.delete("keyName1");
    }
}
posted @ 2020-11-03 15:58  BNTang  阅读(105)  评论(0编辑  收藏  举报