<commit_before>package ch.elexis.core.data.cache;

import java.util.concurrent.TimeUnit;

import ch.elexis.data.PersistentObject;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheStats;

/**
 * A short-term guava cache automatically expiring objects after a certain
 * amount of time. Resembles the behaviour of the original {@link SoftCache};
 * however, not considering single object deviating cache times as propagated
 * by {@link PersistentObject#getCacheTime()}
 *
 * @param <K>
 */
public class GuavaCache<K> implements IPersistentObjectCache<K> {
	
	private Cache<K, Object> shortTermCache;
	
	public GuavaCache(long duration, TimeUnit unit){
		shortTermCache =
			CacheBuilder.newBuilder().recordStats().expireAfterWrite(duration, unit).build();
	}
	
	@Override
	public void put(K key, Object object, int timeToCacheInSeconds){
		shortTermCache.put(key, object);
	}
	
	@Override
	public Object get(K key){
		return shortTermCache.getIfPresent(key);
	}
	
	@Override
	public void remove(K key){
		shortTermCache.invalidate(key);
	}
	
	@Override
	public void clear(){
		shortTermCache.invalidateAll();
	}
	
	@Override
	public void stat(){
		CacheStats shortStats = shortTermCache.stats();
		System.out.println("--- GUAVA CACHE Statistics ---");
		System.out.println("Hits (count/rate): " + shortStats.hitCount() + "/"
			+ shortStats.hitRate());
		System.out.println("Misses (count/rate): " + shortStats.missCount() + "/"
			+ shortStats.missRate());
		System.out.println("Avg load penalty: " + shortStats.averageLoadPenalty());
		System.out.println("Load (count/successcount): " + shortStats.loadCount() + "/"
			+ shortStats.loadSuccessCount());
	}<commit_msg>[3489] Guava cache does not store null, so return (for key or value)<commit_after>package ch.elexis.core.data.cache;

import java.util.concurrent.TimeUnit;

import ch.elexis.data.PersistentObject;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheStats;

/**
 * A short-term guava cache automatically expiring objects after a certain amount of time. Resembles
 * the behaviour of the original {@link SoftCache}; however, not considering single object deviating
 * cache times as propagated by {@link PersistentObject#getCacheTime()}
 *
 * @param <K>
 */
public class GuavaCache<K> implements IPersistentObjectCache<K> {
	
	private Cache<K, Object> shortTermCache;
	
	public GuavaCache(long duration, TimeUnit unit){
		shortTermCache =
			CacheBuilder.newBuilder().recordStats().expireAfterWrite(duration, unit).build();
	}
	
	@Override
	public void put(K key, Object object, int timeToCacheInSeconds){
		if (key == null || object == null)
			return;
		shortTermCache.put(key, object);
	}
	
	@Override
	public Object get(K key){
		return shortTermCache.getIfPresent(key);
	}
	
	@Override
	public void remove(K key){
		shortTermCache.invalidate(key);
	}
	
	@Override
	public void clear(){
		shortTermCache.invalidateAll();
	}
	
	@Override
	public void stat(){
		CacheStats shortStats = shortTermCache.stats();
		System.out.println("--- GUAVA CACHE Statistics ---");
		System.out.println("Hits (count/rate): " + shortStats.hitCount() + "/"
			+ shortStats.hitRate());
		System.out.println("Misses (count/rate): " + shortStats.missCount() + "/"
			+ shortStats.missRate());
		System.out.println("Avg load penalty: " + shortStats.averageLoadPenalty());
		System.out.println("Load (count/successcount): " + shortStats.loadCount() + "/"
			+ shortStats.loadSuccessCount());
	}
{'fraction_non_alphanumeric': 0.13219675796534378, 'fraction_numerical': 0.0011179429849077697, 'mean_word_length': 4.80064829821718, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6987258', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1088, 'n_words': 260}