<commit_before>package com.jdroid.java.http.cache;

import org.slf4j.Logger;
import com.jdroid.java.concurrent.ExecutorUtils;
import com.jdroid.java.parser.Parser;
import com.jdroid.java.utils.LoggerUtils;

/**
 * 
 * @author <NAME>
 */
public enum CachingStrategy {
	
	NO_CACHE {
		
		@Override
		public <T> T execute(CachedWebService cachedWebService, Parser parser) {
			return cachedWebService.executeRequest(parser);
		};
	},
	
	// First try with the cache. If it is a hit, return the cached response, else execute the request
	CACHE_FIRST {
		
		@Override
		public <T> T execute(CachedWebService cachedWebService, Parser parser) {
			T response = cachedWebService.readFromCache(parser);
			if (response == null) {
				response = cachedWebService.executeRequest(parser);
			}
			return response;
		}
	},
	
	// First try with the cache. If it is a hit, return the cached response and then execute an asynchronous request. If
	// is is not a hit, execute the request
	CACHE_FIRST_ASYNCH_REMOTE {
		
		@Override
		public <T> T execute(final CachedWebService cachedWebService, final Parser parser) {
			T response = cachedWebService.readFromCache(parser);
			if (response == null) {
				response = cachedWebService.executeRequest(parser);
			} else {
				ExecutorUtils.execute(new Runnable() {
					
					@Override
					public void run() {
						try {
							cachedWebService.executeRequest(parser);
						} catch (Exception e) {
							LOGGER.warn("Error when executing asynch request.", e);
							// TODO We should logg the exception on the handler
							// AbstractApplication.get().getExceptionHandler().logHandledException(e);
						}
					}
				});
			}
			return response;
		}
	},
	
	// First try with the cache. If it is a hit (doesn't matter if it is expired or not), return the cached response and
	// then execute an asynchronous request. If
	// is is not a hit, execute the request only if the cache doesn't exist or it is expired
	CACHE_FORCED_FIRST_ASYNCH_REMOTE {
		
		@Override
		public <T> T execute(final CachedWebService cachedWebService, final Parser parser) {
			Long originalTimeToLive = cachedWebService.getTimeToLive();
			cachedWebService.setTimeToLive(null);
			T response = cachedWebService.readFromCache(parser);
			if (response == null) {
				response = cachedWebService.executeRequest(parser);
			} else {
				cachedWebService.setTimeToLive(originalTimeToLive);
				ExecutorUtils.execute(new Runnable() {
					
					@Override
					public void run() {
						try {
							CachingStrategy.CACHE_FIRST.execute(cachedWebService, parser);
						} catch (Exception e) {<commit_msg>Comment improvement<commit_after>package com.jdroid.java.http.cache;

import org.slf4j.Logger;
import com.jdroid.java.concurrent.ExecutorUtils;
import com.jdroid.java.parser.Parser;
import com.jdroid.java.utils.LoggerUtils;

/**
 * 
 * @author <NAME>
 */
public enum CachingStrategy {
	
	NO_CACHE {
		
		@Override
		public <T> T execute(CachedWebService cachedWebService, Parser parser) {
			return cachedWebService.executeRequest(parser);
		};
	},
	
	// First try with the cache. If it is a hit, return the cached response, else execute the request
	CACHE_FIRST {
		
		@Override
		public <T> T execute(CachedWebService cachedWebService, Parser parser) {
			T response = cachedWebService.readFromCache(parser);
			if (response == null) {
				response = cachedWebService.executeRequest(parser);
			}
			return response;
		}
	},
	
	// First try with the cache. If it is a hit, return the cached response and then execute an asynchronous request. If
	// is is not a hit, execute the request
	CACHE_FIRST_ASYNCH_REMOTE {
		
		@Override
		public <T> T execute(final CachedWebService cachedWebService, final Parser parser) {
			T response = cachedWebService.readFromCache(parser);
			if (response == null) {
				response = cachedWebService.executeRequest(parser);
			} else {
				ExecutorUtils.execute(new Runnable() {
					
					@Override
					public void run() {
						try {
							cachedWebService.executeRequest(parser);
						} catch (Exception e) {
							LOGGER.warn("Error when executing asynch request.", e);
							// TODO We should logg the exception on the handler
							// AbstractApplication.get().getExceptionHandler().logHandledException(e);
						}
					}
				});
			}
			return response;
		}
	},
	
	// If there isn't anything on the cache, then execute the request caching the response.
	// If there is something in the cache (doesn't matter if it is expired or not) return the cache content. Then
	// execute an asynchronous request (only if the cache is expired) caching the response.
	CACHE_FORCED_FIRST_ASYNCH_REMOTE {
		
		@Override
		public <T> T execute(final CachedWebService cachedWebService, final Parser parser) {
			Long originalTimeToLive = cachedWebService.getTimeToLive();
			cachedWebService.setTimeToLive(null);
			T response = cachedWebService.readFromCache(parser);
			if (response == null) {
				response = cachedWebService.executeRequest(parser);
			} else {
				cachedWebService.setTimeToLive(originalTimeToLive);
				ExecutorUtils.execute(new Runnable() {
					
					@Override
					public void run() {
						try {
							CachingStrategy.CACHE_FIRST.execute(cachedWebService, parser);
						} catch (Exception e) {
{'fraction_non_alphanumeric': 0.09208523592085235, 'fraction_numerical': 0.000380517503805175, 'mean_word_length': 3.5202063628546862, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5630658', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1494, 'n_words': 451}