![JAR search and dependency download from the Maven repository](/logo.png)
org.languagetool.ResultCache Maven / Gradle / Ivy
/* LanguageTool, a natural language style checker
* Copyright (C) 2017 Daniel Naber (http://www.danielnaber.de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package org.languagetool;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.Weigher;
import org.languagetool.rules.RuleMatch;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* A cache to speed up text checking for use cases where sentences are checked more than once. This
* typically happens when using LT as a server and texts get re-checked after corrections have been applied
* for some sentences. Use the same cache object for all {@link JLanguageTool} objects only if
* the JLanguageTool objects all use the same rules. For example, if you call {@code JLanguageTool.addRule()}
* in different ways for the different instances that you use the same cache for, the cache will return invalid results.
* Using a cache with bitext rules isn't supported either.
* It is okay however, to use the same cache for {@link JLanguageTool} objects with different languages, as
* cached results are not used for a different language.
* @since 3.7
*/
public class ResultCache {
private final Cache> matchesCache;
private final Cache sentenceCache;
/**
* Create a cache that expires items 5 minutes after the latest read access.
* @param maxSize maximum cache size in number of sentences
*/
public ResultCache(long maxSize) {
this(maxSize, 5, TimeUnit.MINUTES);
}
/**
* @param maxSize maximum cache size in number of sentences
* @param expireAfter time to expire sentences from the cache after last read access
*/
public ResultCache(long maxSize, int expireAfter, TimeUnit timeUnit) {
if (maxSize < 0) {
throw new IllegalArgumentException("Result cache size must be >= 0: " + maxSize);
}
matchesCache = CacheBuilder.newBuilder().
maximumWeight(maxSize/2).weigher(new MatchesWeigher()).
recordStats().
expireAfterAccess(expireAfter, timeUnit).
build();
sentenceCache = CacheBuilder.newBuilder().
maximumWeight(maxSize/2).weigher(new SentenceWeigher()).
recordStats().
expireAfterAccess(expireAfter, timeUnit).
build();
}
class MatchesWeigher implements Weigher> {
@Override
public int weigh(InputSentence sentence, List matches) {
// this is just a rough guesstimate so that the cacheSize given by the user
// is very roughly the number of average sentences the cache can keep:
return sentence.getText().length() / 75 + matches.size();
}
}
class SentenceWeigher implements Weigher {
@Override
public int weigh(SimpleInputSentence sentence, AnalyzedSentence analyzedSentence) {
return sentence.getText().length() / 75;
}
}
public double hitRate() {
return (matchesCache.stats().hitRate() + sentenceCache.stats().hitRate()) / 2.0;
}
public double requestCount() {
return matchesCache.stats().requestCount() + sentenceCache.stats().requestCount();
}
public long hitCount() {
return matchesCache.stats().hitCount() + sentenceCache.stats().hitCount();
}
public List getIfPresent(InputSentence key) {
return matchesCache.getIfPresent(key);
}
public AnalyzedSentence getIfPresent(SimpleInputSentence key) {
return sentenceCache.getIfPresent(key);
}
public void put(InputSentence key, List sentenceMatches) {
matchesCache.put(key, sentenceMatches);
}
public void put(SimpleInputSentence key, AnalyzedSentence aSentence) {
sentenceCache.put(key, aSentence);
}
/** @since 4.1 */
protected Cache> getMatchesCache() {
return matchesCache;
}
/** @since 4.1 */
protected Cache getSentenceCache() {
return sentenceCache;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy