| EhcachePortalCache.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 *
5 *
6 *
7 * The contents of this file are subject to the terms of the Liferay Enterprise
8 * Subscription License ("License"). You may not use this file except in
9 * compliance with the License. You can obtain a copy of the License by
10 * contacting Liferay, Inc. See the License for the specific language governing
11 * permissions and limitations under the License, including but not limited to
12 * distribution rights of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portal.cache;
24
25 import com.liferay.portal.kernel.cache.PortalCache;
26
27 import java.io.Serializable;
28
29 import net.sf.ehcache.Ehcache;
30 import net.sf.ehcache.Element;
31
32 /**
33 * <a href="EhcachePortalCache.java.html"><b><i>View Source</i></b></a>
34 *
35 * @author Brian Wing Shun Chan
36 */
37 public class EhcachePortalCache implements PortalCache {
38
39 public EhcachePortalCache(Ehcache cache) {
40 _cache = cache;
41 }
42
43 public Object get(String key) {
44 Element element = _cache.get(key);
45
46 if (element == null) {
47 return null;
48 }
49 else {
50 return element.getObjectValue();
51 }
52 }
53
54 public void put(String key, Object obj) {
55 Element element = new Element(key, obj);
56
57 _cache.put(element);
58 }
59
60 public void put(String key, Object obj, int timeToLive) {
61 Element element = new Element(key, obj);
62
63 element.setTimeToLive(timeToLive);
64
65 _cache.put(element);
66 }
67
68 public void put(String key, Serializable obj) {
69 Element element = new Element(key, obj);
70
71 _cache.put(element);
72 }
73
74 public void put(String key, Serializable obj, int timeToLive) {
75 Element element = new Element(key, obj);
76
77 element.setTimeToLive(timeToLive);
78
79 _cache.put(element);
80 }
81
82 public void remove(String key) {
83 _cache.remove(key);
84 }
85
86 public void removeAll() {
87 _cache.removeAll();
88 }
89
90 private Ehcache _cache;
91
92 }