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  import net.spy.memcached.MemcachedClientIF;
25  
26  /**
27   * <a href="MemcachePortalCacheManager.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Michael C. Han
30   */
31  public class MemcachePortalCacheManager implements PortalCacheManager {
32  
33      public void clearAll() {
34          _memcachePortalCaches.clear();
35      }
36  
37      public void destroy() throws Exception {
38          for (MemcachePortalCache memcachePortalCache :
39                  _memcachePortalCaches.values()) {
40  
41              memcachePortalCache.destroy();
42          }
43      }
44  
45      public PortalCache getCache(String name) {
46          return getCache(name, false);
47      }
48  
49      public PortalCache getCache(String name, boolean blocking) {
50          MemcachePortalCache memcachePortalCache = _memcachePortalCaches.get(
51              name);
52  
53          if (memcachePortalCache == null) {
54              try {
55                  MemcachedClientIF memcachedClient  =
56                      _memcachedClientFactory.getMemcachedClient();
57  
58                  memcachePortalCache = new MemcachePortalCache(
59                      name, memcachedClient, _timeout, _timeoutTimeUnit);
60  
61                  memcachePortalCache.setDebug(_debug);
62  
63                  _memcachePortalCaches.put(name, memcachePortalCache);
64              }
65              catch (Exception e) {
66                  throw new IllegalStateException(
67                      "Unable to initiatlize Memcache connection", e);
68              }
69          }
70  
71          return memcachePortalCache;
72      }
73  
74      public void setDebug(boolean debug) {
75          _debug = debug;
76      }
77  
78      public void setMemcachedClientPool(
79          MemcachedClientFactory memcachedClientFactory) {
80  
81          _memcachedClientFactory = memcachedClientFactory;
82      }
83  
84      public void setTimeout(int timeout) {
85          _timeout = timeout;
86      }
87  
88      public void setTimeoutTimeUnit(String timeoutTimeUnit) {
89          _timeoutTimeUnit = TimeUnit.valueOf(timeoutTimeUnit);
90      }
91  
92      private boolean _debug;
93      private MemcachedClientFactory _memcachedClientFactory;
94      private Map<String, MemcachePortalCache> _memcachePortalCaches =
95          new ConcurrentHashMap<String, MemcachePortalCache>();
96      private int _timeout;
97      private TimeUnit _timeoutTimeUnit;
98  
99  }