Skip to content

Commit

Permalink
make name cache configurabl (OpenAPITools#5775)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-rosset authored and michaelpro1 committed May 7, 2020
1 parent 3aa8ba3 commit 4975db4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ public class DefaultCodegen implements CodegenConfig {
)
.build();

int cacheSize = Integer.parseInt(GlobalSettings.getProperty(NAME_CACHE_SIZE_PROPERTY, "500"));
int cacheExpiry = Integer.parseInt(GlobalSettings.getProperty(NAME_CACHE_EXPIRY_PROPERTY, "10"));
sanitizedNameCache = Caffeine.newBuilder()
.maximumSize(500)
.expireAfterAccess(10, TimeUnit.SECONDS)
.maximumSize(cacheSize)
.expireAfterAccess(cacheExpiry, TimeUnit.SECONDS)
.ticker(Ticker.systemTicker())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.benmanes.caffeine.cache.Ticker;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.openapitools.codegen.config.GlobalSettings;

import java.util.List;
import java.util.Locale;
Expand All @@ -16,22 +17,33 @@

public class StringUtils {

/**
* Modify the cache size of the sanitizedNameCache, camelizedWordsCache and underscoreWords.
*/
public static final String NAME_CACHE_SIZE_PROPERTY = "org.openapitools.codegen.utils.namecache.cachesize";
/**
* Modify the cache expiry of the sanitizedNameCache, camelizedWordsCache and underscoreWords.
*/
public static final String NAME_CACHE_EXPIRY_PROPERTY = "org.openapitools.codegen.utils.namecache.expireafter";

// A cache of camelized words. The camelize() method is invoked many times with the same
// arguments, this cache is used to optimized performance.
private static Cache<Pair<String, Boolean>, String> camelizedWordsCache;

// A cache of underscored words, used to optimize the performance of the underscore() method.
private static Cache<String, String> underscoreWords;
static {
int cacheSize = Integer.parseInt(GlobalSettings.getProperty(NAME_CACHE_SIZE_PROPERTY, "200"));
int cacheExpiry = Integer.parseInt(GlobalSettings.getProperty(NAME_CACHE_EXPIRY_PROPERTY, "5"));
camelizedWordsCache = Caffeine.newBuilder()
.maximumSize(200)
.expireAfterAccess(5, TimeUnit.SECONDS)
.maximumSize(cacheSize)
.expireAfterAccess(cacheExpiry, TimeUnit.SECONDS)
.ticker(Ticker.systemTicker())
.build();

underscoreWords = Caffeine.newBuilder()
.maximumSize(200)
.expireAfterAccess(5, TimeUnit.SECONDS)
.maximumSize(cacheSize)
.expireAfterAccess(cacheExpiry, TimeUnit.SECONDS)
.ticker(Ticker.systemTicker())
.build();
}
Expand Down

0 comments on commit 4975db4

Please sign in to comment.