美文网首页Spring MVC 之 简单配置
Spring Java 注解配置之 Ehcache3.+配置

Spring Java 注解配置之 Ehcache3.+配置

作者: 巧哥儿 | 来源:发表于2018-08-31 16:37 被阅读381次

    Spring使用Ehcache3.+缓存机制

    @Configuration
    @EnableSpringHttpSession
    public class SpringCacheEhcacheConfig {
    
        /**
         * 缓存配置
         * 
         * @return
         * @throws IOException
         */
        @Bean("mManagerFactoryBean")
        public JCacheManagerFactoryBean mManagerFactoryBean() throws IOException {
            JCacheManagerFactoryBean factoryBean = new JCacheManagerFactoryBean();
            factoryBean.setCacheManagerUri(new ClassPathResource("ehcache.xml").getURI());
    
            return factoryBean;
        }
    
        /**
         * 缓存管理器
         * 
         * @return
         */
        @Bean("springCacheManager")
        public JCacheCacheManager mCacheManager(@Qualifier("mManagerFactoryBean") CacheManager cacheManager) {
            return new JCacheCacheManager(cacheManager);
        }
    
        /**
         * Spring Session
         * 
         * @return
         */
        @Bean
        public JCacheCacheSessionRepository mSessionRepository(@Qualifier("mManagerFactoryBean") CacheManager cacheManager) {
            return new JCacheCacheSessionRepository(cacheManager);
        }
    
    }
    

    需要在src/main/resources 下建立ehcache.xml配置文件,其配置方式在ehcache官方文档中可以了解到,我的配置比较简单,就配置了shrio使用的缓存

    <?xml version="1.0" encoding="UTF-8"?>
    <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:jsr107='http://www.ehcache.org/v3/jsr107' xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="
            http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd
            http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.1.xsd">
        <persistence directory="temp" />
    
        <cache alias="session-cache" uses-template="heap-cache">
            <key-type>java.lang.String</key-type>
            <value-type>cn.virens.web.components.session.CacheSession</value-type>
            <expiry>
                <ttl unit="minutes">30</ttl>
            </expiry>
        </cache>
    
        <cache alias="shiro-authorizationCache" uses-template="heap-cache">
            <expiry>
                <ttl unit="minutes">30</ttl>
            </expiry>
        </cache>
    
        <cache alias="shiro-authenticationCache" uses-template="heap-cache">
            <expiry>
                <ttl unit="minutes">30</ttl>
            </expiry>
        </cache>
    
        <cache alias="menus-cache" uses-template="heap-cache">
            <expiry>
                <ttl unit="minutes">60</ttl>
            </expiry>
        </cache>
        <cache alias="upload-files-cache" uses-template="heap-cache">
            <expiry>
                <ttl unit="minutes">60</ttl>
            </expiry>
        </cache>
        <cache alias="upload-images-cache" uses-template="heap-cache">
            <expiry>
                <ttl unit="minutes">60</ttl>
            </expiry>
        </cache>
        
        <cache-template name="heap-cache">
            <resources>
                <offheap unit="MB">50</offheap>
                <heap unit="entries">2000</heap>
                <disk persistent="true" unit="MB">500</disk>
            </resources>
        </cache-template>
    </config>
    

    相关文章

      网友评论

        本文标题:Spring Java 注解配置之 Ehcache3.+配置

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