1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.cache.ehcache;
16  
17  import com.liferay.portal.kernel.cache.PortalCache;
18  
19  import java.io.Serializable;
20  
21  import net.sf.ehcache.Ehcache;
22  import net.sf.ehcache.Element;
23  
24  /**
25   * <a href="EhcachePortalCache.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class EhcachePortalCache implements PortalCache {
30  
31      public EhcachePortalCache(Ehcache cache) {
32          _cache = cache;
33      }
34  
35      public Object get(String key) {
36          Element element = _cache.get(key);
37  
38          if (element == null) {
39              return null;
40          }
41          else {
42              return element.getObjectValue();
43          }
44      }
45  
46      public void put(String key, Object obj) {
47          Element element = new Element(key, obj);
48  
49          _cache.put(element);
50      }
51  
52      public void put(String key, Object obj, int timeToLive) {
53          Element element = new Element(key, obj);
54  
55          element.setTimeToLive(timeToLive);
56  
57          _cache.put(element);
58      }
59  
60      public void put(String key, Serializable obj) {
61          Element element = new Element(key, obj);
62  
63          _cache.put(element);
64      }
65  
66      public void put(String key, Serializable obj, int timeToLive) {
67          Element element = new Element(key, obj);
68  
69          element.setTimeToLive(timeToLive);
70  
71          _cache.put(element);
72      }
73  
74      public void remove(String key) {
75          _cache.remove(key);
76      }
77  
78      public void removeAll() {
79          _cache.removeAll();
80      }
81  
82      private Ehcache _cache;
83  
84  }