Example #1
0
  /**
   * Tries to initializes the image cache. If the initialization fails the image cache remains
   * {@code null}.
   *
   * @param context the XWiki context
   */
  private void initCache(XWikiContext context) {
    if (this.imageCache == null) {
      CacheConfiguration configuration = new CacheConfiguration();

      configuration.setConfigurationId("xwiki.plugin.image");

      // Set cache constraints.
      LRUEvictionConfiguration lru = new LRUEvictionConfiguration();
      configuration.put(LRUEvictionConfiguration.CONFIGURATIONID, lru);

      String capacityParam = context.getWiki().Param("xwiki.plugin.image.cache.capacity");
      if (!StringUtils.isBlank(capacityParam) && StringUtils.isNumeric(capacityParam.trim())) {
        try {
          this.capacity = Integer.parseInt(capacityParam.trim());
        } catch (NumberFormatException e) {
          LOG.warn(
              String.format(
                  "Failed to parse xwiki.plugin.image.cache.capacity configuration parameter. "
                      + "Using %s as the cache capacity.",
                  this.capacity),
              e);
        }
      }
      lru.setMaxEntries(this.capacity);

      try {
        this.imageCache = context.getWiki().getLocalCacheFactory().newCache(configuration);
      } catch (CacheException e) {
        LOG.error("Error initializing the image cache.", e);
      }
    }
  }
  /* Copy, Paste & Customize from com.xpn.xwiki.plugin.image */
  synchronized void initCache() {
    CacheConfiguration configuration = new CacheConfiguration();

    configuration.setConfigurationId("celements.photo");

    // Set folder o store cache
    File tempDir = getContext().getWiki().getTempDirectory(getContext());
    File imgTempDir = new File(tempDir, configuration.getConfigurationId());
    try {
      imgTempDir.mkdirs();
    } catch (Exception ex) {
      LOGGER.warn("Cannot create temporary files", ex);
    }
    configuration.put("cache.path", imgTempDir.getAbsolutePath());

    // Set cache constraints
    LRUEvictionConfiguration lru = new LRUEvictionConfiguration();
    capacity = readIntegerValue("xwiki.plugin.image.cache.capacity", capacity);
    lru.setMaxEntries(capacity);
    ttlConfig = readIntegerValue("xwiki.plugin.image.cache.ttl", ttlConfig);
    lru.setTimeToLive(ttlConfig);
    LOGGER.debug(
        "creating an image cache with capacity ["
            + lru.getMaxEntries()
            + "] and ttl ["
            + lru.getTimeToLive()
            + "] and cache.path ["
            + lru.get("cache.path")
            + "].");
    configuration.put(LRUEvictionConfiguration.CONFIGURATIONID, lru);

    try {
      imageCache = getCacheManager().createNewCache(configuration);
    } catch (CacheException exp) {
      LOGGER.error("Error initializing the image cache.", exp);
    }
    initializedCache = true;
  }