Beispiel #1
0
  /**
   * Test.
   *
   * @throws IOException e
   */
  @Test
  public void testCache() throws IOException {
    final String cacheName = "test 1";
    final CacheManager cacheManager = CacheManager.getInstance();
    cacheManager.addCache(cacheName);
    final String cacheName2 = "test 2";
    try {
      final Cache cache = cacheManager.getCache(cacheName);
      cache.put(new Element(1, Math.random()));
      cache.get(1);
      cache.get(0);
      cacheManager.addCache(cacheName2);
      final Cache cache2 = cacheManager.getCache(cacheName2);
      cache2.getCacheConfiguration().setOverflowToDisk(false);
      cache2.getCacheConfiguration().setEternal(true);
      cache2.getCacheConfiguration().setMaxElementsInMemory(0);

      // JavaInformations doit être réinstancié pour récupérer les caches
      final List<JavaInformations> javaInformationsList2 =
          Collections.singletonList(new JavaInformations(null, true));
      final HtmlReport htmlReport =
          new HtmlReport(collector, null, javaInformationsList2, Period.TOUT, writer);
      htmlReport.toHtml(null, null);
      assertNotEmptyAndClear(writer);
      setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, "false");
      htmlReport.toHtml(null, null);
      assertNotEmptyAndClear(writer);
    } finally {
      setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, null);
      cacheManager.removeCache(cacheName);
      cacheManager.removeCache(cacheName2);
    }
  }
Beispiel #2
0
  @Test
  public void testConfigFileHonorsClusteringOff() {
    final CacheManager cacheManager =
        new CacheManager(
            CacheManager.class.getResourceAsStream("/terracotta/ehcache-event-replication.xml"));
    try {
      final Cache cache = cacheManager.getCache("replication");
      assertThat(cache, notNullValue());
      final TerracottaConfiguration terracottaConfiguration =
          cache.getCacheConfiguration().getTerracottaConfiguration();
      assertThat(terracottaConfiguration, notNullValue());
      assertThat(terracottaConfiguration.isClustered(), is(false));
      final List eventListenerConfigurations =
          cache.getCacheConfiguration().getCacheEventListenerConfigurations();
      assertThat(eventListenerConfigurations, notNullValue());
      assertThat(eventListenerConfigurations.size(), is(1));
      assertThat(
          ((CacheConfiguration.CacheEventListenerFactoryConfiguration)
                  eventListenerConfigurations.get(0))
              .getFullyQualifiedClassPath(),
          equalTo(TerracottaCacheEventReplicationFactory.class.getName()));
      cache.put(new Element("key", "value"));
      assertThat((String) cache.get("key").getValue(), equalTo("value"));

    } finally {
      cacheManager.shutdown();
    }
  }
  @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;
  }
 /**
  * 获得一个Cache,没有则创建一个。
  *
  * @param cacheName
  * @return
  */
 private static Cache getCache(String cacheName) {
   Cache cache = cacheManager.getCache(cacheName);
   if (cache == null) {
     cacheManager.addCache(cacheName);
     cache = cacheManager.getCache(cacheName);
     cache.getCacheConfiguration().setEternal(true);
   }
   return cache;
 }
 private void reconfigureCache(String name, JHipsterProperties jHipsterProperties) {
   net.sf.ehcache.Cache cache = cacheManager.getCache(name);
   if (cache != null) {
     cache
         .getCacheConfiguration()
         .setTimeToLiveSeconds(jHipsterProperties.getCache().getTimeToLiveSeconds());
     net.sf.ehcache.Ehcache decoratedCache = InstrumentedEhcache.instrument(metricRegistry, cache);
     cacheManager.replaceCacheWithDecoratedCache(cache, decoratedCache);
   }
 }
 public CacheConfiguration getCacheConfiguration() {
   return cache.getCacheConfiguration();
 }