public Object loadResult(
      boolean entityCacheEnabled,
      Class<?> clazz,
      Serializable primaryKey,
      SessionFactory sessionFactory) {

    if (!PropsValues.VALUE_OBJECT_ENTITY_CACHE_ENABLED
        || !entityCacheEnabled
        || !CacheRegistryUtil.isActive()) {

      Session session = null;

      try {
        session = sessionFactory.openSession();

        return session.load(clazz, primaryKey);
      } finally {
        sessionFactory.closeSession(session);
      }
    }

    Object result = null;

    Map<Serializable, Object> localCache = null;

    Serializable localCacheKey = null;

    if (_localCacheAvailable) {
      localCache = _localCache.get();

      localCacheKey = _encodeLocalCacheKey(clazz, primaryKey);

      result = localCache.get(localCacheKey);
    }

    Object loadResult = null;

    if (result == null) {
      PortalCache portalCache = _getPortalCache(clazz.getName(), true);

      Serializable cacheKey = _encodeCacheKey(primaryKey);

      result = portalCache.get(cacheKey);

      if (result == null) {
        if (_log.isDebugEnabled()) {
          _log.debug("Load " + clazz + " " + primaryKey + " from session");
        }

        Session session = null;

        try {
          session = sessionFactory.openSession();

          loadResult = session.load(clazz, primaryKey);
        } finally {
          if (loadResult == null) {
            result = StringPool.BLANK;
          } else {
            result = ((BaseModel<?>) loadResult).toCacheModel();
          }

          portalCache.put(cacheKey, result);

          sessionFactory.closeSession(session);
        }
      }

      if (_localCacheAvailable) {
        localCache.put(localCacheKey, result);
      }
    }

    if (loadResult != null) {
      return loadResult;
    } else {
      return _toEntityModel(result);
    }
  }
Exemplo n.º 2
0
  @Override
  public Serializable loadResult(
      boolean entityCacheEnabled,
      Class<?> clazz,
      Serializable primaryKey,
      SessionFactory sessionFactory) {

    if (!PropsValues.VALUE_OBJECT_ENTITY_CACHE_ENABLED
        || !entityCacheEnabled
        || !CacheRegistryUtil.isActive()) {

      Session session = null;

      try {
        session = sessionFactory.openSession();

        return (Serializable) session.load(clazz, primaryKey);
      } finally {
        sessionFactory.closeSession(session);
      }
    }

    Serializable result = null;

    Map<Serializable, Serializable> localCache = null;

    Serializable localCacheKey = null;

    if (_LOCAL_CACHE_AVAILABLE) {
      localCache = _localCache.get();

      localCacheKey = _encodeLocalCacheKey(clazz, primaryKey);

      result = localCache.get(localCacheKey);
    }

    Serializable loadResult = null;

    if (result == null) {
      PortalCache<Serializable, Serializable> portalCache = _getPortalCache(clazz, true);

      Serializable cacheKey = _encodeCacheKey(primaryKey);

      result = portalCache.get(cacheKey);

      if (result == null) {
        if (_log.isDebugEnabled()) {
          _log.debug("Load " + clazz + " " + primaryKey + " from session");
        }

        Session session = null;

        try {
          session = sessionFactory.openSession();

          loadResult = (Serializable) session.load(clazz, primaryKey);
        } finally {
          if (loadResult == null) {
            result = StringPool.BLANK;
          } else {
            result = ((BaseModel<?>) loadResult).toCacheModel();
          }

          PortalCacheHelperUtil.putWithoutReplicator(portalCache, cacheKey, result);

          sessionFactory.closeSession(session);
        }
      }

      if (_LOCAL_CACHE_AVAILABLE) {
        localCache.put(localCacheKey, result);
      }
    }

    if (loadResult != null) {
      return loadResult;
    }

    return _toEntityModel(result);
  }