Skip to content

Commit

Permalink
WW-5355 Initial Caffeine cache implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Oct 15, 2023
1 parent 5011a79 commit 9527da5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@
<artifactId>freemarker</artifactId>
</dependency>

<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <Key> The type for the cache key entries
* @param <Value> The type for the cache value entries
*/
public class OgnlCaffeineCache<Key, Value> implements OgnlCache<Key, Value> {

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

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

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

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

@Override
public void putIfAbsent(Key key, Value value) {
if (cache.getIfPresent(key) == null) {
cache.put(key, value);
}
}

@Override
public int size() {
return Math.toIntExact(cache.estimatedSize());
}

@Override
public void clear() {
cache.invalidateAll();
}

@Override
public int getEvictionLimit() {
return evictionLimit;
}

@Override
public void setEvictionLimit(int cacheEvictionLimit) {
throw new UnsupportedOperationException("Cannot change eviction limit on a Caffeine cache after initialisation");
}
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,12 @@
<version>${freemarker.version}</version>
</dependency>

<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.1.8</version>
</dependency>

<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
Expand Down

0 comments on commit 9527da5

Please sign in to comment.