ehcache3x-jsr107

ehcache-3.8.1 与 JSR107

jar包:ehcache-3.8.1.jar;cache-api-1.1.1.jar

一:创建缓存对象

使用JSR107统一的manager

package app.demo.echcache;

import java.net.URISyntaxException;
import java.util.List;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableCaching
public class SpringCacheEhcacheConfig {

    /**
     * 缓存管理器
     * 
     * @return
     * @throws URISyntaxException 
     */
    @Bean
    public org.springframework.cache.CacheManager cacheManager() throws URISyntaxException {
        CachingProvider cachingProvider = Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider");
        CacheManager manager = null;

        manager = cachingProvider.getCacheManager(getClass().getResource("/resources/ehcache.xml").toURI(),
                getClass().getClassLoader());

        MutableConfiguration<String, List> mutableConfiguration = new MutableConfiguration<String, List>();
        mutableConfiguration.setTypes(String.class, List.class);

        manager.createCache("userRoles", mutableConfiguration);// 先创建一个,名称需要一样,ehcache中的配置会覆盖加强

        return   new  JCacheCacheManager(manager);
    }
}

二:xml配置

<config
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns='http://www.ehcache.org/v3'
    xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
    xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

    <!-- 拓展 -->
    <service>
   <!--  <jsr107:defaults default-template="slNone"> -->
     <jsr107:defaults default-template="defaultName" enable-management="false" enable-statistics="false">
        <jsr107:cache name="userRoles" template="stringListlNone"/>
    </jsr107:defaults>
  </service>

  <!-- jsr 简单实现 -->
 <!--  <cache alias="userRoles">
     <key-type>java.lang.String</key-type>
    <value-type>java.util.List</value-type>
     <heap unit="entries">2000</heap>
  </cache> -->



  <cache-template name="defaultName"></cache-template>

      <cache-template name="stringListlNone">
          <key-type>java.lang.String</key-type>
        <value-type>java.util.List</value-type>
          <expiry>
                  <!--  <ttl unit="seconds">20</ttl>  --><!--存活时间,到期失效-->
                  <!-- <tti unit="minutes">2</ttl> 闲置时间,到期失效-->
              <none/>
          </expiry>    
          <!-- 堆上最多容纳300个entry -->
          <heap unit="entries">300</heap>
           <!--<offheap></offheap> -->
            <!--<disk></disk> -->
      </cache-template>

</config>

三:应用

3.1:注解方式

import javax.cache.annotation.CacheDefaults;
import javax.cache.annotation.CacheKey;
import javax.cache.annotation.CacheKeyGenerator;
import javax.cache.annotation.CacheResult;

//缓存
@CacheResult(cacheName ="userRoles")
public List<String> findRolesByPerson(@CacheKey String id) {
   return null;
}
//清除所有键值对
@CacheEvict(cacheNames = "userRoles",allEntries = true)
public void cacheEvictUserRoles() {

}

3.2:代码方式

import org.springframework.cache.CacheManager;

@Autowired
public CacheManager cacheManager;

public void test() {
        Cache cache = cacheManager.getCache("userRoles");
        List<String> aa = new ArrayList<String>();
        aa.add("齐天大圣");
        cache.put("11111", aa);

        cache.get("11111").get();//[齐天大圣],若缓存中没值,会报 null
        cache.get("11111", List.class);//[齐天大圣]
        cache.evict("11111");//清除key为11111 的缓存
        cache.get("3333", new Callable() {//[齐天大圣333],若缓存中有值返回值,没有则执行Callable中的方法,并缓存

            @Override
            public Object call() throws Exception {
                List<String> bb = new ArrayList<String>();
                bb.add("齐天大圣333");
                return bb;
            }
        });
    }
posted @ 2022-08-17 17:37  水映苑  阅读(73)  评论(0编辑  收藏  举报