Spring Cache抽象-缓存管理器

简介: Spring Cache抽象-缓存管理器

概述


CacheManager是SPI(Service Provider Interface,服务提供程序接口),提供了访问缓存名称和缓存对象的方法,同时也提供了管理缓存、操作缓存和移除缓存的方法。

下面我们来看下SpringCache框架说提供的不同的缓存管理器实现


SimpleCacheManager


通过使用SimpleCacheManager可以配置缓存列表,并利用这些缓存进行相关的操作。


SimpleCacheManager是缓存管理器的简化版本。 我们来看一下下面的配置实例。 对应缓存的定义,我们使用了ConcurrentMapCacheFactoryBean类对ComcurrentMapCache进行实例化,该实例使用了JDK的ConcurrentMap实现。

<!-- (1)添加cache命名空间和schema文件 -->    
    <!-- (2)开启支持缓存的配置项 -->
    <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>
    <!-- (3) 配置cacheManger -->
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"
        p:caches-ref="cacheObjects">
    </bean>
    <!-- (4)caches集合 -->
    <util:set id="cacheObjects">
        <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
            p:name="default"/>
        <!-- @Cacheable(cacheNames = "littleArtisan")标注的cache名称 -->
        <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
            p:name="littleArtisan"/>
    </util:set>


NoOpCacheManager


NoOpCacheManager主要用于测试目的,事实上它并不缓存任何数据。 我们来看下该缓存管理器的配置定义,我们没有未该管理器提供缓存列表,因为它仅仅作为测试的目的

<bean id="noOpCacheManager" class="org.springframework.cache.support.NoOpCacheManager"/>


ConcurrentMapCacheManager


ConcurrentMapCacheManager使用了JDK的ConcurrentMap。 它提供了与SimpleCacheManager类似的共鞥,但并不需要像前面那样定义缓存。 该缓存定义如下:

<bean id="concurrentMapCacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager"/>

CompositeCacheManager


CompositeCacheManager能够定义多个缓存管理器。


当在应用程序上下文中声明 <cache:annotation-driven>标记时,它只提供一个缓存管理器,有的时候并不能满足用户的需求,而CompositeCacheManager定义将多个缓存管理器定义组合在一起,从而扩展了该功能。


此外,CompositeCacheManager还提供了一种机制,通过使用fallbackToNoOpCache属性回到NoOpCacheManager.


我们来看下面的的例子: 定义了一个CompositeCacheManager,将一个简单的缓存管理器与HazelCast缓存管理器绑定到一起 。 简单的缓存管理器定义了members缓存,而HazelCast缓存管理器定义了visitors.

<bean id="compositeCacheManager" class="org.springframework.cache.support.CompositeCacheManager">
        <property name="cacheManagers">
            <list>
                <!-- SimpleCacheManager -->
                <bean class="org.springframework.cache.support.SimpleCacheManager">
                    <property name="caches">
                        <set>
                            <bean id="membes"
                                class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"></bean>
                        </set>
                    </property>
                </bean>
                <!-- HazelcastCacheManager -->
                <bean id="cacheManager" class="com.hazelcast.spring.cache.HazelcastCacheManager">
                    <constructor-arg ref="hazelcast" />
                </bean>
            </list>
        </property>
    </bean>
    <hz:hazelcast id="hazelcast">
        <hz:config>
            <hz:map name="vistors">
                <hz:map-store enabled="true" class-name="com.sartisan.cache.domain.Vistor"
                    write-delay-seconds="0" />
            </hz:map>
        </hz:config>
    </hz:hazelcast>


相关文章
|
2月前
|
存储 缓存 NoSQL
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
|
2月前
|
缓存 NoSQL Java
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
|
21天前
|
缓存 Java 开发工具
Spring是如何解决循环依赖的?从底层源码入手,详细解读Spring框架的三级缓存
三级缓存是Spring框架里,一个经典的技术点,它很好地解决了循环依赖的问题,也是很多面试中会被问到的问题,本文从源码入手,详细剖析Spring三级缓存的来龙去脉。
Spring是如何解决循环依赖的?从底层源码入手,详细解读Spring框架的三级缓存
|
7天前
|
存储 缓存 Java
在Spring Boot中使用缓存的技术解析
通过利用Spring Boot中的缓存支持,开发者可以轻松地实现高效和可扩展的缓存策略,进而提升应用的性能和用户体验。Spring Boot的声明式缓存抽象和对多种缓存技术的支持,使得集成和使用缓存变得前所未有的简单。无论是在开发新应用还是优化现有应用,合理地使用缓存都是提高性能的有效手段。
14 1
|
2月前
|
缓存 Java Spring
Spring缓存实践指南:从入门到精通的全方位攻略!
【8月更文挑战第31天】在现代Web应用开发中,性能优化至关重要。Spring框架提供的缓存机制可以帮助开发者轻松实现数据缓存,提升应用响应速度并减少服务器负载。通过简单的配置和注解,如`@Cacheable`、`@CachePut`和`@CacheEvict`,可以将缓存功能无缝集成到Spring应用中。例如,在配置文件中启用缓存支持并通过`@Cacheable`注解标记方法即可实现缓存。此外,合理设计缓存策略也很重要,需考虑数据变动频率及缓存大小等因素。总之,Spring缓存机制为提升应用性能提供了一种简便快捷的方式。
37 0
|
2月前
|
缓存 NoSQL Java
惊!Spring Boot遇上Redis,竟开启了一场缓存实战的革命!
【8月更文挑战第29天】在互联网时代,数据的高速读写至关重要。Spring Boot凭借简洁高效的特点广受开发者喜爱,而Redis作为高性能内存数据库,在缓存和消息队列领域表现出色。本文通过电商平台商品推荐系统的实战案例,详细介绍如何在Spring Boot项目中整合Redis,提升系统响应速度和用户体验。
52 0
|
2月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
【Azure Redis 缓存】Azure Cache for Redis 服务的导出RDB文件无法在自建的Redis服务中导入
|
2月前
|
缓存 开发框架 NoSQL
【Azure Redis 缓存】VM 里的 Redis 能直接迁移到 Azure Cache for Redis ? 需要改动代码吗?
【Azure Redis 缓存】VM 里的 Redis 能直接迁移到 Azure Cache for Redis ? 需要改动代码吗?
|
2月前
|
缓存 NoSQL Unix
【Azure Redis 缓存】Azure Cache for Redis 中如何快速查看慢指令情况(Slowlogs)
【Azure Redis 缓存】Azure Cache for Redis 中如何快速查看慢指令情况(Slowlogs)
|
2月前
|
缓存 NoSQL Java
【Azure Redis 缓存】定位Java Spring Boot 使用 Jedis 或 Lettuce 无法连接到 Redis的网络连通性步骤
【Azure Redis 缓存】定位Java Spring Boot 使用 Jedis 或 Lettuce 无法连接到 Redis的网络连通性步骤
下一篇
无影云桌面