springboot结合缓存

    技术2022-07-11  77

    1 结合ehcache缓存

    <!--缓存的依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>

    在启动类上面添加@EnableCaching 注解 开启缓存

    @SpringBootApplication @EnableCaching public class SpringvueApplication { public static void main(String[] args) { SpringApplication.run(SpringvueApplication.class, args); } }

    缓存配置文件 ehcahe.xml

    <ehcache> <diskStore path="java.io.tmpdir/cache"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> <cache name="book_cache" maxElementsInMemory="10000" eternal="true" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskPersistent="true" diskExpiryThreadIntervalSeconds="10"/> </ehcache>

    配置解释如下 创建Book的实体类和测试dao dao中注解解释 测试类如下 2.redis缓存 核心依赖

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>

    写在application.properties中的配置 其余一样

    Processed: 0.015, SQL: 12