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    import java.util.Map;
020    
021    /**
022     * @author Shuyang Zhou
023     */
024    public class PortalCacheMapSynchronizeUtil {
025    
026            public static <K extends Serializable, V> void synchronize(
027                    PortalCache<K, V> portalCache, Map<? extends K, ? extends V> map,
028                    Synchronizer<K, V> synchronizer) {
029    
030                    portalCache.registerPortalCacheListener(
031                            new SynchronizePortalCacheListener<K, V>(map, synchronizer));
032            }
033    
034            public interface Synchronizer<K extends Serializable, V> {
035    
036                    public void onSynchronize(
037                            Map<? extends K, ? extends V> map, K key, V value, int timeToLive);
038    
039            }
040    
041            private static class SynchronizePortalCacheListener
042                    <K extends Serializable, V> implements PortalCacheListener<K, V> {
043    
044                    public SynchronizePortalCacheListener(
045                            Map<? extends K, ? extends V> map,
046                            Synchronizer<K, V> synchronizer) {
047    
048                            _map = map;
049                            _synchronizer = synchronizer;
050                    }
051    
052                    @Override
053                    public void dispose() {
054                    }
055    
056                    @Override
057                    public void notifyEntryEvicted(
058                            PortalCache<K, V> portalCache, K key, V value, int timeToLive) {
059    
060                            _synchronizer.onSynchronize(_map, key, value, timeToLive);
061                    }
062    
063                    @Override
064                    public void notifyEntryExpired(
065                            PortalCache<K, V> portalCache, K key, V value, int timeToLive) {
066    
067                            _synchronizer.onSynchronize(_map, key, value, timeToLive);
068                    }
069    
070                    @Override
071                    public void notifyEntryPut(
072                            PortalCache<K, V> portalCache, K key, V value, int timeToLive) {
073    
074                            _synchronizer.onSynchronize(_map, key, value, timeToLive);
075                    }
076    
077                    @Override
078                    public void notifyEntryRemoved(
079                            PortalCache<K, V> portalCache, K key, V value, int timeToLive) {
080    
081                            _synchronizer.onSynchronize(_map, key, value, timeToLive);
082                    }
083    
084                    @Override
085                    public void notifyEntryUpdated(
086                            PortalCache<K, V> portalCache, K key, V value, int timeToLive) {
087    
088                            _synchronizer.onSynchronize(_map, key, value, timeToLive);
089                    }
090    
091                    @Override
092                    public void notifyRemoveAll(PortalCache<K, V> portalCache) {
093                            _map.clear();
094                    }
095    
096                    private final Map<? extends K, ? extends V> _map;
097                    private final Synchronizer<K, V> _synchronizer;
098    
099            }
100    
101    }