1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.jexl3.internal;
18
19 import java.lang.ref.SoftReference;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Map;
23
24 import org.apache.commons.jexl3.JexlCache;
25
26 /**
27 * A soft referenced cache.
28 * <p>
29 * The actual cache is held through a soft reference, allowing it to be GCed
30 * under memory pressure.
31 * </p>
32 * <p>
33 * Note that the underlying map is a synchronized LinkedHashMap.
34 * The reason is that a get() will reorder elements (the LRU queue) and thus
35 * needs synchronization to ensure thread-safety.
36 * </p>
37 * <p>
38 * When caching JEXL scripts or expressions, one should expect the execution cost of those
39 * to be several fold the cost of the cache handling; after some (synthetic) tests, measures indicate
40 * cache handling is a marginal latency factor.
41 * </p>
42 *
43 * @param <K> the cache key entry type
44 * @param <V> the cache key value type
45 */
46 public class SoftCache<K, V> implements JexlCache<K, V> {
47
48 /**
49 * The default cache load factor.
50 */
51 protected static final float LOAD_FACTOR = 0.75f;
52
53 /**
54 * Creates a synchronized LinkedHashMap.
55 *
56 * @param capacity the map capacity
57 * @return the map instance
58 * @param <K> key type
59 * @param <V> value type
60 */
61 public static <K, V> Map<K, V> createSynchronizedLinkedHashMap(final int capacity) {
62 return Collections.synchronizedMap(new java.util.LinkedHashMap<K, V>(capacity, LOAD_FACTOR, true) {
63
64 /**
65 * Serial version UID.
66 */
67 private static final long serialVersionUID = 1L;
68
69 @Override
70 protected boolean removeEldestEntry(final Map.Entry<K, V> eldest) {
71 return super.size() > capacity;
72 }
73 });
74 }
75
76 /**
77 * The cache capacity.
78 */
79 protected final int capacity;
80
81 /**
82 * The soft reference to the cache map.
83 */
84 protected volatile SoftReference<Map<K, V>> reference;
85
86 /**
87 * Creates a new instance of a soft cache.
88 *
89 * @param theSize the cache size
90 */
91 public SoftCache(final int theSize) {
92 capacity = theSize;
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 @Override
99 public int capacity() {
100 return capacity;
101 }
102
103 /**
104 * {@inheritDoc}
105 */
106 @Override
107 public void clear() {
108 final SoftReference<Map<K, V>> ref = reference;
109 if (ref != null) {
110 reference = null;
111 final Map<K, V> map = ref.get();
112 if (map != null) {
113 map.clear();
114 }
115 }
116 }
117
118 /**
119 * Creates a cache store.
120 *
121 * @param <KT> the key type
122 * @param <VT> the value type
123 * @param cacheSize the cache size, must be > 0
124 * @return a Map usable as a cache bounded to the given size
125 */
126 protected <KT, VT> Map<KT, VT> createMap(final int cacheSize) {
127 return createSynchronizedLinkedHashMap(cacheSize);
128 }
129
130 /**
131 * {@inheritDoc}
132 */
133 @Override
134 public Collection<Map.Entry<K, V>> entries() {
135 final SoftReference<Map<K, V>> ref = reference;
136 final Map<K, V> map = ref != null ? ref.get() : null;
137 return map == null? Collections.emptyList() : map.entrySet();
138 }
139
140 /**
141 * {@inheritDoc}
142 */
143 @Override
144 public V get(final K key) {
145 final SoftReference<Map<K, V>> ref = reference;
146 final Map<K, V> map = ref != null ? ref.get() : null;
147 return map != null ? map.get(key) : null;
148 }
149
150 /**
151 * {@inheritDoc}
152 */
153 @Override
154 public V put(final K key, final V script) {
155 SoftReference<Map<K, V>> ref = reference;
156 Map<K, V> map = ref != null ? ref.get() : null;
157 if (map == null) {
158 synchronized (this) {
159 ref = reference;
160 map = ref != null ? ref.get() : null;
161 if (map == null) {
162 map = createMap(capacity);
163 reference = new SoftReference<>(map);
164 }
165 }
166 }
167 return map.put(key, script);
168 }
169
170 /**
171 * {@inheritDoc}
172 */
173 @Override
174 public int size() {
175 final SoftReference<Map<K, V>> ref = reference;
176 final Map<K, V> map = ref != null ? ref.get() : null;
177 return map != null ? map.size() : 0;
178 }
179 }
180