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 import net.spy.memcached.MemcachedClientIF;
027
028
031 public class MemcachePortalCacheManager<V>
032 implements PortalCacheManager<String, V> {
033
034 @Override
035 public void clearAll() {
036 _memcachePortalCaches.clear();
037 }
038
039 public void destroy() throws Exception {
040 for (MemcachePortalCache<V> memcachePortalCache :
041 _memcachePortalCaches.values()) {
042
043 memcachePortalCache.destroy();
044 }
045 }
046
047 @Override
048 public PortalCache<String, V> getCache(String name) {
049 return getCache(name, false);
050 }
051
052 @Override
053 public PortalCache<String, V> getCache(String name, boolean blocking) {
054 MemcachePortalCache<V> memcachePortalCache = _memcachePortalCaches.get(
055 name);
056
057 if (memcachePortalCache == null) {
058 try {
059 MemcachedClientIF memcachedClient =
060 _memcachedClientFactory.getMemcachedClient();
061
062 memcachePortalCache = new MemcachePortalCache<V>(
063 name, memcachedClient, _timeout, _timeoutTimeUnit);
064
065 _memcachePortalCaches.put(name, memcachePortalCache);
066 }
067 catch (Exception e) {
068 throw new IllegalStateException(
069 "Unable to initiatlize Memcache connection", e);
070 }
071 }
072
073 return memcachePortalCache;
074 }
075
076 @Override
077 public void reconfigureCaches(URL configurationURL) {
078 }
079
080 @Override
081 public void removeCache(String name) {
082 _memcachePortalCaches.remove(name);
083 }
084
085 public void setMemcachedClientPool(
086 MemcachedClientFactory memcachedClientFactory) {
087
088 _memcachedClientFactory = memcachedClientFactory;
089 }
090
091 public void setTimeout(int timeout) {
092 _timeout = timeout;
093 }
094
095 public void setTimeoutTimeUnit(String timeoutTimeUnit) {
096 _timeoutTimeUnit = TimeUnit.valueOf(timeoutTimeUnit);
097 }
098
099 private MemcachedClientFactory _memcachedClientFactory;
100 private Map<String, MemcachePortalCache<V>> _memcachePortalCaches =
101 new ConcurrentHashMap<String, MemcachePortalCache<V>>();
102 private int _timeout;
103 private TimeUnit _timeoutTimeUnit;
104
105 }