@Bean
  public CacheManager cacheManager() {
    log.debug("Starting Ehcache");
    cacheManager = net.sf.ehcache.CacheManager.create();
    cacheManager
        .getConfiguration()
        .setMaxBytesLocalHeap(
            env.getProperty("cache.ehcache.maxBytesLocalHeap", String.class, "16M"));
    log.debug("Registering Ehcache Metrics gauges");
    Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities();
    for (EntityType<?> entity : entities) {

      String name = entity.getName();
      if (name == null || entity.getJavaType() != null) {
        name = entity.getJavaType().getName();
      }
      Assert.notNull(name, "entity cannot exist without a identifier");

      net.sf.ehcache.Cache cache = cacheManager.getCache(name);
      if (cache != null) {
        cache
            .getCacheConfiguration()
            .setTimeToLiveSeconds(env.getProperty("cache.timeToLiveSeconds", Long.class, 3600L));
        net.sf.ehcache.Ehcache decoratedCache =
            InstrumentedEhcache.instrument(metricRegistry, cache);
        cacheManager.replaceCacheWithDecoratedCache(cache, decoratedCache);
      }
    }
    EhCacheCacheManager ehCacheManager = new EhCacheCacheManager();
    ehCacheManager.setCacheManager(cacheManager);
    return ehCacheManager;
  }
 @Test
 public void ehCacheCacheWithExistingCacheManager() {
   load(EhCacheCustomCacheManager.class, "spring.cache.type=ehcache");
   EhCacheCacheManager cacheManager = validateCacheManager(EhCacheCacheManager.class);
   assertThat(
       cacheManager.getCacheManager(), equalTo(this.context.getBean("customEhCacheCacheManager")));
 }
 @Test
 public void ehCacheCacheWithCaches() {
   load(DefaultCacheConfiguration.class, "spring.cache.type=ehcache");
   EhCacheCacheManager cacheManager = validateCacheManager(EhCacheCacheManager.class);
   assertThat(cacheManager.getCacheNames(), containsInAnyOrder("cacheTest1", "cacheTest2"));
   assertThat(cacheManager.getCacheNames(), hasSize(2));
   assertThat(
       this.context.getBean(net.sf.ehcache.CacheManager.class),
       equalTo(cacheManager.getCacheManager()));
 }
 @Test
 public void ehCacheCacheWithConfig() {
   load(
       DefaultCacheConfiguration.class,
       "spring.cache.type=ehcache",
       "spring.cache.ehcache.config=cache/ehcache-override.xml");
   EhCacheCacheManager cacheManager = validateCacheManager(EhCacheCacheManager.class);
   assertThat(
       cacheManager.getCacheNames(),
       containsInAnyOrder("cacheOverrideTest1", "cacheOverrideTest2"));
   assertThat(cacheManager.getCacheNames(), hasSize(2));
 }
Пример #5
0
 @Bean
 public CacheManager cacheManager(JHipsterProperties jHipsterProperties) {
   log.debug("Starting Ehcache");
   cacheManager = net.sf.ehcache.CacheManager.create();
   cacheManager
       .getConfiguration()
       .setMaxBytesLocalHeap(jHipsterProperties.getCache().getEhcache().getMaxBytesLocalHeap());
   log.debug("Registering Ehcache Metrics gauges");
   Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities();
   for (EntityType<?> entity : entities) {
     String name = entity.getName();
     if (name == null || entity.getJavaType() != null) {
       name = entity.getJavaType().getName();
     }
     Assert.notNull(name, "entity cannot exist without an identifier");
     reconfigureCache(name, jHipsterProperties);
     for (PluralAttribute pluralAttribute : entity.getPluralAttributes()) {
       reconfigureCache(name + "." + pluralAttribute.getName(), jHipsterProperties);
     }
   }
   EhCacheCacheManager ehCacheManager = new EhCacheCacheManager();
   ehCacheManager.setCacheManager(cacheManager);
   return ehCacheManager;
 }
Пример #6
0
 @Override
 public CacheManager cacheManager() {
   EhCacheCacheManager cacheManager = new EhCacheCacheManager();
   cacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
   return cacheManager;
 }