Spring Cloud 2.x系列之springcloud如何使用spring-cache

学了springcloud如何操作数据库进行增删改查,又学了springcloud如何操作reids把数据存储到redis。今天结合数据库操作和reids操作,来看看如何使用SpringCache。SpringCache提供了基于注解的缓存配置方法。它本质上不是一个具体的缓存实现方案(例如EHCache),而是一个对缓存使用的抽象和封装,通过在已有代码中打上几个预定义的注释,就可以实现希望达到的缓存效果。SpringCache支持跟第三方缓存例如EHCache、Redis集成;另外也提供了开箱即用的默认实现,可以直接拿来使用。SpringCache支持使用SpEL(Spring ExpressionLanguage)来定义缓存的key和各种condition,因此具备相当的灵活性,并可以支持非常复杂的语义。

1、新建项目sc-redis-cache,对用的pom.xml文件如下

<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <groupId>spring-cloud</groupId>

   <artifactId>sc-redis-cache</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <packaging>jar</packaging>



   <name>sc-redis-cache</name>

   <url>http://maven.apache.org</url>

   <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>2.0.4.RELEASE</version>

   </parent>



   <dependencyManagement>

      <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Finchley.RELEASE</version>

           <type>pom</type>

           <scope>import</scope>

        </dependency>
      </dependencies>

   </dependencyManagement>



   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>

      <maven.compiler.target>1.8</maven.compiler.target>

   </properties>

   <dependencies>

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-data-redis</artifactId>

      </dependency>

      <dependency>

        <groupId>org.apache.commons</groupId>

        <artifactId>commons-pool2</artifactId>

      </dependency>

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

      </dependency>



      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-cache</artifactId>

      </dependency>

      <dependency>

        <groupId>com.zaxxer</groupId>

        <artifactId>HikariCP</artifactId>

      </dependency>



      <dependency>

        <groupId>mysql</groupId>

        <artifactId>mysql-connector-java</artifactId>

        <version>6.0.3</version>

      </dependency>


      <dependency>

        <groupId>org.mybatis</groupId>

        <artifactId>mybatis-spring</artifactId>

        <version>1.3.2</version>

      </dependency>

      <dependency>

        <groupId>org.mybatis.spring.boot</groupId>

        <artifactId>mybatis-spring-boot-starter</artifactId>

        <version>1.3.2</version>

      </dependency>

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-test</artifactId>

        <scope>test</scope>

      </dependency>

   </dependencies>

</project>

2、新建springboot启动类RedisCacheApplication.java

原文链接

猜你喜欢

转载自blog.csdn.net/weixin_40581617/article/details/82965300