码迷,mamicode.com
首页 > 编程语言 > 详细

Spring中的cache

时间:2015-10-04 23:32:48      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

首先说一下Cache和Buffer的区别,下面一段话说的比较清楚

Cache vs Buffer

The terms "buffer" and "cache" tend to be used interchangeably; note however they represent different things. A buffer is used traditionally as an intermediate temporary store for data between a fast and a slow entity. As one party would have to wait for the other affecting performance, the buffer alleviates this by allowing entire blocks of data to move at once rather then in small chunks. The data is written and read only once from the buffer. Furthermore, the buffers are visible to at least one party which is aware of it.

A cache on the other hand by definition is hidden and neither party is aware that caching occurs.It as well improves performance but does that by allowing the same data to be read multiple times in a fast fashion.

1、采用Spring的默认cache

存在不足

不支持高可用性,也不具备持久化数据能力(只能read)并发不好

注解介绍

@Cacheable 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存

@CachePut 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用

@CachEvict 主要针对方法配置,能够根据一定的条件对缓存进行清空

@Caching

@CacheConfig

默认的key生成器策略KeyGenerator

If no params are given, return SimpleKey.EMPTY.
If only one param is given, return that instance.
If more the one param is given, return a SimpleKey containing all parameters.

1.2、基于XML

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/cache
           http://www.springframework.org/schema/cache/spring-cache.xsd">
    <context:component-scan base-package="com.traveller.domain.cache"/>
    <context:annotation-config/>
    <cache:annotation-driven/>
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                    <property name="name" value="default"/>
                </bean>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                    <property name="name" value="ascenceCache"/>
                </bean>
            </set>
        </property>
    </bean>
</beans>

1.3、基于配置类

 

@Configuration
@EnableCaching
public class CacheConfig implements CachingConfigurer {
    @Bean
    @Override
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        cacheManager.setCaches(Arrays.asList(
                new ConcurrentMapCache("scenceCache"),
                new ConcurrentMapCache("userCache")));

        return cacheManager;
    }
   ...... }

 

 

 

参考:http://www.cnblogs.com/rollenholt/p/4202631.html

参考:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html


EHCache 集成

Spring中的cache

标签:

原文地址:http://www.cnblogs.com/mingziday/p/4854047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!