-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JEXL-414: added cache interface, synchronized & concurrent implementa…
…tions and factory handling;
- Loading branch information
Henri Biestro
committed
Nov 18, 2023
1 parent
b2431dd
commit e4b6b39
Showing
10 changed files
with
264 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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 org.apache.commons.jexl3; | ||
|
||
import org.apache.commons.jexl3.internal.ConcurrentCache; | ||
import org.apache.commons.jexl3.internal.SoftCache; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.function.IntFunction; | ||
|
||
/** | ||
* Caching scripts or templates interface. | ||
* @param <K> source | ||
* @param <V> script or template | ||
*/ | ||
public interface JexlCache<K, V> { | ||
/** | ||
* Returns the cache size. | ||
* | ||
* @return the cache size | ||
*/ | ||
int size(); | ||
|
||
/** | ||
* Clears the cache. | ||
*/ | ||
void clear(); | ||
|
||
/** | ||
* Gets a value from cache. | ||
* | ||
* @param key the cache entry key | ||
* @return the cache entry value | ||
*/ | ||
V get(K key); | ||
|
||
/** | ||
* Puts a value in cache. | ||
* | ||
* @param key the cache entry key | ||
* @param script the cache entry value | ||
*/ | ||
V put(K key, V script); | ||
|
||
/** | ||
* Produces the cache entry set. | ||
* <p> | ||
* For implementations testing only | ||
* </p> | ||
* @return the cache entry list | ||
*/ | ||
default Collection<Map.Entry<K, V>> entries() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
/** | ||
* @return a synchronized cache factory amenable to low concurrency usage | ||
*/ | ||
static IntFunction<JexlCache<?,?>> createSynchronized() { | ||
return SoftCache::new; | ||
} | ||
|
||
/** | ||
* @return a concurrent cache factory amenable to high concurrency usage | ||
*/ | ||
static IntFunction<JexlCache<?,?>> createConcurrent() { | ||
return ConcurrentCache::new; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/org/apache/commons/jexl3/internal/ConcurrentCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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 org.apache.commons.jexl3.internal; | ||
|
||
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; | ||
|
||
import java.lang.ref.SoftReference; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
/** | ||
* A cache whose underlying map is a ConcurrentLinkedHashMap. | ||
* | ||
* @param <K> the cache key entry type | ||
* @param <V> the cache key value type | ||
*/ | ||
public class ConcurrentCache<K, V> extends SoftCache<K, V> { | ||
/** | ||
* Creates a new instance of a concurrent cache. | ||
* | ||
* @param theSize the cache size | ||
*/ | ||
public ConcurrentCache(final int theSize) { | ||
super(theSize); | ||
} | ||
|
||
@Override | ||
public Collection<Map.Entry<K, V>> entries() { | ||
final SoftReference<Map<K, V>> ref = reference; | ||
final Map<K, V> map = ref != null ? ref.get() : null; | ||
return map == null? Collections.emptyList() : map.entrySet(); | ||
} | ||
|
||
@Override | ||
public <K, V> Map<K, V> createMap(final int cacheSize) { | ||
return new ConcurrentLinkedHashMap.Builder<K, V>() | ||
.concurrencyLevel(Runtime.getRuntime().availableProcessors()) | ||
.maximumWeightedCapacity(cacheSize) | ||
.build(); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.