@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;
  }
 @Override
 public Properties getProperties() {
   Configuration ec = cacheManager.getConfiguration();
   Properties p = new Properties();
   p.put("name", ec.getName());
   p.put("source", ec.getConfigurationSource().toString());
   p.put("timeoutSeconds", ec.getDefaultTransactionTimeoutInSeconds());
   p.put("maxBytesDisk", ec.getMaxBytesLocalDisk());
   p.put("maxBytesHeap", ec.getMaxBytesLocalHeap());
   p.put("maxDepth", ec.getSizeOfPolicyConfiguration().getMaxDepth());
   p.put("defaultCacheMaxEntries", ec.getDefaultCacheConfiguration().getMaxEntriesLocalHeap());
   p.put("defaultCacheTimeToIdleSecs", ec.getDefaultCacheConfiguration().getTimeToIdleSeconds());
   p.put("defaultCacheTimeToLiveSecs", ec.getDefaultCacheConfiguration().getTimeToLiveSeconds());
   p.put("defaultCacheEternal", ec.getDefaultCacheConfiguration().isEternal());
   return p;
 }
 @Override
 public CachePool createCachePool(String poolName, int cacheSize, int expiredSeconds) {
   CacheManager cacheManager = CacheManager.create();
   Cache enCache = cacheManager.getCache(poolName);
   if (enCache == null) {
     CacheConfiguration cacheConf =
         cacheManager.getConfiguration().getDefaultCacheConfiguration().clone();
     cacheConf.setName(poolName);
     if (cacheConf.getMaxEntriesLocalHeap() != 0) {
       cacheConf.setMaxEntriesLocalHeap(cacheSize);
     } else {
       cacheConf.setMaxBytesLocalHeap(String.valueOf(cacheSize));
     }
     cacheConf.setTimeToIdleSeconds(expiredSeconds);
     Cache cache = new Cache(cacheConf);
     cacheManager.addCache(cache);
     return new EnchachePool(poolName, cache, cacheSize);
   } else {
     return new EnchachePool(poolName, enCache, cacheSize);
   }
 }
Esempio n. 4
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;
 }
Esempio n. 5
0
  @Override
  public String getStatus() {
    // MIRRORS the OLD status report
    final StringBuilder buf = new StringBuilder();
    buf.append("** Memory report\n");
    buf.append("freeMemory: ").append(Runtime.getRuntime().freeMemory());
    buf.append(" totalMemory: ");
    buf.append(Runtime.getRuntime().totalMemory());
    buf.append(" maxMemory: ");
    buf.append(Runtime.getRuntime().maxMemory());
    buf.append("\n\n");

    String[] allCacheNames = cacheManager.getCacheNames();
    Arrays.sort(allCacheNames);
    ArrayList<Ehcache> caches = new ArrayList<Ehcache>(allCacheNames.length);
    for (String cacheName : allCacheNames) {
      Ehcache cache = cacheManager.getCache(cacheName);
      caches.add(cache);
    }

    // summary (cache descriptions)
    for (Ehcache cache : caches) {
      Cache c = new EhcacheCache(cache);
      buf.append(c.getDescription()).append("\n");
    }

    // extended report
    // TODO probably should remove this
    buf.append("\n** Extended Cache Report\n");
    for (Ehcache cache : caches) {
      buf.append(cache.toString());
      buf.append("\n");
    }

    // config report
    buf.append("\n** Current Cache Configurations\n");
    // determine whether to use old or new form keys
    boolean legacyKeys = true; // set true for a 2.9/BasicMemoryService compatible set of keys
    String maxKey = "maxEntries";
    String ttlKey = "timeToLive";
    String ttiKey = "timeToIdle";
    String eteKey = "eternal";
    //noinspection ConstantConditions
    if (legacyKeys) {
      maxKey = "maxElementsInMemory";
      ttlKey = "timeToLiveSeconds";
      ttiKey = "timeToIdleSeconds";
    }
    // DEFAULT cache config
    CacheConfiguration defaults = cacheManager.getConfiguration().getDefaultCacheConfiguration();
    long maxEntriesDefault = defaults.getMaxEntriesLocalHeap();
    long ttlSecsDefault = defaults.getTimeToLiveSeconds();
    long ttiSecsDefault = defaults.getTimeToIdleSeconds();
    boolean eternalDefault = defaults.isEternal();
    buf.append("# DEFAULTS: ")
        .append(maxKey)
        .append("=")
        .append(maxEntriesDefault)
        .append(",")
        .append(ttlKey)
        .append("=")
        .append(ttlSecsDefault)
        .append(",")
        .append(ttiKey)
        .append("=")
        .append(ttiSecsDefault)
        .append(",")
        .append(eteKey)
        .append("=")
        .append(eternalDefault)
        .append("\n");
    // new: timeToLive=600,timeToIdle=360,maxEntries=5000,eternal=false
    // old: timeToLiveSeconds=3600,timeToIdleSeconds=900,maxElementsInMemory=20000,eternal=false
    for (Ehcache cache : caches) {
      long maxEntries = cache.getCacheConfiguration().getMaxEntriesLocalHeap();
      long ttlSecs = cache.getCacheConfiguration().getTimeToLiveSeconds();
      long ttiSecs = cache.getCacheConfiguration().getTimeToIdleSeconds();
      boolean eternal = cache.getCacheConfiguration().isEternal();
      if (maxEntries == maxEntriesDefault
          && ttlSecs == ttlSecsDefault
          && ttiSecs == ttiSecsDefault
          && eternal == eternalDefault) {
        // Cache ONLY uses the defaults
        buf.append("# memory.").append(cache.getName()).append(" *ALL DEFAULTS*\n");
      } else {
        // NOT only defaults cache, show the settings that differ from the defaults
        buf.append("memory.").append(cache.getName()).append("=");
        boolean first = true;
        if (maxEntries != maxEntriesDefault) {
          //noinspection ConstantConditions
          first = addKeyValueToConfig(buf, maxKey, maxEntries, first);
        }
        if (ttlSecs != ttlSecsDefault) {
          first = addKeyValueToConfig(buf, ttlKey, ttlSecs, first);
        }
        if (ttiSecs != ttiSecsDefault) {
          first = addKeyValueToConfig(buf, ttiKey, ttiSecs, first);
        }
        if (eternal != eternalDefault) {
          addKeyValueToConfig(buf, eteKey, eternal, first);
        }
        buf.append("\n");
        // TODO remove the overflow to disk check
        //noinspection deprecation
        if (cache.getCacheConfiguration().isOverflowToDisk()) {
          // overflowToDisk. maxEntriesLocalDisk
          buf.append("# NOTE: ")
              .append(cache.getName())
              .append(" is configured for Overflow(disk), ")
              .append(cache.getCacheConfiguration().getMaxEntriesLocalDisk())
              .append(" entries\n");
        }
      }
    }

    final String rv = buf.toString();
    log.info(rv);

    return rv;
  }