001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.cache;
016    
017    import java.io.Serializable;
018    
019    /**
020     * @author Tina Tian
021     */
022    public class PortalCacheHelperUtil {
023    
024            public static <K extends Serializable, V> void putWithoutReplicator(
025                    PortalCache<K, V> portalCache, K key, V value) {
026    
027                    putWithoutReplicator(
028                            portalCache, key, value, PortalCache.DEFAULT_TIME_TO_LIVE);
029            }
030    
031            public static <K extends Serializable, V> void putWithoutReplicator(
032                    PortalCache<K, V> portalCache, K key, V value, int timeToLive) {
033    
034                    boolean enabled = SkipReplicationThreadLocal.isEnabled();
035    
036                    if (!enabled) {
037                            SkipReplicationThreadLocal.setEnabled(true);
038                    }
039    
040                    try {
041                            portalCache.put(key, value, timeToLive);
042                    }
043                    finally {
044                            if (!enabled) {
045                                    SkipReplicationThreadLocal.setEnabled(false);
046                            }
047                    }
048            }
049    
050            public static void removeAllWithoutReplicator(
051                    PortalCache<?, ?> portalCache) {
052    
053                    boolean skip = SkipReplicationThreadLocal.isEnabled();
054    
055                    if (!skip) {
056                            SkipReplicationThreadLocal.setEnabled(true);
057                    }
058    
059                    try {
060                            portalCache.removeAll();
061                    }
062                    finally {
063                            if (!skip) {
064                                    SkipReplicationThreadLocal.setEnabled(false);
065                            }
066                    }
067            }
068    
069            public static <K extends Serializable> void removeWithoutReplicator(
070                    PortalCache<K, ?> portalCache, K key) {
071    
072                    boolean skip = SkipReplicationThreadLocal.isEnabled();
073    
074                    if (!skip) {
075                            SkipReplicationThreadLocal.setEnabled(true);
076                    }
077    
078                    try {
079                            portalCache.remove(key);
080                    }
081                    finally {
082                            if (!skip) {
083                                    SkipReplicationThreadLocal.setEnabled(false);
084                            }
085                    }
086            }
087    
088    }