SpringBoot整合Redis并使用Spring缓存注解进行缓存操作

SpringBoot整合Redis依赖

提示:因为不需要用到自定义模板,所以引入的依赖相对SpringBoot整合Redis并示例使用模板操作来说,要少些。

第一步在pom.xml中引入Redis相关依赖。

给出本文完整的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.aspire</groupId>
    <artifactId>cache-annotation-redis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cache-annotation-redis</name>
    <description>Spring缓存注解 + Redis</description>

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

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

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

</project>

第二步在系统配置文件application.properties文件中,配置Redis连接信息。

# ..................................................REDIS
# 单机版的redis可以这么连接
spring.redis.host=10.8.106.67
spring.redis.port=6379

# 对于单机版的Redis,可以使用此参数指定(数据库index),操作使用哪一个数据库
# Redis支持多个数据库(默认是16个,可通过配置来修改数据库数量),并且每个数据库的数据是隔离的,不能
# 共享,并且基于单机才有,如果是集群就没有数据库的概念,集群只有一个db0数据库。
spring.redis.database=0


# redis集群
#spring.redis.cluster.nodes = 10.8.109.24:6379, 10.8.109.36:6379, 10.8.109.49:6379, 10.8.109.24:6380, 10.8.109.36:6380, 10.8.109.49:6380

# 如果redis需要登录密码的话,需要给出
spring.redis.password = ds123

SpringBoot启用Spring缓存注解

第一步确认Spring版本不低于3.1。

注:本人用的是SpringBoot2.1.2.RELEASE,对应的Spring版本为5.1.4.RELEASE。

第二步在SpringBoot启动类上启用Spring缓存技术。

第三步在类上或类中的方法上使用Spring缓存注解即可。

注:这个【类】指的是注入了Spring容器中的。如果没有注入,那么在该类上或该类中的缓存注解是不会生效的

扫描二维码关注公众号,回复: 6071532 查看本文章

注:Spring缓存注解的具体用法可参考https://blog.csdn.net/justry_deng/article/details/89283664


提示:如果没有引入任何缓存中间件的话,那么Spring缓存注解默认操作的就是本机的缓存;而如果引
           入了Redis这样的缓存中间件的话,Spring缓存注解操作的就是Redis中的缓存了。


^_^ 如有不当之处,欢迎指正

^_^ 测试代码托管链接 
             
 https://github.com/JustryDeng/CommonRepository

^_^ 本文已经被收录进《程序员成长笔记(一)》,笔者JustryDeng

猜你喜欢

转载自blog.csdn.net/justry_deng/article/details/89285317