@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); } }
@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); }
@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); }