コード例 #1
0
  /**
   * @param cacheName
   * @param legacyMode If true always create a new Cache. If false, cache must be defined in bean
   *     factory.
   * @return
   */
  private Ehcache instantiateCache(String cacheName) {
    if (M_log.isDebugEnabled()) M_log.debug("createNewCache(String " + cacheName + ")");

    String name = cacheName;
    if (name == null || "".equals(name)) {
      name = "DefaultCache" + UUID.randomUUID().toString();
    }

    // Cache creation should all go to the cache manager and be
    // configured via the cache manager setup.

    if (cacheManager.cacheExists(name)) {
      return cacheManager.getEhcache(name);
    }

    Ehcache cache = null;

    try {
      Ehcache defaultCache = getDefaultCache();
      if (defaultCache != null) {
        cache = (Ehcache) defaultCache.clone();
        cache.setName(cacheName);

        // Not look for any custom configuration.
        // Check for old configuration properties.
        if (serverConfigurationService().getString(name) == null) {
          M_log.warn("Old cache configuration " + name + " must be changed to memory." + name);
        }
        String config = serverConfigurationService().getString("memory." + name);
        if (config != null && config.length() > 0) {
          M_log.debug("Found configuration for cache: " + name + " of: " + config);
          new CacheInitializer().configure(config).initialize(cache.getCacheConfiguration());
        }

        cacheManager.addCache(cache);
      }
    } catch (Exception ex) {
      M_log.warn("Unable to access or close default cache", ex);
    }

    if (cache == null) {
      cacheManager.addCache(name);
      cache = cacheManager.getEhcache(name);
    }

    // KNL-1292: do not set if the cache is not yet init'ed
    if (cache != null && cache.getStatus().equals(Status.STATUS_ALIVE)) {
      // KNL-532 - Upgraded Ehcache 2.5.1 (2.1.0+) defaults to no stats collection.
      // We may choose to allow configuration per-cache for performance tuning.
      // For now, we default everything to on, while this property allows a system-wide override.
      cache.setStatisticsEnabled(
          !(serverConfigurationService()
              .getBoolean("memory.cache.statistics.force.disabled", false)));
    }

    return cache;

    /*


    if(legacyMode)
    {
    	if (cacheManager.cacheExists(name)) {
    		M_log.warn("Cache already exists and is bound to CacheManager; creating new cache from defaults: "
    				+ name);
    		// favor creation of new caches for backwards compatibility
    		// in the future, it seems like you would want to return the same
    		// cache if it already exists
    		name = name + UUID.randomUUID().toString();
    	}
    }

    Ehcache cache = null;

    // try to locate a named cache in the bean factory
    try {
    	cache = (Ehcache) ComponentManager.get(name);
    } catch (Exception e) {
    	cache = null;
    	M_log.error("Error occurred when trying to load cache from bean factory!", e);
    }


    if(cache != null) // found the cache
    {
    	M_log.info("Loaded Named Cache " + cache);

    	return cache;
    }
    else // did not find the cache
    {
    	if(legacyMode)
    	{
    		cacheManager.addCache(name); // create a new cache
    		cache = cacheManager.getEhcache(name);
    		M_log.info("Loaded Default Cache " + cache);
    	}
    	else
    	{
    		M_log.error("Could not find named cache in the bean factory!:"
    						+ name);
    	}

    	return cache;
    }
    */
  }