<commit_before>﻿using System.Globalization;
using System.Linq;
using TeaCommerce.Api.Notifications;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Cache;

namespace TeaCommerce.Umbraco.Application
{

    public class ApplicationStartup : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            NotificationCenter.Store.Created += (s, e) => { InvalidateStoresCache(); };
            NotificationCenter.Store.Deleted += (s, e) => { InvalidateStoresCache(); };

            NotificationCenter.Order.Created += (o, e) => { InvalidateOrdersCache(o.StoreId); };
            NotificationCenter.Order.Updated += (o, e) => { InvalidateOrdersCache(o.StoreId); };
            NotificationCenter.Order.Deleted += (o, e) => { InvalidateOrdersCache(o.StoreId); };

            ContentService.Copying += ContentService_Copying;
        }

        private void InvalidateStoresCache()
        {
            DistributedCache.Instance.RefreshAll(Constants.DistributedCache.StoreCacheRefresherGuid);
        }

        private void InvalidateOrdersCache(long storeId)
        {
            DistributedCache.Instance.Refresh(Constants.DistributedCache.OrderCacheRefresherGuid, (int)storeId);
        }

        private void ContentService_Copying(IContentService sender, CopyEventArgs<IContent> e)
        {
            Property masterRelationProperty = e.Copy.Properties.SingleOrDefault(p => p.Alias == Api.Constants.ProductPropertyAliases.MasterRelationPropertyAlias);

            if (masterRelationProperty == null || (masterRelationProperty.Value != null && !string.IsNullOrEmpty(masterRelationProperty.Value.ToString())))
            {
                return;
            }

            // Delete all property data
            foreach (Property property in e.Copy.Properties)
            {
                property.Value = null;
            }

            masterRelationProperty.Value = e.Original.Id.ToString(CultureInfo.InvariantCulture);
        }

    }
}<commit_msg>Setup event handlers to refresh distributed caches<commit_after>﻿using System;
using System.Globalization;
using System.Linq;
using TeaCommerce.Api.Notifications;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Cache;

namespace TeaCommerce.Umbraco.Application
{
    public class ApplicationStartup : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            SetupDistributedCacheInvalidation();
            SetupContentCopyingHandler();
        }

        private void SetupDistributedCacheInvalidation()
        {
            NotificationCenter.Campaign.Created += (e) => { InvalidateDistributedCache(Constants.DistributedCache.CampaignCacheRefresherGuid, e.StoreId); };
            NotificationCenter.Campaign.Updated += (e) => { InvalidateDistributedCache(Constants.DistributedCache.CampaignCacheRefresherGuid, e.StoreId); };
            NotificationCenter.Campaign.Deleted += (e) => { InvalidateDistributedCache(Constants.DistributedCache.CampaignCacheRefresherGuid, e.StoreId); };

            NotificationCenter.Country.Created += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.CountryCacheRefresherGuid, e.StoreId); };
            NotificationCenter.Country.Updated += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.CountryCacheRefresherGuid, e.StoreId); };
            NotificationCenter.Country.Deleted += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.CountryCacheRefresherGuid, e.StoreId); };

            NotificationCenter.CountryRegion.Created += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.CountryRegionCacheRefresherGuid, e.StoreId); };
            NotificationCenter.CountryRegion.Updated += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.CountryRegionCacheRefresherGuid, e.StoreId); };
            NotificationCenter.CountryRegion.Deleted += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.CountryRegionCacheRefresherGuid, e.StoreId); };

            NotificationCenter.Currency.Created += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.CurrencyCacheRefresherGuid, e.StoreId); };
            NotificationCenter.Currency.Updated += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.CurrencyCacheRefresherGuid, e.StoreId); };
            NotificationCenter.Currency.Deleted += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.CurrencyCacheRefresherGuid, e.StoreId); };

            NotificationCenter.EmailTemplate.Created += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.EmailTemplateCacheRefresherGuid, e.StoreId); };
            NotificationCenter.EmailTemplate.Updated += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.EmailTemplateCacheRefresherGuid, e.StoreId); };
            NotificationCenter.EmailTemplate.Deleted += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.EmailTemplateCacheRefresherGuid, e.StoreId); };

            NotificationCenter.Order.Created += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.OrderCacheRefresherGuid, e.StoreId); };
            NotificationCenter.Order.Updated += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.OrderCacheRefresherGuid, e.StoreId); };
            NotificationCenter.Order.Deleted += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.OrderCacheRefresherGuid, e.StoreId); };

            NotificationCenter.OrderStatus.Created += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.OrderStatusCacheRefresherGuid, e.StoreId); };
            NotificationCenter.OrderStatus.Updated += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.OrderStatusCacheRefresherGuid, e.StoreId); };
            NotificationCenter.OrderStatus.Deleted += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.OrderStatusCacheRefresherGuid, e.StoreId); };

            NotificationCenter.PaymentMethod.Created += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.PaymentMethodCacheRefresherGuid, e.StoreId); };
            NotificationCenter.PaymentMethod.Updated += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.PaymentMethodCacheRefresherGuid, e.StoreId); };
            NotificationCenter.PaymentMethod.Deleted += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.PaymentMethodCacheRefresherGuid, e.StoreId); };

            NotificationCenter.ShippingMethod.Created += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.ShippingMethodCacheRefresherGuid, e.StoreId); };
            NotificationCenter.ShippingMethod.Updated += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.ShippingMethodCacheRefresherGuid, e.StoreId); };
            NotificationCenter.ShippingMethod.Deleted += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.ShippingMethodCacheRefresherGuid, e.StoreId); };

            NotificationCenter.Store.Created += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.StoreCacheRefresherGuid); };
            NotificationCenter.Store.Updated += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.StoreCacheRefresherGuid); };
            NotificationCenter.Store.Deleted += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.StoreCacheRefresherGuid); };

            NotificationCenter.VatGroup.Created += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.VatGroupCacheRefresherGuid, e.StoreId); };
            NotificationCenter.VatGroup.Updated += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.VatGroupCacheRefresherGuid, e.StoreId); };
            NotificationCenter.VatGroup.Deleted += (e, a) => { InvalidateDistributedCache(Constants.DistributedCache.VatGroupCacheRefresherGuid, e.StoreId); };
        }

        private void InvalidateDistributedCache(Guid cacheKey, long? storeId = null)
        {
            if (storeId.HasValue)
            {
                DistributedCache.Instance.Refresh(cacheKey, (int)storeId);
            }
            else
            {
                DistributedCache.Instance.RefreshAll(cacheKey);
            }
        }

        private void SetupContentCopyingHandler()
        {
            ContentService.Copying += ContentService_Copying;
        }

        private void ContentService_Copying(IContentService sender, CopyEventArgs<IContent> e)
        {
            Property masterRelationProperty = e.Copy.Properties.SingleOrDefault(p => p.Alias == Api.Constants.ProductPropertyAliases.MasterRelationPropertyAlias);

            if (masterRelationProperty == null || (masterRelationProperty.Value != null && !string.IsNullOrEmpty(masterRelationProperty.Value.ToString())))
            {
                return;
            }

            // Delete all property data
            foreach (Property property in e.Copy.Properties)
            {
                property.Value = null;
            }

            masterRelationProperty.Value = e.Original.Id.ToString(CultureInfo.InvariantCulture);
        }

    }
}
{'fraction_non_alphanumeric': 0.10301560522986082, 'fraction_numerical': 0.0, 'mean_word_length': 3.7425, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '4668068', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2441, 'n_words': 342}