Skip to content

Commit

Permalink
WW-5355 Address code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Oct 15, 2023
1 parent 7463e1d commit 28cc645
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public OgnlCache<Key, Value> buildOgnlCache(int evictionLimit,
case SYNC_LINKED_LRU:
return new OgnlLRUCache<>(evictionLimit, initialCapacity, loadFactor);
case CAFFEINE_WTLFU:
return new OgnlCaffeineCache<>(evictionLimit, initialCapacity, loadFactor);
return new OgnlCaffeineCache<>(evictionLimit, initialCapacity);
default:
throw new IllegalArgumentException("Unknown cache type: " + cacheType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@
* <li>Memory constraints</li>
* </ul>
*
* @param <Key> The type for the cache key entries
* @param <Value> The type for the cache value entries
* @param <K> The type for the cache key entries
* @param <V> The type for the cache value entries
*/
public class OgnlCaffeineCache<Key, Value> implements OgnlCache<Key, Value> {
public class OgnlCaffeineCache<K, V> implements OgnlCache<K, V> {

private final Cache<Key, Value> cache;
private final Cache<K, V> cache;
private final int evictionLimit;

public OgnlCaffeineCache(int evictionLimit, int initialCapacity, float loadFactor) {
public OgnlCaffeineCache(int evictionLimit, int initialCapacity) {
this.evictionLimit = evictionLimit;
this.cache = Caffeine.newBuilder().initialCapacity(initialCapacity).maximumSize(evictionLimit).build();
}

@Override
public Value get(Key key) {
public V get(K key) {
return cache.getIfPresent(key);
}

@Override
public void put(Key key, Value value) {
public void put(K key, V value) {
cache.put(key, value);
}

@Override
public void putIfAbsent(Key key, Value value) {
public void putIfAbsent(K key, V value) {
if (cache.getIfPresent(key) == null) {
cache.put(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
* <p>Setting a very high eviction limit simulates an unlimited cache.</p>
* <p>Setting too low an eviction limit will make the cache ineffective.</p>
*
* @param <Key> The type for the cache key entries
* @param <Value> The type for the cache value entries
* @param <K> The type for the cache key entries
* @param <V> The type for the cache value entries
*/
public class OgnlDefaultCache<Key, Value> implements OgnlCache<Key, Value> {
public class OgnlDefaultCache<K, V> implements OgnlCache<K, V> {

private final ConcurrentHashMap<Key, Value> ognlCache;
private final ConcurrentHashMap<K, V> ognlCache;
private final AtomicInteger cacheEvictionLimit;

public OgnlDefaultCache(int evictionLimit, int initialCapacity, float loadFactor) {
Expand All @@ -41,18 +41,18 @@ public OgnlDefaultCache(int evictionLimit, int initialCapacity, float loadFactor
}

@Override
public Value get(Key key) {
public V get(K key) {
return ognlCache.get(key);
}

@Override
public void put(Key key, Value value) {
public void put(K key, V value) {
ognlCache.put(key, value);
this.clearIfEvictionLimitExceeded();
}

@Override
public void putIfAbsent(Key key, Value value) {
public void putIfAbsent(K key, V value) {
ognlCache.putIfAbsent(key, value);
this.clearIfEvictionLimitExceeded();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@
* <p>Setting too high an eviction limit may also produce more overhead than value.</p>
* <p>An appropriate eviction limit will need to be determined on an individual application basis.</p>
*
* @param <Key> The type for the cache key entries
* @param <Value> The type for the cache value entries
* @param <K> The type for the cache key entries
* @param <V> The type for the cache value entries
*/
public class OgnlLRUCache<Key, Value> implements OgnlCache<Key, Value> {
public class OgnlLRUCache<K, V> implements OgnlCache<K, V> {

private final Map<Key, Value> ognlLRUCache;
private final Map<K, V> ognlLRUCache;
private final AtomicInteger cacheEvictionLimit;

public OgnlLRUCache(int evictionLimit, int initialCapacity, float loadFactor) {
cacheEvictionLimit = new AtomicInteger(evictionLimit);
// Access-order mode selected (order mode true in LinkedHashMap constructor).
ognlLRUCache = Collections.synchronizedMap(new LinkedHashMap<Key, Value>(initialCapacity, loadFactor, true) {
ognlLRUCache = Collections.synchronizedMap(new LinkedHashMap<K, V>(initialCapacity, loadFactor, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<Key, Value> eldest) {
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > cacheEvictionLimit.get();
}
});
}

@Override
public Value get(Key key) {
public V get(K key) {
return ognlLRUCache.get(key);
}

@Override
public void put(Key key, Value value) {
public void put(K key, V value) {
ognlLRUCache.put(key, value);
}

@Override
public void putIfAbsent(Key key, Value value) {
public void putIfAbsent(K key, V value) {
ognlLRUCache.putIfAbsent(key, value);
}

Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/apache/struts2/StrutsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public final class StrutsConstants {
* @since 6.0.0
* @deprecated since 6.4.0, use {@link StrutsConstants#STRUTS_OGNL_BEANINFO_CACHE_TYPE} instead.
*/
@Deprecated
public static final String STRUTS_OGNL_BEANINFO_CACHE_LRU_MODE = "struts.ognl.beanInfoCacheLRUMode";

/**
Expand Down

0 comments on commit 28cc645

Please sign in to comment.