View Javadoc
1   /*
2    * Copyright 2004-2025 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ibatis.sqlmap.engine.cache.memory;
17  
18  import com.ibatis.sqlmap.client.SqlMapException;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  /**
24   * An enumeration for the values for the memory cache levels.
25   */
26  public final class MemoryCacheLevel {
27  
28    /** The cache level map. */
29    private static Map cacheLevelMap = new HashMap<>();
30  
31    /**
32     * Constant for weak caching This cache model is probably the best choice in most cases. It will increase performance
33     * for popular results, but it will absolutely release the memory to be used in allocating other objects, assuming
34     * that the results are not currently in use.
35     */
36    public final static MemoryCacheLevel WEAK;
37  
38    /**
39     * Constant for soft caching. This cache model will reduce the likelihood of running out of memory in case the results
40     * are not currently in use and the memory is needed for other objects. However, this is not the most aggressive
41     * cache-model in that regard. Hence, memory still might be allocated and unavailable for more important objects.
42     */
43    public final static MemoryCacheLevel SOFT;
44  
45    /**
46     * Constant for strong caching. This cache model will guarantee that the results stay in memory until the cache is
47     * explicitly flushed. This is ideal for results that are:
48     * <ol>
49     * <li>very small</li>
50     * <li>absolutely static</li>
51     * <li>used very often</li>
52     * </ol>
53     * The advantage is that performance will be very good for this particular query. The disadvantage is that if the
54     * memory used by these results is needed, then it will not be released to make room for other objects (possibly more
55     * important objects).
56     */
57    public final static MemoryCacheLevel STRONG;
58  
59    /** The reference type. */
60    private String referenceType;
61  
62    static {
63      WEAK = new MemoryCacheLevel("WEAK");
64      SOFT = new MemoryCacheLevel("SOFT");
65      STRONG = new MemoryCacheLevel("STRONG");
66  
67      cacheLevelMap.put(WEAK.referenceType, WEAK);
68      cacheLevelMap.put(SOFT.referenceType, SOFT);
69      cacheLevelMap.put(STRONG.referenceType, STRONG);
70    }
71  
72    /**
73     * Creates a new instance of CacheLevel.
74     *
75     * @param type
76     *          the type
77     */
78    private MemoryCacheLevel(String type) {
79      this.referenceType = type;
80    }
81  
82    /**
83     * Getter for the reference type.
84     *
85     * @return the type of reference type used
86     */
87    public String getReferenceType() {
88      return this.referenceType;
89    }
90  
91    /**
92     * Gets a MemoryCacheLevel by name.
93     *
94     * @param refType
95     *          the name of the reference type
96     *
97     * @return the MemoryCacheLevel that the name indicates
98     */
99    public static MemoryCacheLevel getByReferenceType(String refType) {
100     MemoryCacheLevel cacheLevel = (MemoryCacheLevel) cacheLevelMap.get(refType);
101     if (cacheLevel == null) {
102       throw new SqlMapException("Error getting CacheLevel (reference type) for name: '" + refType + "'.");
103     }
104     return cacheLevel;
105   }
106 }