`
rensanning
  • 浏览: 3541202 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:38018
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:606690
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:681390
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:88859
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:401389
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69588
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:91487
社区版块
存档分类
最新评论

Spring Boot 入门 - 进阶篇(5)- 数据缓存(@Cacheable)

 
阅读更多
缓存可以缓解数据库访问的压力,Spring自身不提供缓存的存储实现,需要借助第三方,比如JCache、EhCache、Hazelcast、Redis、Guava等。Spring Boot可以自动化配置合适的缓存管理器(CacheManager),默认采用的是ConcurrentMapCacheManager(java.util.concurrent.ConcurrentHashMap)。

(1)添加 spring-boot-starter-cache 依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>


(2)开启缓存功能
@Configuration
@EnableCaching
public class CacheConfig {

}


(3)缓存数据
对于缓存的操作,主要有:@Cacheable、@CachePut、@CacheEvict。

@Cacheable
Spring 在执行 @Cacheable 标注的方法前先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,执行该方法并将方法返回值放进缓存。
参数: value缓存名、 key缓存键值、 condition满足缓存条件、unless否决缓存条件
@Cacheable(value = "user", key = "#id")
public User findById(final Long id) {
    System.out.println("cache miss, invoke find by id, id:" + id);
    for (User user : users) {
        if (user.getId().equals(id)) {
            return user;
        }
    }
    return null;
}


@CachePut
和 @Cacheable 类似,但会把方法的返回值放入缓存中, 主要用于数据新增和修改方法。
@CachePut(value = "user", key = "#user.id")
public User save(User user) {
    users.add(user);
    return user;
}


@CacheEvict
方法执行成功后会从缓存中移除相应数据。
参数: value缓存名、 key缓存键值、 condition满足缓存条件、 unless否决缓存条件、 allEntries是否移除所有数据(设置为true时会移除所有缓存)
@CacheEvict(value = "user", key = "#user.id") // 移除指定key的数据
public User delete(User user) {
    users.remove(user);
    return user;
}

@CacheEvict(value = "user", allEntries = true) // 移除所有数据
public void deleteAll() {
    users.clear();
}


(4)集成EhCache

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>


SpringBoot可以自动配置不需要什么特殊设置即可使用。

src/main/resources/ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <cache name="user" maxEntriesLocalHeap="200" timeToLiveSeconds="600">
    </cache>
</ehcache>


src\main\resources/application.properties
引用
spring.cache.ehcache.config=classpath:ehcache.xml


如果想自定义设置一些个性化参数时,通过Java Config形式配置。
@Configuration
@EnableCaching
public class CacheConfig {

	@Bean
	public CacheManager cacheManager() {
		return new EhCacheCacheManager(ehCacheCacheManager().getObject());
	}

	@Bean
	public EhCacheManagerFactoryBean ehCacheCacheManager() {
		EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
		cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
		cmfb.setShared(true);
		return cmfb;
	}

}


(5)组合CacheManager

从多个CacheManager中轮询得到相应的Cache。

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public CacheManager compositeCacheManager(RedisTemplate<Object, Object> redisTemplate) {
        CompositeCacheManager cacheManager = new CompositeCacheManager(new ConcurrentMapCacheManager(), new SimpleCacheManager());
        cacheManager.setFallbackToNoOpCache(false);
        cacheManager.afterPropertiesSet();
        return cacheManager;
    }

}


*** 设置缓存无效化 spring.cache.type=none
*** 缓存的对象必须实现Serializable
*** 除GuavaCacheManager之外都支持Spring事务,即回滚时Cache的数据也会被移除
分享到:
评论

相关推荐

    3.1、spring boot redis注解缓存Cacheable (value) 1

    spring boot redis 注解缓存Cacheable (value) 1 spring boot redis 注解缓存是基于spring boot 框架和redis缓存机制的结合,实现了缓存机制的自动化管理。下面将详细介绍spring boot redis 注解缓存的实现机制和...

    redis-spring-boot-starter.rar

    3. Spring Cache支持:Spring Boot通过`@Cacheable`等注解,可以方便地实现缓存功能,底层就是利用Redis实现的。 4. Spring Session:通过集成Spring Session,可以将用户的会话数据存储在Redis中,实现会话共享。 ...

    spring-boot-tutorials-master.zip

    在"spring-boot-tutorials-master.zip"这个压缩包中,我们找到了一系列关于Spring Boot的实战示例,涵盖了多个关键组件和技术,如Dubbo、RabbitMQ、Swagger2、缓存管理和JSP。接下来,我们将深入探讨这些知识点。 1...

    spring-boot-cache.rar

    Spring Boot Cache 是一个用于简化 Spring 应用程序中缓存管理的强大工具。在这个名为 "spring-boot-cache.rar" 的压缩包中,我们很可能是找到了一个基于 Spring Boot 的小型项目,它展示了如何在 Spring Boot 应用...

    autoload-cache-spring-boot-starter:自动加载缓存spring boot starter

    `autoload-cache-spring-boot-starter` 是一个针对Java开发的Spring Boot项目的启动器,它专门设计用于自动加载和管理缓存。Spring Boot以其简洁的配置和开箱即用的特性深受开发者喜爱,而这个starter旨在进一步简化...

    spring-boot-redis.zip

    - Spring Boot可以通过`@Cacheable`、`@CacheEvict`和`@CacheConfig`等注解实现缓存功能,简化了缓存管理。 - 示例: ```java @Cacheable(value = "myCache") public MyObject getDataFromCache(String key) { ...

    Spring Boot实战派(源码)

    《Spring Boot实战派》源码提供了丰富的学习材料,旨在帮助开发者深入理解并熟练掌握Spring Boot这一流行的Java后端开发框架。Spring Boot简化了Spring应用程序的初始设置和配置,使得开发人员能够快速构建可运行的...

    spring boot - redis-1

    Spring Boot可以轻松实现基于注解的缓存管理,例如使用`@Cacheable`、`@CacheEvict`、`@CachePut`等注解,实现方法级别的缓存控制。 9. **RedisTemplate高级用法** RedisTemplate还支持事务、管道、发布/订阅等...

    spring + redis使用@Cacheable,@CachePut,@CacheEvict

    本篇文章将深入探讨如何使用Spring与Redis集成,并详细解释`@Cacheable`、`@CachePut`和`@CacheEvict`这三个关键的注解,它们是Spring缓存管理的核心组件。 首先,我们需要在项目中引入Spring Data Redis库,它提供...

    spring-boot-redis mybatis demo

    【标题】"spring-boot-redis mybatis demo" 演示了如何在Spring Boot项目中集成Redis缓存和MyBatis持久层框架。这个项目旨在提供一个基础模板,帮助开发者理解如何有效地利用这两种技术来提升应用程序的性能和响应...

    springboot-mybatis-redis缓存集成

    在现代Web应用开发中,Spring Boot、MyBatis和Redis被广泛使用,它们分别作为便捷的框架、持久层解决方案和高性能的缓存系统。本文将详细介绍如何将这三者集成,构建一个高效的微服务架构。 首先,Spring Boot是...

    09. Spring Boot缓存技术

    当我们谈论"Spring Boot缓存技术"时,实际上是在讨论如何在Spring Boot应用中有效地管理和利用缓存,以提高系统的性能和响应速度。缓存技术是解决高并发场景下数据访问延迟的有效手段,它通过将常用数据存储在内存中...

    springboot结合 @Cacheable和@CacheEvict拉起和清空缓存.zip

    在Spring Boot应用中,缓存管理是一个非常重要的性能优化手段,尤其在处理高并发和大数据量的场景下。本教程将深入探讨如何利用Spring Boot集成的缓存抽象,特别是通过`@Cacheable`和`@CacheEvict`注解来实现缓存的...

    Java课程实验 Spring Boot 缓存管理

    在Spring Boot中,你可以使用Spring框架提供的缓存管理来提高应用程序的性能。Spring Boot支持多种缓存实现,包括内存缓存和分布式缓存。 1.添加缓存依赖: 在项目的 pom.xml 文件中添加所需的缓存依赖。 2.配置缓存...

    Spring Boot详解

    5. Spring Boot自动配置原理 - 基于条件注解(@Conditional)和@EnableAutoConfiguration,Spring Boot会根据项目中的类路径判断哪些自动配置应该生效。 6. Spring Boot的Web开发 - 使用@RestController和@...

    spring-boot-redis-guava-caffeine-cache:springboot 使用 redis guava caffeine 缓存示例

    本项目"spring-boot-redis-guava-caffeine-cache"主要探讨了如何在Spring Boot应用中集成Redis、Guava和Caffeine三种不同的缓存策略。 首先,Redis是一款开源的内存数据结构存储系统,常被用作数据库、缓存和消息...

    springboot整合redis集群零配置

    本篇文章将深入探讨如何在Spring Boot中实现对Redis集群的零配置整合,并介绍使用AOP实现的自定义缓存注解。 首先,让我们来看看"Springboot整合Redis集群,零配置方式"。在Spring Boot中,我们可以通过引入`spring-...

    详解Spring Boot使用redis实现数据缓存

    在本文中,我们将深入探讨如何在Spring Boot应用中利用Redis作为数据缓存系统。Spring Boot以其简化微服务开发的特性,结合Redis的高效缓存能力,可以为应用程序提供高效的性能优化。 首先,集成Spring Boot与Redis...

Global site tag (gtag.js) - Google Analytics