<commit_before>package org.ambraproject.wombat.config;

import org.ambraproject.wombat.model.TaxonomyCountTable;
import org.ambraproject.wombat.model.TaxonomyGraph;
import org.ambraproject.wombat.service.remote.RemoteCacheKey;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

class CacheManagerWrapper implements ServiceCacheSet {

  private final CacheManager manager;

  private static <K, V> Cache<K, V> constructCache(CacheManager manager, String cacheName,
                                                   Class<K> keyType, Class<V> valueType,
                                                   Consumer<MutableConfiguration<K, V>> configurationConsumer) {
    MutableConfiguration<K, V> configuration = new MutableConfiguration<>();
    configuration.setTypes(keyType, valueType);
    configurationConsumer.accept(configuration);
    return manager.createCache(cacheName, configuration);
  }


  private static final String ASSET_FILENAME_CACHE = "assetFilename";
  private static final String ASSET_CONTENT_CACHE = "assetContent";
  private static final String TAXONOMY_GRAPH_CACHE = "taxonomyGraph";
  private static final String TAXONOMY_COUNT_TABLE_CACHE = "taxonomyCountTable";
  private static final String RECENT_ARTICLE_CACHE = "recentArticle";

  static final Duration DEFAULT_TTL = new Duration(TimeUnit.HOURS, 1);

  CacheManagerWrapper() {
    manager = Caching.getCachingProvider().getCacheManager();

    constructCache(manager, ASSET_FILENAME_CACHE, String.class, String.class, config -> {
      config.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 15)));
    });

    constructCache(manager, ASSET_CONTENT_CACHE, String.class, Object.class, config -> {
      config.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 15)));
    });<commit_msg>DPRO-2637: Add hard-coded maxBytesLocalHeap=2g property<commit_after>package org.ambraproject.wombat.config;

import org.ambraproject.wombat.model.TaxonomyCountTable;
import org.ambraproject.wombat.model.TaxonomyGraph;
import org.ambraproject.wombat.service.remote.RemoteCacheKey;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

class CacheManagerWrapper implements ServiceCacheSet {

  private final CacheManager manager;

  private static <K, V> Cache<K, V> constructCache(CacheManager manager, String cacheName,
                                                   Class<K> keyType, Class<V> valueType,
                                                   Consumer<MutableConfiguration<K, V>> configurationConsumer) {
    MutableConfiguration<K, V> configuration = new MutableConfiguration<>();
    configuration.setTypes(keyType, valueType);
    configurationConsumer.accept(configuration);
    return manager.createCache(cacheName, configuration);
  }


  private static final String ASSET_FILENAME_CACHE = "assetFilename";
  private static final String ASSET_CONTENT_CACHE = "assetContent";
  private static final String TAXONOMY_GRAPH_CACHE = "taxonomyGraph";
  private static final String TAXONOMY_COUNT_TABLE_CACHE = "taxonomyCountTable";
  private static final String RECENT_ARTICLE_CACHE = "recentArticle";

  static final Duration DEFAULT_TTL = new Duration(TimeUnit.HOURS, 1);

  CacheManagerWrapper() {
    manager = Caching.getCachingProvider().getCacheManager();
    manager.getProperties().setProperty("maxBytesLocalHeap", "2g"); // TODO: Extract into configuration

    constructCache(manager, ASSET_FILENAME_CACHE, String.class, String.class, config -> {
      config.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 15)));
    });

    constructCache(manager, ASSET_CONTENT_CACHE, String.class, Object.class, config -> {
      config.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 15)));
    });
{'fraction_non_alphanumeric': 0.0903476669716377, 'fraction_numerical': 0.0036596523330283625, 'mean_word_length': 4.694010416666667, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4265148', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1178, 'n_words': 246}