一、SpringDataRedis简介

SpringDataRedis简介.jpg

二、SpringDataRedis的基本使用

2.1、创建工程,导入Maven坐标:注意SpringDataRedis是对Jedis的一层封装,所以需要导入Jedis的坐标

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>study-springdata</artifactId>
  7. <groupId>com.study</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>springdataredis</artifactId>
  12. <dependencies>
  13. <!-- SpringDataRedis是对jedis的一层封装,所以需要导入jedis的坐标-->
  14. <dependency>
  15. <groupId>redis.clients</groupId>
  16. <artifactId>jedis</artifactId>
  17. <version>2.9.3</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.data</groupId>
  21. <artifactId>spring-data-redis</artifactId>
  22. <version>2.1.8.RELEASE</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-test</artifactId>
  27. <version>5.1.6.RELEASE</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>junit</groupId>
  31. <artifactId>junit</artifactId>
  32. <version>4.13</version>
  33. </dependency>
  34. <!-- 做JSON转换的-->
  35. <dependency>
  36. <groupId>com.fasterxml.jackson.core</groupId>
  37. <artifactId>jackson-databind</artifactId>
  38. <version>2.11.2</version>
  39. </dependency>
  40. </dependencies>
  41. </project>

2.2配置Spring的核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!--    配置Jedis连接池参数-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--        配置最大连接数-->
        <property name="maxTotal" value="30"/>
<!--        最大空闲连接数-->
        <property name="maxIdle" value="20"/>
<!--        最小空闲连接数-->
        <property name="minIdle" value="10"/>
    </bean>

<!--    配置Jedis连接工厂-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="182.92.71.236"/>
        <property name="port" value="6379"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
    </bean>

<!--    配置Redis的模板:提供用来操作Redis的API-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!--        需要通过连接工厂获取连接-->
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
<!--        配置序列化器:配置非hash的序列化器-->
        <property name="keySerializer"><!--配置key的-->
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer"><!--配置key的-->
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
    </bean>
</beans>

2.3编写测试

package com.study;

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.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application.xml")
public class SpringDataRedisTest {
    //通过配置文件配置的redisTemplate对象来操作Redis
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void testAddRedis(){
        ValueOperations options = redisTemplate.opsForValue();
        options.set("name","test");
    }
    //这里可以单独指定某一次操作使用某个序列化器
    @Test
    public void testAdd(){
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        ValueOperations options = redisTemplate.opsForValue();
        options.set("name1","test1");
}

注意:我们在向Redis中存入数据的时候,数据会经过序列化器,它首先会对数据进行序列化,然后在存入Redis中
redis序列化器.jpg
2.4:SpringDataRedis底层分析:
SpringDataRedis就是对Jedis或其他操作Redis的技术上做了一层封装,它屏蔽了这些原生技术的使用细节,统一了调用接口,是我们操作更加简单了

三、SpringDataRedis的常用操作

3.1SpringDataRedisAPI介绍
SpringDataRedisAPI介绍.jpg
3.2SpringDataRedis操作String类型

package com.study;

import org.junit.Before;
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.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application.xml")
public class SpringDataRedisTest1 {
    @Autowired
    private RedisTemplate redisTemplate;
    ValueOperations options=null;
    @Before
    public void before(){
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        options = redisTemplate.opsForValue();
    }
    /**
     * 添加key-value
     */
    @Test
    public void addKeyAndValue(){
        /*设置key-value:不设置过期时间*/
        //options.set("test","aaa");

        /*设置过期时间为5秒*/
        //options.set("test1","aaa",10, TimeUnit.SECONDS);

        /*替换value:索引从0开始aaa--abb*/
        //options.set("test","bb",1);

        /*当key存在则不进行任何操作,不存在则进行保存操作*/
        //options.setIfAbsent("name","hello");

        /*批量保存*/
//        HashMap<String, String> map = new HashMap<String, String>();
//        map.put("name1","test1");
//        map.put("name2","test2");
//        map.put("name3","test3");
//        options.multiSet(map);

        /*追加value:test1----test1132,key存在则追加,不存在则保存*/
        options.append("name","132");
    }

    @Test
    public void testGet(){
        //根据Key获取value
        String name = (String) options.get("name1");
        System.out.println(name);
        //根据Key获取Value并截取[2,4],索引依然从0开始
        String name1 = options.get("name1", 2, 4);
        //批量获取
        ArrayList<String> list = new ArrayList<String>();
        list.add("name1");
        list.add("name2");
        list.add("name3");
        List valueLists = options.multiGet(list);
        //根据Key获取Value的长度
        Long valueLength = options.size("name");
        /*Value自增操作*/
        //value自增1
        options.increment("name");
        //指定自增值
        options.increment("name",4);
        //与自增类似是自减操作 options.decrement()和自增使用方法相同

        /*删除操作*/
        redisTemplate.delete("name");
        //批量删除
        ArrayList<String> list1 = new ArrayList<String>();
        list1.add("name1");
        list1.add("name2");
        redisTemplate.delete(list1);
    }
}