Skip to content

Commit

Permalink
LPS-56126 SF
Browse files Browse the repository at this point in the history
  • Loading branch information
Preston-Crary authored and brianchandotcom committed Jul 2, 2015
1 parent 2897e2b commit e02e85c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 70 deletions.
Expand Up @@ -68,7 +68,7 @@ public void clearCache() {
public void clearCache(
long groupId, String articleId, String ddmTemplateKey) {

_getPortalCacheIndexer().removeIndexedCacheKeys(
_getPortalCacheIndexer().removeKeys(
JournalContentKeyIndexAccessor.getIndex(
groupId, articleId, ddmTemplateKey));
}
Expand Down
Expand Up @@ -72,9 +72,8 @@ public static void clearCache(long... userIds) {
for (long userId : userIds) {
_userPermissionCheckerBagPortalCache.remove(userId);

_userRolePortalCacheIndexer.removeIndexedCacheKeys(userId);
_permissionCheckerBagPortalCacheIndexer.removeIndexedCacheKeys(
userId);
_userRolePortalCacheIndexer.removeKeys(userId);
_permissionCheckerBagPortalCacheIndexer.removeKeys(userId);
}

_permissionPortalCache.removeAll();
Expand All @@ -91,7 +90,7 @@ public static void clearResourceBlockCache(
return;
}

_resourceBlockIdsBagCacheIndexer.removeIndexedCacheKeys(
_resourceBlockIdsBagCacheIndexer.removeKeys(
ResourceBlockIdsBagKeyIndexAccessor.getIndex(
companyId, groupId, name));
}
Expand All @@ -113,7 +112,7 @@ public static void clearResourcePermissionCache(
return;
}

_permissionPortalCacheIndexer.removeIndexedCacheKeys(
_permissionPortalCacheIndexer.removeKeys(
PermissionKeyIndexAccessor.getIndex(name, primKey));
}

Expand Down
Expand Up @@ -17,7 +17,7 @@
import com.liferay.portal.cache.test.TestPortalCache;
import com.liferay.portal.kernel.cache.CacheListener;
import com.liferay.portal.kernel.cache.PortalCache;
import com.liferay.portal.kernel.concurrent.test.MappedMethodNameCallableInvocationHandler;
import com.liferay.portal.kernel.concurrent.test.MappedMethodCallableInvocationHandler;
import com.liferay.portal.kernel.test.ReflectionTestUtil;
import com.liferay.portal.kernel.test.rule.CodeCoverageAssertor;
import com.liferay.portal.kernel.test.util.RandomTestUtil;
Expand Down Expand Up @@ -52,8 +52,8 @@ public void setUp() throws Exception {
_cacheListener = ReflectionTestUtil.getFieldValue(
_portalCache, "aggregatedCacheListener");

_mappedMethodNameCallableInvocationHandler =
new MappedMethodNameCallableInvocationHandler(
_mappedMethodCallableInvocationHandler =
new MappedMethodCallableInvocationHandler(
ReflectionTestUtil.getFieldValue(
_portalCacheIndexer, "_indexedCacheKeys"),
true);
Expand All @@ -63,14 +63,14 @@ public void setUp() throws Exception {
ProxyUtil.newProxyInstance(
ClassLoader.getSystemClassLoader(),
new Class<?>[] {ConcurrentMap.class},
_mappedMethodNameCallableInvocationHandler));
_mappedMethodCallableInvocationHandler));
}

@Test
public void testAddIndexedCacheKeyConcurrentPutDifferentKeys()
throws ReflectiveOperationException {

_mappedMethodNameCallableInvocationHandler.putBeforeCallable(
_mappedMethodCallableInvocationHandler.putBeforeCallable(
ConcurrentMap.class.getMethod(
"putIfAbsent", Object.class, Object.class),
new Callable<Void>() {
Expand Down Expand Up @@ -131,7 +131,7 @@ public void testDispose() {

_portalCache.unregisterCacheListeners();

Set<TestKey> testKeys = _portalCacheIndexer.getIndexedCacheKeys(
Set<TestKey> testKeys = _portalCacheIndexer.getKeys(
_indexAccessor.getIndex(_INDEX_1_KEY_1));

Assert.assertTrue(testKeys.isEmpty());
Expand All @@ -141,7 +141,7 @@ public void testDispose() {
public void testGetIndexedCacheKeysWithIndexKey() {
_portalCache.put(_INDEX_1_KEY_1, _VALUE);

Set<TestKey> testKeys = _portalCacheIndexer.getIndexedCacheKeys(
Set<TestKey> testKeys = _portalCacheIndexer.getKeys(
_indexAccessor.getIndex(_INDEX_1_KEY_1));

testKeys.clear();
Expand All @@ -151,8 +151,7 @@ public void testGetIndexedCacheKeysWithIndexKey() {

@Test
public void testGetIndexedCacheKeysWithoutIndexKey() {
_portalCacheIndexer.getIndexedCacheKeys(
_indexAccessor.getIndex(_INDEX_1_KEY_1));
_portalCacheIndexer.getKeys(_indexAccessor.getIndex(_INDEX_1_KEY_1));

assertIndexCacheSynchronization();
}
Expand Down Expand Up @@ -216,7 +215,7 @@ public void testRemoveIndexedCacheKeyConcurrentPut()

_portalCache.put(_INDEX_1_KEY_1, _VALUE);

_mappedMethodNameCallableInvocationHandler.putBeforeCallable(
_mappedMethodCallableInvocationHandler.putBeforeCallable(
ConcurrentMap.class.getMethod("remove", Object.class, Object.class),
new Callable<Void>() {

Expand All @@ -240,13 +239,13 @@ public void testRemoveIndexedCacheKeyConcurrentRemove()

_portalCache.put(_INDEX_1_KEY_1, _VALUE);

_mappedMethodNameCallableInvocationHandler.putBeforeCallable(
_mappedMethodCallableInvocationHandler.putBeforeCallable(
ConcurrentMap.class.getMethod("remove", Object.class, Object.class),
new Callable<Void>() {

@Override
public Void call() throws Exception {
_portalCacheIndexer.removeIndexedCacheKeys(1L);
_portalCacheIndexer.removeKeys(1L);

return null;
}
Expand All @@ -262,8 +261,7 @@ public Void call() throws Exception {
public void testRemoveIndexedCacheKeysWithIndex() {
_portalCache.put(_INDEX_1_KEY_1, _VALUE);

_portalCacheIndexer.removeIndexedCacheKeys(
_indexAccessor.getIndex(_INDEX_1_KEY_1));
_portalCacheIndexer.removeKeys(_indexAccessor.getIndex(_INDEX_1_KEY_1));

assertIndexCacheSynchronization();
}
Expand All @@ -272,8 +270,7 @@ public void testRemoveIndexedCacheKeysWithIndex() {
public void testRemoveIndexedCacheKeysWithoutIndex() {
_portalCache.put(_INDEX_1_KEY_1, _VALUE);

_portalCacheIndexer.removeIndexedCacheKeys(
_indexAccessor.getIndex(_INDEX_2_KEY_3));
_portalCacheIndexer.removeKeys(_indexAccessor.getIndex(_INDEX_2_KEY_3));

assertIndexCacheSynchronization();
}
Expand Down Expand Up @@ -309,8 +306,7 @@ protected void assertIndexCacheSynchronization() {
Set<TestKey> actualTestKeys = new HashSet<>();

for (Long index : indexes) {
actualTestKeys.addAll(
_portalCacheIndexer.getIndexedCacheKeys(index));
actualTestKeys.addAll(_portalCacheIndexer.getKeys(index));
}

Assert.assertEquals(expectedTestKeys, actualTestKeys);
Expand All @@ -328,8 +324,8 @@ protected void assertIndexCacheSynchronization() {
new TestKeyIndexAccessor();

private CacheListener<TestKey, String> _cacheListener;
private MappedMethodNameCallableInvocationHandler
_mappedMethodNameCallableInvocationHandler;
private MappedMethodCallableInvocationHandler
_mappedMethodCallableInvocationHandler;
private PortalCache<TestKey, String> _portalCache;
private PortalCacheIndexer<Long, TestKey, String> _portalCacheIndexer;

Expand Down
Expand Up @@ -45,64 +45,61 @@ public PortalCacheIndexer(
}
}

public Set<K> getIndexedCacheKeys(I index) {
Set<K> indexedCacheKeys = _indexedCacheKeys.get(index);
public Set<K> getKeys(I index) {
Set<K> keys = _indexedCacheKeys.get(index);

if (indexedCacheKeys == null) {
if (keys == null) {
return Collections.emptySet();
}

return new HashSet<>(indexedCacheKeys);
return new HashSet<>(keys);
}

public void removeIndexedCacheKeys(I index) {
Set<K> indexedCacheKeys = _indexedCacheKeys.remove(index);
public void removeKeys(I index) {
Set<K> keys = _indexedCacheKeys.remove(index);

if (indexedCacheKeys == null) {
if (keys == null) {
return;
}

for (K indexedCacheKey : indexedCacheKeys) {
_portalCache.remove(indexedCacheKey);
for (K key : keys) {
_portalCache.remove(key);
}
}

private void _addIndexedCacheKey(K indexedCacheKey) {
I index = _indexAccessor.getIndex(indexedCacheKey);
private void _addIndexedCacheKey(K key) {
I index = _indexAccessor.getIndex(key);

Set<K> indexedCacheKeys = _indexedCacheKeys.get(index);
Set<K> keys = _indexedCacheKeys.get(index);

if (indexedCacheKeys == null) {
Set<K> newIndexedCacheKeys = new ConcurrentHashSet<>();
if (keys == null) {
Set<K> newKeys = new ConcurrentHashSet<>();

newIndexedCacheKeys.add(indexedCacheKey);
newKeys.add(key);

indexedCacheKeys = _indexedCacheKeys.putIfAbsent(
index, newIndexedCacheKeys);
keys = _indexedCacheKeys.putIfAbsent(index, newKeys);

if (indexedCacheKeys == null) {
if (keys == null) {
return;
}
}

indexedCacheKeys.add(indexedCacheKey);
keys.add(key);
}

private void _removeIndexedCacheKey(K indexedCacheKey) {
I index = _indexAccessor.getIndex(indexedCacheKey);
private void _removeIndexedCacheKey(K key) {
I index = _indexAccessor.getIndex(key);

Set<K> indexedCacheKeys = _indexedCacheKeys.get(index);
Set<K> keys = _indexedCacheKeys.get(index);

if (indexedCacheKeys == null) {
if (keys == null) {
return;
}

indexedCacheKeys.remove(indexedCacheKey);
keys.remove(key);

if (indexedCacheKeys.isEmpty() &&
_indexedCacheKeys.remove(index, indexedCacheKeys)) {

for (K victimIndexedCacheKey : indexedCacheKeys) {
if (keys.isEmpty() && _indexedCacheKeys.remove(index, keys)) {
for (K victimIndexedCacheKey : keys) {
_addIndexedCacheKey(victimIndexedCacheKey);
}
}
Expand All @@ -122,40 +119,35 @@ public void dispose() {

@Override
public void notifyEntryEvicted(
PortalCache<K, V> portalCache, K indexedCacheKey, V value,
int timeToLive) {
PortalCache<K, V> portalCache, K key, V value, int timeToLive) {

_removeIndexedCacheKey(indexedCacheKey);
_removeIndexedCacheKey(key);
}

@Override
public void notifyEntryExpired(
PortalCache<K, V> portalCache, K indexedCacheKey, V value,
int timeToLive) {
PortalCache<K, V> portalCache, K key, V value, int timeToLive) {

_removeIndexedCacheKey(indexedCacheKey);
_removeIndexedCacheKey(key);
}

@Override
public void notifyEntryPut(
PortalCache<K, V> portalCache, K indexedCacheKey, V value,
int timeToLive) {
PortalCache<K, V> portalCache, K key, V value, int timeToLive) {

_addIndexedCacheKey(indexedCacheKey);
_addIndexedCacheKey(key);
}

@Override
public void notifyEntryRemoved(
PortalCache<K, V> portalCache, K indexedCacheKey, V value,
int timeToLive) {
PortalCache<K, V> portalCache, K key, V value, int timeToLive) {

_removeIndexedCacheKey(indexedCacheKey);
_removeIndexedCacheKey(key);
}

@Override
public void notifyEntryUpdated(
PortalCache<K, V> portalCache, K indexedCacheKey, V value,
int timeToLive) {
PortalCache<K, V> portalCache, K key, V value, int timeToLive) {
}

@Override
Expand Down
Expand Up @@ -25,10 +25,10 @@
/**
* @author Preston Crary
*/
public class MappedMethodNameCallableInvocationHandler
public class MappedMethodCallableInvocationHandler
implements InvocationHandler {

public MappedMethodNameCallableInvocationHandler(
public MappedMethodCallableInvocationHandler(
Object instance, boolean removeOnCall) {

_instance = instance;
Expand Down

0 comments on commit e02e85c

Please sign in to comment.