美文网首页
Spring @cacheable

Spring @cacheable

作者: 陆成 | 来源:发表于2016-01-06 15:19 被阅读0次

1. Hello cache

1.1. spring cache XML配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:hz="http://www.hazelcast.com/schema/spring"
       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.hazelcast.com/schema/spring
              http://www.hazelcast.com/schema/spring/hazelcast-spring-3.3.xsd
              http://www.springframework.org/schema/cache
              http://www.springframework.org/schema/cache/spring-cache.xsd">

    
    <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>

    <bean id="cacheFactory" class="xx.xx.SpringCacheFactory">
        <property name="cacheManager" ref="cacheManager" />
    </bean>

    <bean id="cacheManager" class="com.hazelcast.spring.cache.HazelcastCacheManager">
        <constructor-arg ref="hazelcastInstance"/>
    </bean>

    <bean id="distributedServiceFactory" class="xx.xx.HazelcastServiceFactory">
        <property name="hazelcastInstance" ref="hazelcastInstance"/>
    </bean>


    <hz:hazelcast id="hazelcastInstance">
        <hz:config>
            <hz:group name="xx" password="xx"/>
            <hz:management-center enabled="false" url="http://localhost:8080/mancenter"/>
            <hz:network port="5701" port-auto-increment="true">
                <hz:outbound-ports>
                    <hz:ports>0</hz:ports>
                </hz:outbound-ports>
                <hz:join>
                    <hz:multicast enabled="false" multicast-group="224.2.2.3" multicast-port="54327"/>
                    <hz:tcp-ip enabled="true">
                        <hz:interface>127.0.0.1</hz:interface>
                    </hz:tcp-ip>
                </hz:join>
                <hz:interfaces enabled="false">
                    <hz:interface>10.10.1.*</hz:interface>
                </hz:interfaces>
                <hz:ssl enabled="false"/>
                <hz:socket-interceptor enabled="false"/>
                <hz:symmetric-encryption enabled="false" algorithm="PBEWithMD5AndDES"
                                         salt="thesalt" password="thepass" iteration-count="19"/>
            </hz:network>
            <hz:partition-group enabled="false"/>
            <hz:executor-service name="default" pool-size="16" queue-capacity="0"/>

            <hz:executor-service name="messageExecutorService" pool-size="5" queue-capacity="0"/>

            <hz:queue name="default" max-size="0" backup-count="1" async-backup-count="0" />

            <hz:map name="default" in-memory-format="BINARY" backup-count="1"
                    async-backup-count="0" min-eviction-check-millis="100"
                    time-to-live-seconds="30" max-idle-seconds="0" eviction-policy="NONE"
                    max-size="0" max-size-policy="PER_NODE" eviction-percentage="25"
                    merge-policy="com.hazelcast.map.merge.PassThroughMergePolicy"/>

            <hz:multimap name="default" backup-count="1" value-collection-type="SET"/>

            <hz:list name="default" backup-count="1"/>

            <hz:set name="default" backup-count="1"/>

            <hz:jobtracker name="default" max-thread-size="0" queue-size="0"
                    retry-count="0" chunk-size="1000" communicate-stats="true"
                    topology-changed-strategy="CANCEL_RUNNING_OPERATION" />

            <hz:serialization portable-version="0"/>
        </hz:config>
    </hz:hazelcast>
 
</beans>

1.2. hello world

    @Cacheable("test")
    public String testCache() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "test";
    }

调用此方法,第一次会sleep 2000ms,后面都会直接返回结果,表示缓存成功。

1.3 其他注解

  • @Cacheable
    triggers cache population
  • @CacheEvict
    triggers cache eviction
  • @CachePut
    updates the cache without interfering with the method execution
  • @Caching
    regroups multiple cache operations to be applied on a method
  • @CacheConfig
    shares some common cache-related settings at class-level

注解文档

2. Hazelcast

2.1 feature

Paste_Image.png

2.2 docs

Get start
spring Integration docs
Map Configuration

相关文章

网友评论

      本文标题:Spring @cacheable

      本文链接:https://www.haomeiwen.com/subject/zgpxkttx.html