<commit_before>﻿using Umbraco.Core;
using Umbraco.Core.Services;
using umbraco;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.member;
using System.Linq;

namespace Umbraco.Web.Cache
{
    /// <summary>
    /// Special class made to listen to save events on objects where umbraco.library caches some of their objects
    /// </summary>
    public class LibraryCacheRefresher : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            if (UmbracoSettings.UmbracoLibraryCacheDuration <= 0) return;


            Member.AfterSave += MemberAfterSave;
            Member.BeforeDelete += MemberBeforeDelete;

            MediaService.Saved += MediaServiceSaved;
            MediaService.Deleting += MediaServiceDeleting;
            MediaService.Moved += MediaServiceMoved;
            MediaService.Trashed += MediaService_Trashed;
        }

        static void MediaService_Trashed(IMediaService sender, Core.Events.MoveEventArgs<Core.Models.IMedia> e)
        {
            ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(e.Entity.Id);
        }

        static void MediaServiceMoved(IMediaService sender, Core.Events.MoveEventArgs<Core.Models.IMedia> e)
        {
            ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(e.Entity.Id);            
        }

        static void MediaServiceDeleting(IMediaService sender, Core.Events.DeleteEventArgs<Core.Models.IMedia> e)
        {
            foreach (var item in e.DeletedEntities)
            {
                ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(item.Id);
            }
        }

        //NOTE: this should not need to be done, the SavedCollection event shouldn't even exist!! :(
        static void MediaServiceSavedCollection(IMediaService sender, Core.Events.SaveEventArgs<System.Collections.Generic.IEnumerable<Core.Models.IMedia>> e)
        {            
            foreach (var item in e.SavedEntities.SelectMany(x => x))
            {
                ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(item.Id);
            }
        }

        static void MediaServiceSaved(IMediaService sender, Core.Events.SaveEventArgs<Core.Models.IMedia> e)
        {
            foreach (var item in e.SavedEntities)
            {
                ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(item.Id);
            }
        }

        static void MemberBeforeDelete(Member sender, DeleteEventArgs e)
        {
            ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMember(sender.Id);
        }

        static void MemberAfterSave(Member sender, SaveEventArgs e)
        {
            ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMember(sender.Id);
        }
    }
}<commit_msg>Adds patch fixes for #U4-1653<commit_after>﻿using Umbraco.Core;
using Umbraco.Core.Services;
using umbraco;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.member;
using System.Linq;

namespace Umbraco.Web.Cache
{
    /// <summary>
    /// Special class made to listen to save events on objects where umbraco.library caches some of their objects
    /// </summary>
    public class LibraryCacheRefresher : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            if (UmbracoSettings.UmbracoLibraryCacheDuration <= 0) return;


            Member.AfterSave += MemberAfterSave;
            Member.BeforeDelete += MemberBeforeDelete;

            MediaService.Saved += MediaServiceSaved;
            //We need to perform all of the 'before' events here because we need a reference to the
            //media item's Path before it is moved/deleting/trashed
            //see: http://issues.umbraco.org/issue/U4-1653
            MediaService.Deleting += MediaServiceDeleting;
            MediaService.Moving += MediaServiceMoving;
            MediaService.Trashing += MediaServiceTrashing;
        }

        static void MediaServiceTrashing(IMediaService sender, Core.Events.MoveEventArgs<Core.Models.IMedia> e)
        {
            ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(e.Entity.Id);
        }

        static void MediaServiceMoving(IMediaService sender, Core.Events.MoveEventArgs<Core.Models.IMedia> e)
        {
            ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(e.Entity.Id);            
        }

        static void MediaServiceDeleting(IMediaService sender, Core.Events.DeleteEventArgs<Core.Models.IMedia> e)
        {
            foreach (var item in e.DeletedEntities)
            {
                ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(item.Id);
            }
        }

        static void MediaServiceSaved(IMediaService sender, Core.Events.SaveEventArgs<Core.Models.IMedia> e)
        {
            foreach (var item in e.SavedEntities)
            {
                ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(item.Id);
            }
        }

        static void MemberBeforeDelete(Member sender, DeleteEventArgs e)
        {
            ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMember(sender.Id);
        }

        static void MemberAfterSave(Member sender, SaveEventArgs e)
        {
            ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMember(sender.Id);
        }
    }
}
{'fraction_non_alphanumeric': 0.07467588591184096, 'fraction_numerical': 0.002074330164217805, 'mean_word_length': 2.399529964747356, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 18, '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': '5555328', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1426, 'n_words': 281}