Spring Boot 集成 redis 以及入门使用

在这里插入图片描述

介绍

本文主要对spring boot中使用redis时,碰到的一些概念进行简单介绍,以及环境搭建。

什么是redis

redis是一个nosql数据库,常常用于提供缓存技术,可以跨应用提供缓存,所以也可以被称为缓存服务器。

什么是jredis

jredis是一个用于访问redis服务器的客户端。大致关系如:mysql(redis) 和 jdbc(jredis)。官方还说了一个客户端Lettuce,哈哈,没听过。

什么是spring data redis

spring data redis 是 spring data下的一个子项目,其目的是想在spring环境中,可以更简单轻松的操作redis。在boot中就直接集成的是spring data redis,因此如果要查看相关api就要找它。

环境

spring boot : 2.1.1.RELEASE
database: mysql5
开发工具: idea 2018.03
redis:5.0.3

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

   
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--Redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

导入坐标

application.yml

spring:
  #redis配置
  redis:
    #Redis服务器主机
    host: localhost
    #redis服务器的登录密码
    password:
    #Redis服务器端口
    port: 6379
    #是否启用SSL支持
    ssl: false
    jedis:
      pool:
        #池在给定时间可以分配的最大连接数。使用负值无限制
        max-active: 8
        #池中“空闲”连接的最大数量。使用负值表示无限数量的空闲连接
        max-idle: 8
        #在池耗尽时,在抛出异常之前连接分配应该阻塞的最长时间。使用负值无限期阻止
        max-wait: 5000ms
        #目标是池中维护的最小空闲连接数。此设置仅在其为正时才有效
        min-idle: 0

到此,环境搭建就结束了。

基本api介绍

RedisTemplate:采用的jdk自带的序列化/翻序列化技术,如果value存的是对象,推荐使用。

StringRedisTemplate:如果value存的字符串,推荐使用。

RedisTemplate大致说明

redis中有五种数据类型,分别是:

String—字符串
Hash—无序散列表
List—列表
Set—集合(去重)
Sorted Set—有序集合
通过redisTemplate操作这五种数据类型。


        /*字符串*/
        ValueOperations valueOperations = redisTemplate.opsForValue();
        /*无序散列表*/
        HashOperations hashOperations = redisTemplate.opsForHash();
        /*列表*/
        ListOperations listOperations = redisTemplate.opsForList();
        /*集合(去重)*/
        SetOperations setOperations = redisTemplate.opsForSet();
        /*有序集合*/
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();


如果要用的话,请参考spring data redis - api文档。

总结
以上就是在spring boot中如何配置,使用redis,感谢大家的用心。
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值