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