1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.cache.memcached;
16  
17  import com.liferay.portal.kernel.cache.PortalCache;
18  import com.liferay.portal.kernel.cache.PortalCacheManager;
19  
20  import java.util.Map;
21  import java.util.concurrent.ConcurrentHashMap;
22  import java.util.concurrent.TimeUnit;
23  
24  /**
25   * <a href="PooledMemcachePortalCacheManager.java.html"><b><i>View Source</i>
26   * </b></a>
27   *
28   * @author Michael C. Han
29   */
30  public class PooledMemcachePortalCacheManager implements PortalCacheManager {
31  
32      public void afterPropertiesSet() {
33      }
34  
35      public void destroy() throws Exception {
36          for (PortalCache portalCache : _portalCaches.values()) {
37              portalCache.destroy();
38          }
39      }
40  
41      public void clearAll() {
42          _portalCaches.clear();
43      }
44  
45      public PortalCache getCache(String name) {
46          return getCache(name, false);
47      }
48  
49      public PortalCache getCache(String name, boolean blocking) {
50          PortalCache portalCache = _portalCaches.get(name);
51  
52          if (portalCache == null) {
53              portalCache = new PooledMemcachePortalCache(
54                  name, _memcachedClientFactory, _timeout, _timeoutTimeUnit);
55  
56              portalCache.setDebug(_debug);
57  
58              _portalCaches.put(name, portalCache);
59          }
60  
61          return portalCache;
62      }
63  
64      public void setDebug(boolean debug) {
65          _debug = debug;
66      }
67  
68      public void setMemcachedClientPool(
69          MemcachedClientFactory memcachedClientFactory) {
70  
71          _memcachedClientFactory = memcachedClientFactory;
72      }
73  
74      public void setTimeout(int timeout) {
75          _timeout = timeout;
76      }
77  
78      public void setTimeoutTimeUnit(String timeoutTimeUnit) {
79          _timeoutTimeUnit = TimeUnit.valueOf(timeoutTimeUnit);
80      }
81  
82      private boolean _debug;
83      private MemcachedClientFactory _memcachedClientFactory;
84      private Map<String, PortalCache> _portalCaches =
85          new ConcurrentHashMap<String, PortalCache>();
86      private int _timeout;
87      private TimeUnit _timeoutTimeUnit;
88  
89  }