1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.cache.ehcache;
16  
17  import com.liferay.portal.kernel.cache.BasePortalCache;
18  
19  import java.io.Serializable;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import net.sf.ehcache.Ehcache;
26  import net.sf.ehcache.Element;
27  
28  /**
29   * <a href="EhcachePortalCache.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class EhcachePortalCache extends BasePortalCache {
34  
35      public EhcachePortalCache(Ehcache cache) {
36          _cache = cache;
37      }
38  
39      public Object get(String key) {
40          String processedKey = processKey(key);
41  
42          Element element = _cache.get(processedKey);
43  
44          if (element == null) {
45              return null;
46          }
47          else {
48              return element.getObjectValue();
49          }
50      }
51  
52      public Collection<Object> get(Collection<String> keys) {
53          List<Object> values = new ArrayList<Object>(keys.size());
54  
55          for (String key : keys) {
56              values.add(get(key));
57          }
58  
59          return values;
60      }
61  
62      public void put(String key, Object obj) {
63          Element element = createElement(key, obj);
64  
65          _cache.put(element);
66      }
67  
68      public void put(String key, Object obj, int timeToLive) {
69          Element element = createElement(key, obj);
70  
71          element.setTimeToLive(timeToLive);
72  
73          _cache.put(element);
74      }
75  
76      public void put(String key, Serializable obj) {
77          Element element = createElement(key, obj);
78  
79          _cache.put(element);
80      }
81  
82      public void put(String key, Serializable obj, int timeToLive) {
83          Element element = createElement(key, obj);
84  
85          element.setTimeToLive(timeToLive);
86  
87          _cache.put(element);
88      }
89  
90      public void remove(String key) {
91          String processedKey = processKey(key);
92  
93          _cache.remove(processedKey);
94      }
95  
96      public void removeAll() {
97          _cache.removeAll();
98      }
99  
100     protected Element createElement(String key, Object obj) {
101         String processedKey = processKey(key);
102 
103         Element element = new Element(processedKey, obj);
104 
105         return element;
106     }
107 
108     private Ehcache _cache;
109 
110 }