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