1
22
23 package com.liferay.portal.webcache;
24
25 import com.liferay.portal.kernel.cache.PortalCache;
26 import com.liferay.portal.kernel.cache.SingleVMPool;
27 import com.liferay.portal.kernel.util.Time;
28 import com.liferay.portal.kernel.webcache.WebCacheException;
29 import com.liferay.portal.kernel.webcache.WebCacheItem;
30 import com.liferay.portal.kernel.webcache.WebCachePool;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35
41 public class WebCachePoolImpl implements WebCachePool {
42
43 public static final String CACHE_NAME = WebCachePool.class.getName();
44
45 public void afterPropertiesSet() {
46 _cache = _singleVMPool.getCache(CACHE_NAME);
47 }
48
49 public void clear() {
50 _cache.removeAll();
51 }
52
53 public Object get(String key, WebCacheItem wci) {
54 Object obj = _singleVMPool.get(_cache, key);
55
56 if (obj == null) {
57 try {
58 obj = wci.convert(key);
59
60 int timeToLive = (int)(wci.getRefreshTime() / Time.SECOND);
61
62 _cache.put(key, obj, timeToLive);
63 }
64 catch (WebCacheException wce) {
65 Throwable cause = wce.getCause();
66
67 if (cause != null) {
68 _log.error(cause, cause);
69 }
70 else {
71 _log.error(wce, wce);
72 }
73 }
74 }
75
76 return obj;
77 }
78
79 public void remove(String key) {
80 _singleVMPool.remove(_cache, key);
81 }
82
83 public void setSingleVMPool(SingleVMPool singleVMPool) {
84 _singleVMPool = singleVMPool;
85 }
86
87 private static Log _log = LogFactory.getLog(WebCachePoolImpl.class);
88
89 private SingleVMPool _singleVMPool;
90 private PortalCache _cache;
91
92 }