Ejemplo n.º 1
0
  @Test
  public void testRemoveAll() {
    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    // Remove all 1

    _memoryPortalCache.removeAll();

    Assert.assertNull(_memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(1);
    _defaultCacheListener.assertRemoveAll();

    _defaultCacheListener.reset();

    _defaultCacheReplicator.assertActionsCount(1);
    _defaultCacheReplicator.assertRemoveAll();

    _defaultCacheReplicator.reset();

    // Remove all 2

    _memoryPortalCache.put(_KEY_1, _VALUE_1);
    _memoryPortalCache.put(_KEY_2, _VALUE_2);

    PortalCacheHelperUtil.removeAllWithoutReplicator(_memoryPortalCache);

    Assert.assertNull(_memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(3);
    _defaultCacheListener.assertPut(_KEY_1, _VALUE_1);
    _defaultCacheListener.assertPut(_KEY_2, _VALUE_2);
    _defaultCacheListener.assertRemoveAll();

    _defaultCacheListener.reset();

    _defaultCacheReplicator.assertActionsCount(2);
    _defaultCacheReplicator.assertPut(_KEY_1, _VALUE_1);
    _defaultCacheReplicator.assertPut(_KEY_2, _VALUE_2);

    _defaultCacheReplicator.reset();
  }
Ejemplo n.º 2
0
  @Override
  public void putResult(
      boolean entityCacheEnabled,
      Class<?> clazz,
      Serializable primaryKey,
      Serializable result,
      boolean quiet) {

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

      return;
    }

    result = ((BaseModel<?>) result).toCacheModel();

    if (_LOCAL_CACHE_AVAILABLE) {
      Map<Serializable, Serializable> localCache = _localCache.get();

      Serializable localCacheKey = _encodeLocalCacheKey(clazz, primaryKey);

      localCache.put(localCacheKey, result);
    }

    PortalCache<Serializable, Serializable> portalCache = _getPortalCache(clazz, true);

    Serializable cacheKey = _encodeCacheKey(primaryKey);

    if (quiet) {
      PortalCacheHelperUtil.putWithoutReplicator(portalCache, cacheKey, result);
    } else {
      portalCache.put(cacheKey, result);
    }
  }
Ejemplo n.º 3
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);
  }
Ejemplo n.º 4
0
  @Test
  public void testRemove() {
    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    // Remove 1

    _memoryPortalCache.remove(_KEY_1);

    Assert.assertNull(_memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(1);
    _defaultCacheListener.assertRemoved(_KEY_1, _VALUE_1);

    _defaultCacheListener.reset();

    _defaultCacheReplicator.assertActionsCount(1);
    _defaultCacheReplicator.assertRemoved(_KEY_1, _VALUE_1);

    _defaultCacheReplicator.reset();

    // Remove 2

    _memoryPortalCache.put(_KEY_1, _VALUE_1);

    PortalCacheHelperUtil.removeWithoutReplicator(_memoryPortalCache, _KEY_1);

    Assert.assertNull(_memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(2);
    _defaultCacheListener.assertPut(_KEY_1, _VALUE_1);
    _defaultCacheListener.assertRemoved(_KEY_1, _VALUE_1);

    _defaultCacheListener.reset();

    _defaultCacheReplicator.assertActionsCount(1);
    _defaultCacheReplicator.assertPut(_KEY_1, _VALUE_1);

    _defaultCacheReplicator.reset();

    // Remove 3

    _memoryPortalCache.remove(_KEY_2);

    Assert.assertNull(_memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(0);
    _defaultCacheReplicator.assertActionsCount(0);

    // Remove 4

    _memoryPortalCache.put(_KEY_1, _VALUE_1);

    Assert.assertTrue(_memoryPortalCache.remove(_KEY_1, _VALUE_1));

    Assert.assertNull(_memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(2);
    _defaultCacheListener.assertPut(_KEY_1, _VALUE_1);
    _defaultCacheListener.assertRemoved(_KEY_1, _VALUE_1);

    _defaultCacheListener.reset();

    _defaultCacheReplicator.assertActionsCount(2);
    _defaultCacheReplicator.assertPut(_KEY_1, _VALUE_1);
    _defaultCacheReplicator.assertRemoved(_KEY_1, _VALUE_1);

    _defaultCacheReplicator.reset();

    // Remove 5

    _memoryPortalCache.put(_KEY_1, _VALUE_1);

    Assert.assertFalse(_memoryPortalCache.remove(_KEY_1, _VALUE_2));

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(1);
    _defaultCacheListener.assertPut(_KEY_1, _VALUE_1);

    _defaultCacheListener.reset();

    _defaultCacheReplicator.assertActionsCount(1);
    _defaultCacheReplicator.assertPut(_KEY_1, _VALUE_1);

    _defaultCacheReplicator.reset();

    // Remove 6

    try {
      _memoryPortalCache.remove(null);

      Assert.fail();
    } catch (NullPointerException npe) {
      Assert.assertEquals("Key is null", npe.getMessage());
    }

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(0);
    _defaultCacheReplicator.assertActionsCount(0);

    // Remove 7

    try {
      _memoryPortalCache.remove(null, _VALUE_1);

      Assert.fail();
    } catch (NullPointerException npe) {
      Assert.assertEquals("Key is null", npe.getMessage());
    }

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(0);
    _defaultCacheReplicator.assertActionsCount(0);

    // Remove 8

    try {
      _memoryPortalCache.remove(_KEY_1, null);

      Assert.fail();
    } catch (NullPointerException npe) {
      Assert.assertEquals("Value is null", npe.getMessage());
    }

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(0);
    _defaultCacheReplicator.assertActionsCount(0);
  }
Ejemplo n.º 5
0
  @Test
  public void testPut() {
    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertNull(_memoryPortalCache.get(_KEY_2));

    // Put 1

    _memoryPortalCache.put(_KEY_2, _VALUE_2);

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertEquals(_VALUE_2, _memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(1);
    _defaultCacheListener.assertPut(_KEY_2, _VALUE_2);

    _defaultCacheListener.reset();

    _defaultCacheReplicator.assertActionsCount(1);
    _defaultCacheReplicator.assertPut(_KEY_2, _VALUE_2);

    _defaultCacheReplicator.reset();

    // Put 2

    _memoryPortalCache.put(_KEY_2, _VALUE_1, 10);

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(1);
    _defaultCacheListener.assertUpdated(_KEY_2, _VALUE_1, 10);

    _defaultCacheListener.reset();

    _defaultCacheReplicator.assertActionsCount(1);
    _defaultCacheReplicator.assertUpdated(_KEY_2, _VALUE_1, 10);

    _defaultCacheReplicator.reset();

    // Put 3

    try {
      _memoryPortalCache.put(null, null);

      Assert.fail();
    } catch (NullPointerException npe) {
      Assert.assertEquals("Key is null", npe.getMessage());
    }

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(0);
    _defaultCacheReplicator.assertActionsCount(0);

    // Put 4

    try {
      _memoryPortalCache.put(_KEY_1, null);

      Assert.fail();
    } catch (NullPointerException npe) {
      Assert.assertEquals("Value is null", npe.getMessage());
    }

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(0);
    _defaultCacheReplicator.assertActionsCount(0);

    // Put 5

    try {
      _memoryPortalCache.put(_KEY_1, _VALUE_1, -1);

      Assert.fail();
    } catch (IllegalArgumentException iae) {
      Assert.assertEquals("Time to live is negative", iae.getMessage());
    }

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(0);
    _defaultCacheReplicator.assertActionsCount(0);

    // Put 6

    PortalCacheHelperUtil.putWithoutReplicator(_memoryPortalCache, _KEY_2, _VALUE_2);

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertEquals(_VALUE_2, _memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(1);
    _defaultCacheListener.assertUpdated(_KEY_2, _VALUE_2);

    _defaultCacheListener.reset();

    _defaultCacheReplicator.assertActionsCount(0);

    // Put 7

    PortalCacheHelperUtil.putWithoutReplicator(_memoryPortalCache, _KEY_2, _VALUE_1, 10);

    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_1));
    Assert.assertEquals(_VALUE_1, _memoryPortalCache.get(_KEY_2));

    _defaultCacheListener.assertActionsCount(1);
    _defaultCacheListener.assertUpdated(_KEY_2, _VALUE_1, 10);

    _defaultCacheListener.reset();

    _defaultCacheReplicator.assertActionsCount(0);
  }