ben-manes commented on code in PR #766:
URL: https://github.com/apache/struts/pull/766#discussion_r1359917925


##########
core/src/main/java/com/opensymphony/xwork2/ognl/OgnlCaffeineCache.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2022 Apache Software Foundation.
+ *
+ * Licensed 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 com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+
+/**
+ * <p>This OGNL Cache implementation is backed by {@link Caffeine} which uses 
the Window TinyLfu algorithm.</p>
+ *
+ * <p>An appropriate eviction limit should be chosen for your specific 
application based on factors and requirements
+ * such as:</p>
+ * <ul>
+ *     <li>Quantity and complexity of actions</li>
+ *     <li>Volume of requests</li>
+ *     <li>Rate limits and attack potential/patterns</li>
+ *     <li>Memory constraints</li>
+ * </ul>
+ *
+ * @param <K> The type for the cache key entries
+ * @param <V> The type for the cache value entries
+ */
+public class OgnlCaffeineCache<K, V> implements OgnlCache<K, V> {
+
+    private final Cache<K, V> cache;
+    private final int evictionLimit;
+
+    public OgnlCaffeineCache(int evictionLimit, int initialCapacity) {
+        this.evictionLimit = evictionLimit;
+        this.cache = 
Caffeine.newBuilder().initialCapacity(initialCapacity).maximumSize(evictionLimit).build();
+    }
+
+    @Override
+    public V get(K key) {
+        return cache.getIfPresent(key);
+    }
+
+    @Override
+    public void put(K key, V value) {
+        cache.put(key, value);
+    }
+
+    @Override
+    public void putIfAbsent(K key, V value) {
+        if (cache.getIfPresent(key) == null) {
+            cache.put(key, value);
+        }

Review Comment:
   `cache.asMap().putIfAbsent(key, value)`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@struts.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to