This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch WW-5668-i18n-cache-bounds-6x in repository https://gitbox.apache.org/repos/asf/struts.git
commit e993af3caacb2eb8614d1d54f4c9e7b383b77729 Author: Lukasz Lenart <[email protected]> AuthorDate: Sat Aug 1 09:13:29 2026 +0200 WW-5668 Add remove(key) to the OgnlCache abstraction Co-Authored-By: Claude Opus 5 <[email protected]> --- .../com/opensymphony/xwork2/ognl/OgnlCache.java | 9 ++++ .../xwork2/ognl/OgnlCaffeineCache.java | 5 +++ .../opensymphony/xwork2/ognl/OgnlDefaultCache.java | 5 +++ .../com/opensymphony/xwork2/ognl/OgnlLRUCache.java | 5 +++ .../xwork2/ognl/OgnlCacheRemoveTest.java | 50 ++++++++++++++++++++++ 5 files changed, 74 insertions(+) diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCache.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCache.java index fc8366699..908e18be7 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCache.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCache.java @@ -31,6 +31,15 @@ public interface OgnlCache<Key, Value> { void putIfAbsent(Key key, Value value); + /** + * Removes the mapping for the given key, if present. + * + * @param key the key to remove + * @return the previous value associated with the key, or {@code null} if none + * @since 6.11.0 + */ + Value remove(Key key); + int size(); void clear(); diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCaffeineCache.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCaffeineCache.java index b7a0241e3..9e2040176 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCaffeineCache.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCaffeineCache.java @@ -56,6 +56,11 @@ public class OgnlCaffeineCache<K, V> implements OgnlCache<K, V> { cache.asMap().putIfAbsent(key, value); } + @Override + public V remove(K key) { + return cache.asMap().remove(key); + } + @Override public int size() { return cache.asMap().size(); diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlDefaultCache.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlDefaultCache.java index 920403a5d..c631b8cb0 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlDefaultCache.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlDefaultCache.java @@ -57,6 +57,11 @@ public class OgnlDefaultCache<K, V> implements OgnlCache<K, V> { this.clearIfEvictionLimitExceeded(); } + @Override + public V remove(K key) { + return ognlCache.remove(key); + } + @Override public int size() { return ognlCache.size(); diff --git a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlLRUCache.java b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlLRUCache.java index e324418ce..cbc5461f3 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlLRUCache.java +++ b/core/src/main/java/com/opensymphony/xwork2/ognl/OgnlLRUCache.java @@ -64,6 +64,11 @@ public class OgnlLRUCache<K, V> implements OgnlCache<K, V> { ognlLRUCache.putIfAbsent(key, value); } + @Override + public V remove(K key) { + return ognlLRUCache.remove(key); + } + @Override public int size() { return ognlLRUCache.size(); diff --git a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlCacheRemoveTest.java b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlCacheRemoveTest.java new file mode 100644 index 000000000..c637bc41f --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlCacheRemoveTest.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.opensymphony.xwork2.ognl; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class OgnlCacheRemoveTest { + + private void assertRemoveContract(OgnlCache<String, String> cache) { + cache.put("k", "v"); + assertEquals("v", cache.get("k")); + assertEquals("remove returns previous value", "v", cache.remove("k")); + assertNull("entry gone after remove", cache.get("k")); + assertNull("remove of absent key returns null", cache.remove("absent")); + } + + @Test + public void caffeineCacheRemove() { + assertRemoveContract(new OgnlCaffeineCache<>(10, 16)); + } + + @Test + public void defaultCacheRemove() { + assertRemoveContract(new OgnlDefaultCache<>(10, 16, 0.75f)); + } + + @Test + public void lruCacheRemove() { + assertRemoveContract(new OgnlLRUCache<>(10, 16, 0.75f)); + } +}
