001
014
015 package com.liferay.portal.cache.memcached;
016
017 import com.liferay.portal.kernel.cache.PortalCache;
018 import com.liferay.portal.kernel.cache.PortalCacheManager;
019
020 import java.net.URL;
021
022 import java.util.Map;
023 import java.util.concurrent.ConcurrentHashMap;
024 import java.util.concurrent.TimeUnit;
025
026
029 public class PooledMemcachePortalCacheManager<V>
030 implements PortalCacheManager<String, V> {
031
032 public void afterPropertiesSet() {
033 }
034
035 @Override
036 public void clearAll() {
037 _portalCaches.clear();
038 }
039
040 public void destroy() throws Exception {
041 for (PortalCache<String, V> portalCache : _portalCaches.values()) {
042 portalCache.destroy();
043 }
044 }
045
046 @Override
047 public PortalCache<String, V> getCache(String name) {
048 return getCache(name, false);
049 }
050
051 @Override
052 public PortalCache<String, V> getCache(String name, boolean blocking) {
053 PortalCache<String, V> portalCache = _portalCaches.get(name);
054
055 if (portalCache == null) {
056 portalCache = new PooledMemcachePortalCache<V>(
057 name, _memcachedClientFactory, _timeout, _timeoutTimeUnit);
058
059 _portalCaches.put(name, portalCache);
060 }
061
062 return portalCache;
063 }
064
065 @Override
066 public void reconfigureCaches(URL configurationURL) {
067 }
068
069 @Override
070 public void removeCache(String name) {
071 _portalCaches.remove(name);
072 }
073
074 public void setMemcachedClientPool(
075 MemcachedClientFactory memcachedClientFactory) {
076
077 _memcachedClientFactory = memcachedClientFactory;
078 }
079
080 public void setTimeout(int timeout) {
081 _timeout = timeout;
082 }
083
084 public void setTimeoutTimeUnit(String timeoutTimeUnit) {
085 _timeoutTimeUnit = TimeUnit.valueOf(timeoutTimeUnit);
086 }
087
088 private MemcachedClientFactory _memcachedClientFactory;
089 private Map<String, PortalCache<String, V>> _portalCaches =
090 new ConcurrentHashMap<String, PortalCache<String, V>>();
091 private int _timeout;
092 private TimeUnit _timeoutTimeUnit;
093
094 }