<commit_before>package de.domisum.lib.auxilium.contracts.source.implementations.cache;

import de.domisum.lib.auxilium.contracts.source.SingleItemSource;
import de.domisum.lib.auxilium.util.java.annotations.API;
import lombok.RequiredArgsConstructor;

import java.time.Duration;
import java.time.Instant;
import java.util.Optional;

@API
@RequiredArgsConstructor
public class SingleItemSourceCache<T> implements SingleItemSource<T>
{

	// REFERENCES
	private final Duration invalidationInterval;
	private final SingleItemSource<T> backingSource;

	// CACHE
	private T cachedItem;
	private Instant lastFetchFromBackingSource;


	// SOURCE
	@Override public synchronized Optional<T> fetch()
	{
		if(shouldInvalidateCache())
			invalidateCache();

		if(cachedItem == null)
			fetchFromBackingSource();

		return Optional.ofNullable(cachedItem);
	}


	// CACHE
	private void fetchFromBackingSource()
	{
		Optional<T> fromBackingSource = backingSource.fetch();
		if(!fromBackingSource.isPresent())
			return;

		cachedItem = fromBackingSource.get();
		lastFetchFromBackingSource = Instant.now();
	}

	private void invalidateCache()
	{
		cachedItem = null;
	}


	// CONDITION UTIL
	private boolean shouldInvalidateCache()
	{
		Instant oldestAllowedFetchInstant = Instant.now().minus(invalidationInterval);
		return (cachedItem != null) && lastFetchFromBackingSource.isBefore(oldestAllowedFetchInstant);
	}

}<commit_msg>Added never invalidate cache option<commit_after>package de.domisum.lib.auxilium.contracts.source.implementations.cache;

import de.domisum.lib.auxilium.contracts.source.SingleItemSource;
import de.domisum.lib.auxilium.util.java.annotations.API;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

import java.time.Duration;
import java.time.Instant;
import java.util.Optional;

@API
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class SingleItemSourceCache<T> implements SingleItemSource<T>
{

	// REFERENCES
	private final Duration invalidationInterval;
	private final SingleItemSource<T> backingSource;

	// CACHE
	private T cachedItem;
	private Instant lastFetchFromBackingSource;


	// INIT
	@API public static <T> SingleItemSourceCache<T> neverInvalidate(SingleItemSource<T> backingSource)
	{
		return new SingleItemSourceCache<>(null, backingSource);
	}

	@API
	public static <T> SingleItemSourceCache<T> invalidateEvery(Duration invalidationInterval, SingleItemSource<T> backingSource)
	{
		return new SingleItemSourceCache<>(invalidationInterval, backingSource);
	}


	// SOURCE
	@Override public synchronized Optional<T> fetch()
	{
		if(shouldInvalidateCache())
			invalidateCache();

		if(cachedItem == null)
			fetchFromBackingSource();

		return Optional.ofNullable(cachedItem);
	}


	// CACHE
	private void fetchFromBackingSource()
	{
		Optional<T> fromBackingSource = backingSource.fetch();
		if(!fromBackingSource.isPresent())
			return;

		cachedItem = fromBackingSource.get();
		lastFetchFromBackingSource = Instant.now();
	}

	private void invalidateCache()
	{
		cachedItem = null;
	}


	// CONDITION UTIL
	private boolean shouldInvalidateCache()
	{
		if(invalidationInterval == null)
			return false;

		Instant oldestAllowedFetchInstant = Instant.now().minus(invalidationInterval);
		return (cachedItem != null) && lastFetchFromBackingSource.isBefore(oldestAllowedFetchInstant);
	}

}
{'fraction_non_alphanumeric': 0.09892729439809297, 'fraction_numerical': 0.0, 'mean_word_length': 5.768145161290323, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '3962477', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1016, 'n_words': 197}