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.memory;
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  import java.util.Map;
25  import java.util.concurrent.ConcurrentHashMap;
26  
27  /**
28   * <a href="MemoryPortalCache.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class MemoryPortalCache extends BasePortalCache {
33  
34      public MemoryPortalCache(int initialCapacity) {
35           _map = new ConcurrentHashMap<String, Object>(initialCapacity);
36      }
37  
38      public Collection<Object> get(Collection<String> keys) {
39          List<Object> values = new ArrayList<Object>(keys.size());
40  
41          for (String key : keys) {
42              values.add(get(key));
43          }
44  
45          return values;
46      }
47  
48      public Object get(String key) {
49          String processedKey = processKey(key);
50  
51          return _map.get(processedKey);
52      }
53  
54      public void put(String key, Object obj) {
55          String processedKey = processKey(key);
56  
57          _map.put(processedKey, obj);
58      }
59  
60      public void put(String key, Object obj, int timeToLive) {
61          String processedKey = processKey(key);
62  
63          _map.put(processedKey, obj);
64      }
65  
66      public void put(String key, Serializable obj) {
67          String processedKey = processKey(key);
68  
69          _map.put(processedKey, obj);
70      }
71  
72      public void put(String key, Serializable obj, int timeToLive) {
73          String processedKey = processKey(key);
74  
75          _map.put(processedKey, obj);
76      }
77  
78      public void remove(String key) {
79          String processedKey = processKey(key);
80  
81          _map.remove(processedKey);
82      }
83  
84      public void removeAll() {
85          _map.clear();
86      }
87  
88      private Map<String, Object> _map;
89  
90  }