1
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
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 }