1
22
23 package com.liferay.portal.cache;
24
25 import com.liferay.portal.kernel.cache.PortalCache;
26 import com.liferay.portal.kernel.cache.SingleVMPool;
27 import com.liferay.portal.util.PropsUtil;
28
29 import java.io.Serializable;
30
31 import java.net.URL;
32
33 import net.sf.ehcache.Cache;
34 import net.sf.ehcache.CacheManager;
35 import net.sf.ehcache.Element;
36
37
44 public class SingleVMPoolImpl implements SingleVMPool {
45
46 public void clear() {
47 _cacheManager.clearAll();
48 }
49
50 public void clear(String name) {
51 PortalCache portalCache = getCache(name);
52
53 portalCache.removeAll();
54 }
55
56 public Object get(String name, String key) {
57 PortalCache portalCache = getCache(name);
58
59 return get(portalCache, key);
60 }
61
62 public Object get(PortalCache portalCache, String key) {
63 Element element = (Element)portalCache.get(key);
64
65 if (element == null) {
66 return null;
67 }
68 else {
69 return element.getObjectValue();
70 }
71 }
72
73 public PortalCache getCache(String name) {
74 Cache cache = _cacheManager.getCache(name);
75
76 if (cache == null) {
77 _cacheManager.addCache(name);
78
79 cache = _cacheManager.getCache(name);
80 }
81
82 return new PortalCacheImpl(cache);
83 }
84
85 public void put(String name, String key, Object obj) {
86 PortalCache portalCache = getCache(name);
87
88 put(portalCache, key, obj);
89 }
90
91 public void put(PortalCache portalCache, String key, Object obj) {
92 portalCache.put(key, obj);
93 }
94
95 public void put(String name, String key, Serializable obj) {
96 PortalCache portalCache = getCache(name);
97
98 put(portalCache, key, obj);
99 }
100
101 public void put(PortalCache portalCache, String key, Serializable obj) {
102 portalCache.put(key, obj);
103 }
104
105 public void remove(String name, String key) {
106 PortalCache portalCache = getCache(name);
107
108 remove(portalCache, key);
109 }
110
111 public void remove(PortalCache portalCache, String key) {
112 portalCache.remove(key);
113 }
114
115 private SingleVMPoolImpl() {
116 String configLocation = PropsUtil.get(
117 PropsUtil.EHCACHE_SINGLE_VM_CONFIG_LOCATION);
118
119 URL url = getClass().getResource(configLocation);
120
121 _cacheManager = new CacheManager(url);
122 }
123
124 private CacheManager _cacheManager;
125
126 }