<commit_before>package no.kantega.publishing.cache;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import no.kantega.publishing.common.data.Association;
import no.kantega.publishing.event.ContentEvent;
import no.kantega.publishing.event.ContentEventListenerAdapter;

/**
 * ContentEventListener that flushes caches on events.
 */
public class CacheExpirator extends ContentEventListenerAdapter {

    private CacheManager cacheManager;

    @Override
    public void associationUpdated(ContentEvent event) {
        // Pages could have been copied, safest just to flush everything.
        removeAllContent();
        removeAllContentListsAndSiteMaps();
    }

    @Override
    public void associationCopied(ContentEvent event) {
        // Pages could have been copied, safest just to flush everything.
        removeAllContent();
        removeAllContentListsAndSiteMaps();
    }

    @Override
    public void associationAdded(ContentEvent event) {
        // Pages could have been copied, safest just to flush everything.
        removeAllContent();
        removeAllContentListsAndSiteMaps();
    }

    @Override
    public void setAssociationsPriority(ContentEvent contentEvent) {
        // Pages reordered, all lists and sitemaps should be flushed
        removeAllContentListsAndSiteMaps();
    }

    @Override
    public void contentStatusChanged(ContentEvent event) {
        removeContentFromCache(event);
    }

    @Override
    public void contentActivated(ContentEvent event) {
        removeContentFromCache(event);
    }

    @Override
    public void contentDeleted(ContentEvent event) {
        removeContentFromCache(event);
    }

    @Override
    public void contentSaved(ContentEvent event) {
        removeContentFromCache(event);
    }

    @Override
    public void contentExpired(ContentEvent event) {
        removeContentFromCache(event);
    }

    @Override
    public void newContentPublished(ContentEvent event) {
        removeContentFromCache(event);
    }

    private Cache getContentCache() {
        return cacheManager.getCache(CacheManagerFactory.CacheNames.ContentCache.name());
    }

    private void removeContentFromCache(ContentEvent event) {
        // Remove the content that was changed
        final Cache contentCache = getContentCache();

        for(Association a : event.getContent().getAssociations())  {
            Object key = a.getAssociationId();
            contentCache.remove(key);
        }
        removeAllContentListsAndSiteMaps();
    }

    private void removeAllContentListsAndSiteMaps() {
        // Flush the content list cache
        cacheManager.getCache(CacheManagerFactory.CacheNames.ContentListCache.name()).removeAll();
        // Flush the site map cache
        cacheManager.getCache(CacheManagerFactory.CacheNames.SiteMapCache.name()).removeAll();
    }

    private void removeAllContent() {
        // Remove the content that was changed
        final Cache contentCache = getContentCache();
        contentCache.removeAll();
    }

    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }
}<commit_msg>AP-1556 - Make XmlCache bean with interface.<commit_after>package no.kantega.publishing.cache;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import no.kantega.publishing.common.data.Association;
import no.kantega.publishing.event.ContentEvent;
import no.kantega.publishing.event.ContentEventListenerAdapter;

/**
 * ContentEventListener that flushes caches on events.
 */
public class CacheExpirator extends ContentEventListenerAdapter {

    private CacheManager cacheManager;

    @Override
    public void associationUpdated(ContentEvent event) {
        // Pages could have been copied, safest just to flush everything.
        removeAllContent();
        removeAllContentListsAndSiteMaps();
    }

    @Override
    public void associationCopied(ContentEvent event) {
        // Pages could have been copied, safest just to flush everything.
        removeAllContent();
        removeAllContentListsAndSiteMaps();
    }

    @Override
    public void associationAdded(ContentEvent event) {
        // Pages could have been copied, safest just to flush everything.
        removeAllContent();
        removeAllContentListsAndSiteMaps();
    }

    @Override
    public void setAssociationsPriority(ContentEvent contentEvent) {
        // Pages reordered, all lists and sitemaps should be flushed
        removeAllContentListsAndSiteMaps();
    }

    @Override
    public void contentStatusChanged(ContentEvent event) {
        removeContentFromCache(event);
    }

    @Override
    public void contentActivated(ContentEvent event) {
        removeContentFromCache(event);
    }

    @Override
    public void contentDeleted(ContentEvent event) {
        removeContentFromCache(event);
    }

    @Override
    public void contentSaved(ContentEvent event) {
        removeContentFromCache(event);
    }

    @Override
    public void contentExpired(ContentEvent event) {
        removeContentFromCache(event);
    }

    @Override
    public void newContentPublished(ContentEvent event) {
        removeContentFromCache(event);
    }

    private Ehcache getContentCache() {
        return cacheManager.getEhcache(CacheManagerFactory.CacheNames.ContentCache.name());
    }

    private void removeContentFromCache(ContentEvent event) {
        // Remove the content that was changed
        final Ehcache contentCache = getContentCache();

        for(Association a : event.getContent().getAssociations())  {
            Object key = a.getAssociationId();
            contentCache.remove(key);
        }
        removeAllContentListsAndSiteMaps();
    }

    private void removeAllContentListsAndSiteMaps() {
        // Flush the content list cache
        cacheManager.getEhcache(CacheManagerFactory.CacheNames.ContentListCache.name()).removeAll();
        // Flush the site map cache
        cacheManager.getEhcache(CacheManagerFactory.CacheNames.SiteMapCache.name()).removeAll();
    }

    private void removeAllContent() {
        // Remove the content that was changed
        final Ehcache contentCache = getContentCache();
        contentCache.removeAll();
    }

    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }
}
{'fraction_non_alphanumeric': 0.07704377844029499, 'fraction_numerical': 0.0006276478895339715, 'mean_word_length': 3.160574412532637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1219800', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1586, 'n_words': 385}