001
014
015 package com.liferay.portal.kernel.cache;
016
017 import com.liferay.portal.kernel.concurrent.CompeteLatch;
018
019 import java.io.Serializable;
020
021 import java.util.Collection;
022 import java.util.concurrent.ConcurrentHashMap;
023 import java.util.concurrent.ConcurrentMap;
024
025
028 public class BlockingPortalCache implements PortalCache {
029
030 public BlockingPortalCache(PortalCache portalCache) {
031 _portalCache = portalCache;
032 }
033
034 public void destroy() {
035 }
036
037 public Collection<Object> get(Collection<String> keys) {
038 return _portalCache.get(keys);
039 }
040
041 public Object get(String key) {
042 Object obj = _portalCache.get(key);
043
044 if (obj != null) {
045 return obj;
046 }
047
048 CompeteLatch lastCompeteLatch = _competeLatch.get();
049
050 if (lastCompeteLatch != null) {
051 lastCompeteLatch.done();
052
053 _competeLatch.set(null);
054 }
055
056 CompeteLatch currentCompeteLatch = _competeLatchMap.get(key);
057
058 if (currentCompeteLatch == null) {
059 CompeteLatch newCompeteLatch = new CompeteLatch();
060
061 currentCompeteLatch = _competeLatchMap.putIfAbsent(
062 key, newCompeteLatch);
063
064 if (currentCompeteLatch == null) {
065 currentCompeteLatch = newCompeteLatch;
066 }
067 }
068
069 _competeLatch.set(currentCompeteLatch);
070
071 if (!currentCompeteLatch.compete()) {
072 try {
073 currentCompeteLatch.await();
074 }
075 catch (InterruptedException ie) {
076 }
077
078 _competeLatch.set(null);
079
080 obj = _portalCache.get(key);
081 }
082
083 return obj;
084 }
085
086 public void put(String key, Object obj) {
087 if (key == null) {
088 throw new IllegalArgumentException("Key is null");
089 }
090
091 if (obj == null) {
092 throw new IllegalArgumentException("Object is null");
093 }
094
095 _portalCache.put(key, obj);
096
097 CompeteLatch competeLatch = _competeLatch.get();
098
099 if (competeLatch != null) {
100 competeLatch.done();
101
102 _competeLatch.set(null);
103 }
104
105 _competeLatchMap.remove(key);
106 }
107
108 public void put(String key, Object obj, int timeToLive) {
109 if (key == null) {
110 throw new IllegalArgumentException("Key is null");
111 }
112
113 if (obj == null) {
114 throw new IllegalArgumentException("Object is null");
115 }
116
117 _portalCache.put(key, obj, timeToLive);
118
119 CompeteLatch competeLatch = _competeLatch.get();
120
121 if (competeLatch != null) {
122 competeLatch.done();
123
124 _competeLatch.set(null);
125 }
126
127 _competeLatchMap.remove(key);
128 }
129
130 public void put(String key, Serializable obj) {
131 if (key == null) {
132 throw new IllegalArgumentException("Key is null");
133 }
134
135 if (obj == null) {
136 throw new IllegalArgumentException("Object is null");
137 }
138
139 _portalCache.put(key, obj);
140
141 CompeteLatch competeLatch = _competeLatch.get();
142
143 if (competeLatch != null) {
144 competeLatch.done();
145
146 _competeLatch.set(null);
147 }
148
149 _competeLatchMap.remove(key);
150 }
151
152 public void put(String key, Serializable obj, int timeToLive) {
153 if (key == null) {
154 throw new IllegalArgumentException("Key is null");
155 }
156
157 if (obj == null) {
158 throw new IllegalArgumentException("Object is null");
159 }
160
161 _portalCache.put(key, obj, timeToLive);
162
163 CompeteLatch competeLatch = _competeLatch.get();
164
165 if (competeLatch != null) {
166 competeLatch.done();
167
168 _competeLatch.set(null);
169 }
170
171 _competeLatchMap.remove(key);
172 }
173
174 public void remove(String key) {
175 _portalCache.remove(key);
176 _competeLatchMap.remove(key);
177 }
178
179 public void removeAll() {
180 _portalCache.removeAll();
181 _competeLatchMap.clear();
182 }
183
184 public void setDebug(boolean debug) {
185 _portalCache.setDebug(debug);
186 }
187
188 private static ThreadLocal<CompeteLatch> _competeLatch =
189 new ThreadLocal<CompeteLatch>();
190 private final ConcurrentMap<String, CompeteLatch> _competeLatchMap =
191 new ConcurrentHashMap<String, CompeteLatch>();
192 private final PortalCache _portalCache;
193
194 }