Spring Redis Data的基本使用 入门必备!!

一、Redis简介

redis 是一款开源的 Key-Value 数据库,运行在内存中,由 ANSI C 编写。企业开发通常采用 Redis 来实现缓存。同类的产品还有 memcache 、memcached 、MongoDB 等

1.Redis数据结构

字符串类型 string 散列类型 hash 列表类型 list 有序可重复 集合类型 set 无序不可重复 有序集合类型 sortedset 有序不可重复

2.Redis应用场景

做缓存: 缓存的是使用频次较高,但不是特别重要的数据,请求来时,优先查询非关系型数据库,如果没有查到则查询关系型数据库,从关系型数据库中查询到结果后,将结果存放到非关系型数据库中,并将结果返回给浏览器.如果查询到了,直接将查询结果返回给浏览器即可。

当用户执行 增 删 改操作时,优先操作关系型数据库, (会造成Redis数据丢失)

操作完毕后,删除非关系型数据库中的相关数据. (需要Redis数据同步)

二、Jedis

Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。可以在Redis官网下载,当然还有一些开源爱好者提供的客户端,如Jredis、SRP等等,推荐使用Jedis。

  • jdbc: java操作mysql数据的方式
  • jedis: java操作redis数据库的方式

三、 Spring Data Redis

Spring-data-redis是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:简单K-V操作 SetOperations:set类型数据操作 ZSetOperations:zset类型数据操作 HashOperations:针对map类型的数据操作 ListOperations:针对list类型的数据操作

四、Spring Data Redis操作Redis数据库

第一步:导入相关坐标到Spring项目中

<!-- Redis缓存相关坐标 -->
<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>

第二步:在resources下编写redis的配置文件

redis-config.properties
# Redis settings 
# server IP 
redis.host=192.168.25.130
# server port 
redis.port=6379
# server pass 
redis.pass=
# use dbIndex 
redis.database=0
# maxIdle
redis.maxIdle=300
# maxWait
redis.maxWait=3000
# testOnBorrow
redis.testOnBorrow=true
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">

    <!-- 1.加载外部配置文件 -->
    <context:property-placeholder location="classpath*:properties/*.properties"/>

    <!-- 2.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>

    <!-- 3.Jedis连接工厂的配置 -->
    <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"/>

    <!-- 4.redis模板的配置 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="JedisConnectionFactory"/>
    </bean>

</beans>  

第三步:使用redisTemplate模板操作数据库

1.操作String类型数据

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-redis.xml")
public class RedisStrTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testSetString(){
        //模板绑定存储的数据类型为String并存入数据: key是testStr   value是DinTalk
        redisTemplate.boundValueOps("testStr").set("DinTalk");
    }

    @Test
    public void testGetString(){
        //模板绑定存储的数据类型为String并取数据:使用key testStr
        String testStr = (String) redisTemplate.boundValueOps("testStr").get();
        System.out.println(testStr);
    }

    @Test
    public void testDelString(){
        //直接用模板根据key删除数据,删除后无数据查询返回null
        redisTemplate.delete("testStr");
    }

    //使用相同的key重新设置值便是更新
}

2.操作List类型数据

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-redis.xml")
public class RedisListTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testSetList(){
        //模板绑定存储的数据类型为List并存入数据:key是myList,List的数据类型,数据为din.....
        redisTemplate.boundListOps("myList").leftPush("din");
        redisTemplate.boundListOps("myList").leftPush("talk");
        redisTemplate.boundListOps("myList").leftPush("cn");
    }

    @Test
    public void testGetList(){
        //模板绑定存储的数据类型为List并取数据:key是myList,rang中0是开始,-1是全部
        List myList = redisTemplate.boundListOps("myList").range(0, -1);
        myList.stream().forEach(System.out::println);
        //取list中的一个数据 :先进先出(相当于弹出-删除了)
        //String myList = (String) redisTemplate.boundListOps("myList").rightPop();
        //System.out.println(myList); //din
    }

    @Test
    public void testDelList(){
        //直接用模板根据key删除数据(删除整个集合)
        redisTemplate.delete("myList");
        //指定删除list的数据 :删除一个talk
        //redisTemplate.boundListOps("myList").remove(1,"talk");
    }

    //使用相同的key重新设置值便是更新
}

3.操作Set类型数据

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-redis.xml")
public class RedisSetTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testSetValue(){
        //模板绑定存储的数据类型为Set并存入数据: key是mySet   value是din talk 等
        redisTemplate.boundSetOps("mySet").add("www");
        redisTemplate.boundSetOps("mySet").add("din");
        redisTemplate.boundSetOps("mySet").add("talk");
        redisTemplate.boundSetOps("mySet").add("cn");
    }

    @Test
    public void testGetValue(){
        //模板绑定存储的数据类型为Set并取数据:使用key为 mySet
        Set mySet = redisTemplate.boundSetOps("mySet").members();
        mySet.stream().forEach(System.out::println);
    }

    @Test
    public void testDelValue(){
        //直接用模板根据key删除数据,删除整个Set集合
        redisTemplate.delete("mySet");
        //redisTemplate.boundSetOps("mySet").remove("www");
    }

    //使用相同的key重新设置值便是更新
}

4.操作Hash类型数据

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-redis.xml")
public class RedisHashTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testSetHash(){
        //模板绑定存储的数据类型为Hash并存入数据: key是myHash value是{din:叮}等键值对
        redisTemplate.boundHashOps("myHash").put("din","叮");
        redisTemplate.boundHashOps("myHash").put("talk","当");
        redisTemplate.boundHashOps("myHash").put("song","宋");
        redisTemplate.boundHashOps("myHash").put("hui","慧");
    }

    @Test
    public void testGetHash(){
        //模板绑定存储的数据类型为Hash并取数据:使用key为 myHash,获取键的集合
        //Set myHash = redisTemplate.boundHashOps("myHash").keys();
        //myHash.stream().forEach(System.out::println);
        //获取value的集合(值可重复,所以是list)
        //List myHash = redisTemplate.boundHashOps("myHash").values();
        //myHash.stream().forEach(System.out::println);
        //使用myHash这个key获取到Hash并用键获取值
        String s = (String) redisTemplate.boundHashOps("myHash").get("din");
        System.out.println(s);
    }

    @Test
    public void testDelHash(){
        //直接用模板根据key删除数据,删除整个Hash集合
        //redisTemplate.delete("myHash");
        redisTemplate.boundHashOps("myHash").delete("din");
    }

    //使用相同的key重新设置值便是更新
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值