package com.eactive.eai.rms.common.cache.jpa;

import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eactive.eai.rms.common.cache.CacheCleaner;

@Service
public class CacheCleanerImpl implements CacheCleaner {

	private static final Logger logger = LoggerFactory.getLogger(CacheCleanerImpl.class);

	private NativeCacheCleaner nativeCacheService;

	private JpaCacheCleaner jpaCacheService;

	@Autowired
	public CacheCleanerImpl(NativeCacheCleaner nativeCacheService, JpaCacheCleaner jpaCacheService) {
		this.nativeCacheService = nativeCacheService;
		this.jpaCacheService = jpaCacheService;
	}

	@Override
	public Collection<String> findCacheNames() {

		Set<String> cacheNames = new TreeSet<>();
		cacheNames.addAll(nativeCacheService.findCacheNames());
		cacheNames.addAll(jpaCacheService.findCacheNames());

		if (logger.isDebugEnabled()) {
			cacheNames.forEach(s -> logger.debug("cache values : {}", s));
		}
		return cacheNames;
	}

	@Override
	public void clear() {
		nativeCacheService.clear();
		jpaCacheService.clear();
	}

	@Override
	public void clear(String cacheName) {
		nativeCacheService.clear(cacheName);
		jpaCacheService.clear();

	}

}
