예제 #1
0
  public void removeCacheEntriesForAuthenticationToken(String authenticationToken)
      throws Exception {
    if (enabled) {
      try {

        if (StringUtils.isNotBlank(authenticationToken)) {
          logger.debug(
              "All query results for authentication token {} will be removed", authenticationToken);

          Cache cache = cmsRepositoryCache.getCache();

          Node rootNode = cache.getRoot();

          if (rootNode.hasChild(JCR_QUERY_NODE_FQN)) {

            // Get all regions under this node. For each region search
            // for node named after authentication token and remove it
            Set<Node> cacheRegions = rootNode.getChild(JCR_QUERY_NODE_FQN).getChildren();

            if (CollectionUtils.isNotEmpty(cacheRegions)) {

              for (Iterator<Node> iter = cacheRegions.iterator(); iter.hasNext(); ) {
                Node cacheRegion = iter.next();

                if (cacheRegion.hasChild(authenticationToken)) {

                  if (logger.isDebugEnabled()) {
                    logger.debug(
                        "Removing results cached under node {}",
                        cacheRegion.getChild(authenticationToken).getFqn());
                  }

                  cacheRegion.removeChild(authenticationToken);
                }
              }
            }
          }
        } else {
          logger.debug("Authentication token is null or empty. Nothing will be removed from cache");
        }
      } catch (Exception e) {
        if (propagateExceptions) throw e;
        else {
          logger.error("Exception in cache ", e);
        }
      }
    }
  }
예제 #2
0
  public Object getContentObjectFromCache(
      String contentObjectId, CacheRegion cacheRegion, String cacheKey) throws Exception {
    if (!enabled) {
      logger.debug("Query Cache is not Enabled");
      return null;
    }

    try {
      if (StringUtils.isBlank(contentObjectId)) {
        logger.debug(
            "Provided contentObject id is empty or null. Query cache will not be searched.");
        return null;
      }

      if (CacheRegion.NONE == cacheRegion) {
        logger.debug("Provided cache region is NONE. Query cache will not be searched.");
        return null;
      }

      if (cacheKey == null) {
        cacheKey = contentObjectId;
      }

      Fqn<String> contentObjectIdFQN = constructJcrQueryFQN(contentObjectId, cacheRegion);

      Node rootNode = cmsRepositoryCache.getCache().getRoot();

      if (!rootNode.hasChild(contentObjectIdFQN)) {
        logger.debug(
            "The following cache node for content object id does not exist in Query Cache and no results can be retrieved through cache: {}",
            contentObjectIdFQN);
        return null;
      }

      Object cachedItem = rootNode.getChild(contentObjectIdFQN).get(cacheKey);

      if (logger.isDebugEnabled()) {
        if (cachedItem == null) {
          logger.debug(
              "The following cache node for content onbject id does not exist in Query Cache {} under the key {} ",
              contentObjectIdFQN,
              cacheKey);
        } else {
          logger.debug(
              "The following cache node for content onbject id exists in Query Cache {} under the key {} and will be returned",
              contentObjectIdFQN,
              cacheKey);
        }
      }

      return cachedItem;
    } catch (Exception e) {
      if (propagateExceptions) throw e;
      else {
        logger.error("Exception in cache ", e);
        return null;
      }
    }
  }
예제 #3
0
  public Object getJcrQueryResults(CmsCriteria cmsCriteria, String cacheKeyPrefix)
      throws Exception {

    if (!enabled) {
      logger.debug("Query Cache is not Enabled");
      return null;
    }

    try {
      if (cmsCriteria == null || StringUtils.isBlank(cmsCriteria.getXPathQuery())) {
        logger.debug("Provided JCR Query is empty or null. Query cache will not be searched.");
        return null;
      }

      if (!cmsCriteria.isCacheable()) {
        logger.debug(
            "Jcr query {} is not cacheable. Query cache will not be searched",
            cmsCriteria.getXPathQuery());
        return null;
      }

      String jcrQuery = cmsCriteria.getXPathQuery();

      String cacheKey =
          constructCacheKey(
              cacheKeyPrefix,
              cmsCriteria.getOffset(),
              cmsCriteria.getLimit(),
              cmsCriteria.getRenderProperties());

      Fqn jcrQueryFQN = constructJcrQueryFQN(jcrQuery, cmsCriteria.getCacheRegion());

      Node rootNode = cmsRepositoryCache.getCache().getRoot();

      if (!rootNode.hasChild(jcrQueryFQN)) {
        logger.debug(
            "The following jcr query node does not exist in Query Cache and no results can be retrieved through cache: {}",
            jcrQueryFQN);
        return null;
      }

      Object cachedItem = rootNode.getChild(jcrQueryFQN).get(cacheKey);

      if (cachedItem == null) {
        logger.debug(
            "The following jcr query node exists in Query Cache but no results for the given row range and render properties exist in cache. JcrQueryNode = {} ,Cache Key = {}",
            jcrQueryFQN,
            cacheKey);
      } else {
        logger.debug(
            "The following jcr query node exists in Query Cache and results for the given row range and render properties also exist in cache and will be returned. JcrQueryNode = {} ,Cache Key = {}",
            jcrQueryFQN,
            cacheKey);
      }

      return cachedItem;
    } catch (Exception e) {
      if (propagateExceptions) throw e;
      else {
        logger.error("Exception in cache ", e);
        return null;
      }
    }
  }
예제 #4
0
  public void cacheContentObject(
      String contentObjectId, Object contentObject, CacheRegion cacheRegion, String cacheKey)
      throws Exception {
    try {

      if (enabled
          && StringUtils.isNotBlank(contentObjectId)
          && contentObject != null
          && CacheRegion.NONE != cacheRegion) {

        Cache cache = cmsRepositoryCache.getCache();

        Node rootNode = cache.getRoot();

        if (cacheKey == null) {
          cacheKey = contentObjectId;
        }

        Fqn<String> contentObjectIdFQN = constructJcrQueryFQN(contentObjectId, cacheRegion);

        if (!rootNode.hasChild(contentObjectIdFQN)) {

          logger.debug(
              "The following content object cache node does not exist and will be added in Query Cache. The content object cache node to be cached is {} under key {}",
              contentObjectIdFQN,
              cacheKey);

          // Put query results to cache
          cache.put(contentObjectIdFQN, cacheKey, contentObject);

        } else {
          // Content Object already exists
          Node contentOjectCacheNode = rootNode.getChild(contentObjectIdFQN);

          // Write results only if there is no matching rowRangeAndRenderProperties query
          if (contentOjectCacheNode.get(cacheKey) == null) {

            if (contentObject != null) {
              logger.debug(
                  "The following content object cache node exists in Query Cache. The existing Jcr Query node is {} . The key is {} and content object is not null. So a map entry with this key and the content object will be created",
                  contentObjectIdFQN,
                  cacheKey);

              contentOjectCacheNode.put(cacheKey, contentObject);
            } else {
              // Remove Content Object Cache Node
              // cache.remove(jcrQueryFQN);
              logger.debug(
                  "The following content object cache node exists in Query Cache. The existing Jcr Query node is {}.The key is {}.The content objec IS NULL. So NO map entry with this key and the content object will be created",
                  contentObjectIdFQN,
                  cacheKey);
            }
          } else {
            logger.debug(
                "The following content object cache node exists in Query Cache. The existing Jcr Query node is {}. The key is {}. So NO map entry with this key and the content object need to be created",
                contentObjectIdFQN,
                cacheKey);
          }
        }
      }
    } catch (Exception e) {
      if (propagateExceptions) throw e;
      else logger.error("Exception in cache ", e);
    }
  }
예제 #5
0
  public void cacheJcrQueryResults(
      CmsCriteria cmsCriteria,
      Object outcome,
      RenderProperties renderProperties,
      String cacheKeyPrefix)
      throws Exception {
    // Cache only if a query is provided and outcome is not null
    try {
      String jcrQuery = cmsCriteria.getXPathQuery();

      if (enabled && !StringUtils.isBlank(jcrQuery)) {

        if (!cmsCriteria.isCacheable()) {
          logger.debug("Jcr query {} is not cacheable. Query will not be cached.", jcrQuery);
          return;
        }

        Cache cache = cmsRepositoryCache.getCache();

        Node rootNode = cache.getRoot();

        String cacheKey =
            constructCacheKey(
                cacheKeyPrefix, cmsCriteria.getOffset(), cmsCriteria.getLimit(), renderProperties);

        Fqn<String> jcrQueryFQN = constructJcrQueryFQN(jcrQuery, cmsCriteria.getCacheRegion());

        if (!rootNode.hasChild(jcrQueryFQN)) {
          if (outcome != null) {
            logger.debug(
                "The following jcr query node does not exist and will be added in Query Cache along with the row range and render properties key (not null query results). The Jcr Query node to be cached is {} and the cache key is ",
                jcrQueryFQN,
                cacheKey);

            // Put query results to cache
            cache.put(jcrQueryFQN, cacheKey, outcome);

          } else {
            logger.debug(
                "The following jcr query node does not exist in Query Cache but WILL NOT CACHED because query result is null. The Jcr Query node is {} and the cache key is {}",
                jcrQueryFQN,
                cacheKey);
          }
        } else {
          // JcrQuery already exists
          Node jcrQueryNode = rootNode.getChild(jcrQueryFQN);

          // Write results only if there is no matching rowRangeAndRenderProperties query
          if (jcrQueryNode.get(cacheKey) == null) {

            if (outcome != null) {
              logger.debug(
                  "The following jcr query node exists in Query Cache. The existing Jcr Query node is {}. The row range and properties key does not exist in cache. The key is {}. The query outcome is not null. So a map entry with this key and the query results will be created",
                  jcrQueryFQN,
                  cacheKey);
              jcrQueryNode.put(cacheKey, outcome);
            } else {
              // Remove jcrQueryFqn
              // cache.remove(jcrQueryFQN);
              logger.debug(
                  "The following jcr query node exists in Query Cache. The existing Jcr Query node is {}. The row range and properties key does not exist in cache. The key is {} . The query outcome IS NULL. So NO map entry with this key and the query results will be created",
                  jcrQueryFQN,
                  cacheKey);
            }
          } else {
            logger.debug(
                "The following jcr query node exists in Query Cache. The existing Jcr Query node is {}. The cache key {} does also exist in cache. So NO map entry with this key and the query results need to be created",
                jcrQueryFQN,
                cacheKey);
          }
        }
      }
    } catch (Exception e) {
      if (propagateExceptions) throw e;
      else logger.error("Exception in cache ", e);
    }
  }