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.kernel.cache;
16  
17  import com.liferay.portal.kernel.util.InitialThreadLocal;
18  
19  import java.util.EnumMap;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  /**
24   * <a href="ThreadLocalCacheManager.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Shuyang Zhou
27   */
28  public class ThreadLocalCacheManager {
29  
30      public static void clearAll(Lifecycle lifecycle) {
31          Map<Lifecycle, Map<String, ThreadLocalCache<?>>> threadLocalCacheMaps =
32              _threadLocalCacheMaps.get();
33  
34          Map<String, ThreadLocalCache<?>> threadLocalCacheMap =
35              threadLocalCacheMaps.get(lifecycle);
36  
37          if (threadLocalCacheMap != null) {
38              threadLocalCacheMap.clear();
39          }
40      }
41  
42      public static void destroy() {
43          _threadLocalCacheMaps.remove();
44      }
45  
46      public static <T> ThreadLocalCache<T> getThreadLocalCache(
47          Lifecycle lifecycle, String name) {
48  
49          Map<Lifecycle, Map<String, ThreadLocalCache<?>>> threadLocalCacheMaps =
50              _threadLocalCacheMaps.get();
51  
52          Map<String, ThreadLocalCache<?>> threadLocalCacheMap =
53              threadLocalCacheMaps.get(lifecycle);
54  
55          if (threadLocalCacheMap == null) {
56              threadLocalCacheMap = new HashMap<String, ThreadLocalCache<?>>();
57  
58              threadLocalCacheMaps.put(lifecycle, threadLocalCacheMap);
59          }
60  
61          ThreadLocalCache<?> threadLocalCache = threadLocalCacheMap.get(name);
62  
63          if (threadLocalCache == null) {
64              threadLocalCache = new ThreadLocalCache<T>(name, lifecycle);
65  
66              threadLocalCacheMap.put(name, threadLocalCache);
67          }
68  
69          return (ThreadLocalCache<T>)threadLocalCache;
70      }
71  
72      private static ThreadLocal<Map<Lifecycle, Map<String, ThreadLocalCache<?>>>>
73          _threadLocalCacheMaps = new InitialThreadLocal
74              <Map<Lifecycle, Map<String, ThreadLocalCache<?>>>>(
75                  new EnumMap<Lifecycle, Map<String, ThreadLocalCache<?>>>(
76                      Lifecycle.class));
77  
78  }