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.cache.mvcc;
016    
017    import com.liferay.portal.kernel.cache.AggregatedCacheListener;
018    import com.liferay.portal.kernel.cache.LowLevelCache;
019    import com.liferay.portal.kernel.cache.PortalCacheWrapper;
020    import com.liferay.portal.model.MVCCModel;
021    
022    import java.io.Serializable;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class MVCCPortalCache<K extends Serializable, V extends MVCCModel>
028            extends PortalCacheWrapper<K, V> {
029    
030            public MVCCPortalCache(LowLevelCache<K, V> lowLevelCache) {
031                    super(lowLevelCache);
032    
033                    _lowLevelCache = lowLevelCache;
034            }
035    
036            @Override
037            public void put(K key, V value) {
038                    doPut(key, value, DEFAULT_TIME_TO_LIVE, false);
039            }
040    
041            @Override
042            public void put(K key, V value, int timeToLive) {
043                    doPut(key, value, timeToLive, false);
044            }
045    
046            @Override
047            public void putQuiet(K key, V value) {
048                    doPut(key, value, DEFAULT_TIME_TO_LIVE, true);
049            }
050    
051            @Override
052            public void putQuiet(K key, V value, int timeToLive) {
053                    doPut(key, value, timeToLive, true);
054            }
055    
056            protected void doPut(K key, V value, int timeToLive, boolean quiet) {
057                    boolean skipListener = false;
058    
059                    if (quiet) {
060                            skipListener = AggregatedCacheListener.isSkipListener();
061    
062                            AggregatedCacheListener.setSkipListener(true);
063                    }
064    
065                    try {
066                            while (true) {
067                                    V oldValue = _lowLevelCache.get(key);
068    
069                                    if (oldValue == null) {
070                                            oldValue = _lowLevelCache.putIfAbsent(
071                                                    key, value, timeToLive);
072    
073                                            if (oldValue == null) {
074                                                    return;
075                                            }
076                                    }
077    
078                                    if (value.getMvccVersion() <= oldValue.getMvccVersion()) {
079                                            return;
080                                    }
081    
082                                    if (_lowLevelCache.replace(key, oldValue, value, timeToLive)) {
083                                            return;
084                                    }
085                            }
086                    }
087                    finally {
088                            if (quiet) {
089                                    AggregatedCacheListener.setSkipListener(skipListener);
090                            }
091                    }
092            }
093    
094            private final LowLevelCache<K, V> _lowLevelCache;
095    
096    }