From 41cc62226052a66b05571fdd35e4335198ef44e0 Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 11 Jul 2017 19:13:45 +0200 Subject: [PATCH] Current.DistributedCache --- .../DistributedCache/DistributedCacheTests.cs | 17 +- .../Integration/ContentEventsTests.cs | 2 +- .../Scoping/ScopedRepositoryTests.cs | 6 +- src/Umbraco.Tests/Scoping/ScopedXmlTests.cs | 4 +- .../Cache/CacheRefresherComponent.cs | 194 +++++++++--------- src/Umbraco.Web/Cache/DistributedCache.cs | 27 --- src/Umbraco.Web/Composing/Current.cs | 4 + .../InstallSteps/SetUmbracoVersionStep.cs | 2 +- src/Umbraco.Web/WebRuntimeComponent.cs | 4 + .../WebServices/NuCacheStatusController.cs | 3 +- .../umbraco.presentation/template.cs | 2 +- 11 files changed, 126 insertions(+), 139 deletions(-) diff --git a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs index c286b7228c..8f5ecf7f58 100644 --- a/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs +++ b/src/Umbraco.Tests/Cache/DistributedCache/DistributedCacheTests.cs @@ -2,12 +2,9 @@ using System.Collections.Generic; using System.Linq; using LightInject; -using Moq; using NUnit.Framework; -using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Composing; -using Umbraco.Core.Logging; using Umbraco.Core.Sync; namespace Umbraco.Tests.Cache.DistributedCache @@ -18,6 +15,8 @@ namespace Umbraco.Tests.Cache.DistributedCache [TestFixture] public class DistributedCacheTests { + private Umbraco.Web.Cache.DistributedCache _distributedCache; + [SetUp] public void Setup() { @@ -29,6 +28,8 @@ namespace Umbraco.Tests.Cache.DistributedCache container.RegisterCollectionBuilder() .Add(); + + _distributedCache = new Umbraco.Web.Cache.DistributedCache(); } [TearDown] @@ -42,7 +43,7 @@ namespace Umbraco.Tests.Cache.DistributedCache { for (var i = 1; i < 11; i++) { - global::Umbraco.Web.Cache.DistributedCache.Instance.Refresh(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), i); + _distributedCache.Refresh(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), i); } Assert.AreEqual(10, ((TestServerMessenger)Current.ServerMessenger).IntIdsRefreshed.Count); } @@ -52,7 +53,7 @@ namespace Umbraco.Tests.Cache.DistributedCache { for (var i = 0; i < 10; i++) { - global::Umbraco.Web.Cache.DistributedCache.Instance.Refresh( + _distributedCache.Refresh( Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), x => x.Id, new TestObjectWithId{Id = i}); @@ -65,7 +66,7 @@ namespace Umbraco.Tests.Cache.DistributedCache { for (var i = 0; i < 11; i++) { - global::Umbraco.Web.Cache.DistributedCache.Instance.Refresh(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), Guid.NewGuid()); + _distributedCache.Refresh(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), Guid.NewGuid()); } Assert.AreEqual(11, ((TestServerMessenger)Current.ServerMessenger).GuidIdsRefreshed.Count); } @@ -75,7 +76,7 @@ namespace Umbraco.Tests.Cache.DistributedCache { for (var i = 1; i < 13; i++) { - global::Umbraco.Web.Cache.DistributedCache.Instance.Remove(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), i); + _distributedCache.Remove(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), i); } Assert.AreEqual(12, ((TestServerMessenger)Current.ServerMessenger).IntIdsRemoved.Count); } @@ -85,7 +86,7 @@ namespace Umbraco.Tests.Cache.DistributedCache { for (var i = 0; i < 13; i++) { - global::Umbraco.Web.Cache.DistributedCache.Instance.RefreshAll(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73")); + _distributedCache.RefreshAll(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73")); } Assert.AreEqual(13, ((TestServerMessenger)Current.ServerMessenger).CountOfFullRefreshes); } diff --git a/src/Umbraco.Tests/Integration/ContentEventsTests.cs b/src/Umbraco.Tests/Integration/ContentEventsTests.cs index 0ff3ab93f2..6e3565fce4 100644 --- a/src/Umbraco.Tests/Integration/ContentEventsTests.cs +++ b/src/Umbraco.Tests/Integration/ContentEventsTests.cs @@ -31,7 +31,7 @@ namespace Umbraco.Tests.Integration base.SetUp(); _h1 = new CacheRefresherComponent(true); - _h1.Initialize(); + _h1.Initialize(new DistributedCache()); _events = new List(); diff --git a/src/Umbraco.Tests/Scoping/ScopedRepositoryTests.cs b/src/Umbraco.Tests/Scoping/ScopedRepositoryTests.cs index 8bce82e67c..ddde5117d1 100644 --- a/src/Umbraco.Tests/Scoping/ScopedRepositoryTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopedRepositoryTests.cs @@ -78,7 +78,7 @@ namespace Umbraco.Tests.Scoping user = service.GetUserById(user.Id); _cacheRefresher = new CacheRefresherComponent(true); - _cacheRefresher.Initialize(); + _cacheRefresher.Initialize(new DistributedCache()); Assert.IsNull(scopeProvider.AmbientScope); using (var scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped)) @@ -159,7 +159,7 @@ namespace Umbraco.Tests.Scoping Assert.AreEqual("fr-FR", globalCached.IsoCode); _cacheRefresher = new CacheRefresherComponent(true); - _cacheRefresher.Initialize(); + _cacheRefresher.Initialize(new DistributedCache()); Assert.IsNull(scopeProvider.AmbientScope); using (var scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped)) @@ -251,7 +251,7 @@ namespace Umbraco.Tests.Scoping Assert.AreEqual("item-key", globalCached.ItemKey); _cacheRefresher = new CacheRefresherComponent(true); - _cacheRefresher.Initialize(); + _cacheRefresher.Initialize(new DistributedCache()); Assert.IsNull(scopeProvider.AmbientScope); using (var scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped)) diff --git a/src/Umbraco.Tests/Scoping/ScopedXmlTests.cs b/src/Umbraco.Tests/Scoping/ScopedXmlTests.cs index 91f071cc0a..1f7efab2ef 100644 --- a/src/Umbraco.Tests/Scoping/ScopedXmlTests.cs +++ b/src/Umbraco.Tests/Scoping/ScopedXmlTests.cs @@ -90,7 +90,7 @@ namespace Umbraco.Tests.Scoping // wire cache refresher _cacheRefresher = new CacheRefresherComponent(true); - _cacheRefresher.Initialize(); + _cacheRefresher.Initialize(new DistributedCache()); // check xml in context = "before" var xml = XmlInContext; @@ -209,7 +209,7 @@ namespace Umbraco.Tests.Scoping // wire cache refresher _cacheRefresher = new CacheRefresherComponent(true); - _cacheRefresher.Initialize(); + _cacheRefresher.Initialize(new DistributedCache()); // check xml in context = "before" var xml = XmlInContext; diff --git a/src/Umbraco.Web/Cache/CacheRefresherComponent.cs b/src/Umbraco.Web/Cache/CacheRefresherComponent.cs index 1e188ca4fd..bf81bcd4bb 100644 --- a/src/Umbraco.Web/Cache/CacheRefresherComponent.cs +++ b/src/Umbraco.Web/Cache/CacheRefresherComponent.cs @@ -32,6 +32,7 @@ namespace Umbraco.Web.Cache public class CacheRefresherComponent : UmbracoComponentBase, IUmbracoCoreComponent { private static readonly ConcurrentDictionary FoundHandlers = new ConcurrentDictionary(); + private DistributedCache _distributedCache; private List _unbinders; public CacheRefresherComponent() @@ -44,10 +45,12 @@ namespace Umbraco.Web.Cache _unbinders = new List(); } - public void Initialize() + public void Initialize(DistributedCache distributedCache) { Current.Logger.Info("Initializing Umbraco internal event handlers for cache refreshing."); + _distributedCache = distributedCache; + // bind to application tree events Bind(() => ApplicationTreeService.Deleted += ApplicationTreeService_Deleted, () => ApplicationTreeService.Deleted -= ApplicationTreeService_Deleted); @@ -196,7 +199,7 @@ namespace Umbraco.Web.Cache var underscore = new[] { '_' }; return typeof(CacheRefresherComponent) - .GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) + .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .Select(x => { if (x.Name.Contains("_") == false) return null; @@ -252,14 +255,14 @@ namespace Umbraco.Web.Cache #region PublicAccessService - private static void PublicAccessService_Saved(IPublicAccessService sender, SaveEventArgs e) + private void PublicAccessService_Saved(IPublicAccessService sender, SaveEventArgs e) { - DistributedCache.Instance.RefreshPublicAccess(); + _distributedCache.RefreshPublicAccess(); } - private static void PublicAccessService_Deleted(IPublicAccessService sender, DeleteEventArgs e) + private void PublicAccessService_Deleted(IPublicAccessService sender, DeleteEventArgs e) { - DistributedCache.Instance.RefreshPublicAccess(); + _distributedCache.RefreshPublicAccess(); } #endregion @@ -275,13 +278,13 @@ namespace Umbraco.Web.Cache /// When an entity is copied new permissions may be assigned to it based on it's parent, if that is the /// case then we need to clear all user permissions cache. /// - private static void ContentService_Copied(IContentService sender, CopyEventArgs e) + private void ContentService_Copied(IContentService sender, CopyEventArgs e) { //check if permissions have changed var permissionsChanged = ((Content)e.Copy).WasPropertyDirty("PermissionsChanged"); if (permissionsChanged) { - DistributedCache.Instance.RefreshAllUserPermissionsCache(); + _distributedCache.RefreshAllUserPermissionsCache(); } } @@ -297,7 +300,7 @@ namespace Umbraco.Web.Cache /// When an entity is created new permissions may be assigned to it based on it's parent, if that is the /// case then we need to clear all user permissions cache. /// - private static void ContentService_Saved(IContentService sender, SaveEventArgs e) + private void ContentService_Saved(IContentService sender, SaveEventArgs e) { var clearUserPermissions = false; foreach (var entity in e.SavedEntities) @@ -316,117 +319,118 @@ namespace Umbraco.Web.Cache if (clearUserPermissions) { - DistributedCache.Instance.RefreshAllUserPermissionsCache(); + _distributedCache.RefreshAllUserPermissionsCache(); } } - private static void ContentService_Changed(IContentService sender, TreeChange.EventArgs args) + private void ContentService_Changed(IContentService sender, TreeChange.EventArgs args) { - DistributedCache.Instance.RefreshContentCache(args.Changes.ToArray()); + _distributedCache.RefreshContentCache(args.Changes.ToArray()); } // fixme our weird events handling wants this for now - private static void ContentService_Deleted(IContentService sender, DeleteEventArgs e) { } - private static void ContentService_Moved(IContentService sender, MoveEventArgs e) { } - private static void ContentService_Trashed(IContentService sender, MoveEventArgs e) { } - private static void ContentService_EmptiedRecycleBin(IContentService sender, RecycleBinEventArgs e) { } - private static void ContentService_Published(IContentService sender, PublishEventArgs e) { } - private static void ContentService_UnPublished(IContentService sender, PublishEventArgs e) { } + private void ContentService_Deleted(IContentService sender, DeleteEventArgs e) { } + private void ContentService_Moved(IContentService sender, MoveEventArgs e) { } + private void ContentService_Trashed(IContentService sender, MoveEventArgs e) { } + private void ContentService_EmptiedRecycleBin(IContentService sender, RecycleBinEventArgs e) { } + private void ContentService_Published(IContentService sender, PublishEventArgs e) { } + private void ContentService_UnPublished(IContentService sender, PublishEventArgs e) { } #endregion #region ApplicationTreeService - private static void ApplicationTreeService_New(ApplicationTree sender, EventArgs e) + private void ApplicationTreeService_New(ApplicationTree sender, EventArgs e) { - DistributedCache.Instance.RefreshAllApplicationTreeCache(); + _distributedCache.RefreshAllApplicationTreeCache(); } - private static void ApplicationTreeService_Updated(ApplicationTree sender, EventArgs e) + private void ApplicationTreeService_Updated(ApplicationTree sender, EventArgs e) { - DistributedCache.Instance.RefreshAllApplicationTreeCache(); + _distributedCache.RefreshAllApplicationTreeCache(); } - private static void ApplicationTreeService_Deleted(ApplicationTree sender, EventArgs e) + private void ApplicationTreeService_Deleted(ApplicationTree sender, EventArgs e) { - DistributedCache.Instance.RefreshAllApplicationTreeCache(); + _distributedCache.RefreshAllApplicationTreeCache(); } + #endregion #region Application event handlers - private static void SectionService_New(Section sender, EventArgs e) + private void SectionService_New(Section sender, EventArgs e) { - DistributedCache.Instance.RefreshAllApplicationCache(); + _distributedCache.RefreshAllApplicationCache(); } - private static void SectionService_Deleted(Section sender, EventArgs e) + private void SectionService_Deleted(Section sender, EventArgs e) { - DistributedCache.Instance.RefreshAllApplicationCache(); + _distributedCache.RefreshAllApplicationCache(); } #endregion #region UserService / UserType - private static void UserService_DeletedUserType(IUserService sender, DeleteEventArgs e) + private void UserService_DeletedUserType(IUserService sender, DeleteEventArgs e) { foreach (var entity in e.DeletedEntities) - DistributedCache.Instance.RemoveUserTypeCache(entity.Id); + _distributedCache.RemoveUserTypeCache(entity.Id); } - private static void UserService_SavedUserType(IUserService sender, SaveEventArgs e) + private void UserService_SavedUserType(IUserService sender, SaveEventArgs e) { foreach (var entity in e.SavedEntities) - DistributedCache.Instance.RefreshUserTypeCache(entity.Id); + _distributedCache.RefreshUserTypeCache(entity.Id); } #endregion #region LocalizationService / Dictionary - private static void LocalizationService_SavedDictionaryItem(ILocalizationService sender, SaveEventArgs e) + private void LocalizationService_SavedDictionaryItem(ILocalizationService sender, SaveEventArgs e) { foreach (var entity in e.SavedEntities) - DistributedCache.Instance.RefreshDictionaryCache(entity.Id); + _distributedCache.RefreshDictionaryCache(entity.Id); } - private static void LocalizationService_DeletedDictionaryItem(ILocalizationService sender, DeleteEventArgs e) + private void LocalizationService_DeletedDictionaryItem(ILocalizationService sender, DeleteEventArgs e) { foreach (var entity in e.DeletedEntities) - DistributedCache.Instance.RemoveDictionaryCache(entity.Id); + _distributedCache.RemoveDictionaryCache(entity.Id); } #endregion #region DataTypeService - private static void DataTypeService_Saved(IDataTypeService sender, SaveEventArgs e) + private void DataTypeService_Saved(IDataTypeService sender, SaveEventArgs e) { foreach (var entity in e.SavedEntities) - DistributedCache.Instance.RefreshDataTypeCache(entity); + _distributedCache.RefreshDataTypeCache(entity); } - private static void DataTypeService_Deleted(IDataTypeService sender, DeleteEventArgs e) + private void DataTypeService_Deleted(IDataTypeService sender, DeleteEventArgs e) { foreach (var entity in e.DeletedEntities) - DistributedCache.Instance.RemoveDataTypeCache(entity); + _distributedCache.RemoveDataTypeCache(entity); } #endregion #region DomainService - private static void DomainService_Saved(IDomainService sender, SaveEventArgs e) + private void DomainService_Saved(IDomainService sender, SaveEventArgs e) { foreach (var entity in e.SavedEntities) - DistributedCache.Instance.RefreshDomainCache(entity); + _distributedCache.RefreshDomainCache(entity); } - private static void DomainService_Deleted(IDomainService sender, DeleteEventArgs e) + private void DomainService_Deleted(IDomainService sender, DeleteEventArgs e) { foreach (var entity in e.DeletedEntities) - DistributedCache.Instance.RemoveDomainCache(entity); + _distributedCache.RemoveDomainCache(entity); } #endregion @@ -438,10 +442,10 @@ namespace Umbraco.Web.Cache /// /// /// - private static void LocalizationService_DeletedLanguage(ILocalizationService sender, DeleteEventArgs e) + private void LocalizationService_DeletedLanguage(ILocalizationService sender, DeleteEventArgs e) { foreach (var entity in e.DeletedEntities) - DistributedCache.Instance.RemoveLanguageCache(entity); + _distributedCache.RemoveLanguageCache(entity); } /// @@ -449,60 +453,60 @@ namespace Umbraco.Web.Cache /// /// /// - private static void LocalizationService_SavedLanguage(ILocalizationService sender, SaveEventArgs e) + private void LocalizationService_SavedLanguage(ILocalizationService sender, SaveEventArgs e) { foreach (var entity in e.SavedEntities) - DistributedCache.Instance.RefreshLanguageCache(entity); + _distributedCache.RefreshLanguageCache(entity); } #endregion #region Content|Media|MemberTypeService - private static void ContentTypeService_Changed(IContentTypeService sender, ContentTypeChange.EventArgs args) + private void ContentTypeService_Changed(IContentTypeService sender, ContentTypeChange.EventArgs args) { - DistributedCache.Instance.RefreshContentTypeCache(args.Changes.ToArray()); + _distributedCache.RefreshContentTypeCache(args.Changes.ToArray()); } - private static void MediaTypeService_Changed(IMediaTypeService sender, ContentTypeChange.EventArgs args) + private void MediaTypeService_Changed(IMediaTypeService sender, ContentTypeChange.EventArgs args) { - DistributedCache.Instance.RefreshContentTypeCache(args.Changes.ToArray()); + _distributedCache.RefreshContentTypeCache(args.Changes.ToArray()); } - private static void MemberTypeService_Changed(IMemberTypeService sender, ContentTypeChange.EventArgs args) + private void MemberTypeService_Changed(IMemberTypeService sender, ContentTypeChange.EventArgs args) { - DistributedCache.Instance.RefreshContentTypeCache(args.Changes.ToArray()); + _distributedCache.RefreshContentTypeCache(args.Changes.ToArray()); } // fixme our weird events handling wants this for now - private static void ContentTypeService_Saved(IContentTypeService sender, SaveEventArgs args) { } - private static void MediaTypeService_Saved(IMediaTypeService sender, SaveEventArgs args) { } - private static void MemberTypeService_Saved(IMemberTypeService sender, SaveEventArgs args) { } - private static void ContentTypeService_Deleted(IContentTypeService sender, DeleteEventArgs args) { } - private static void MediaTypeService_Deleted(IMediaTypeService sender, DeleteEventArgs args) { } - private static void MemberTypeService_Deleted(IMemberTypeService sender, DeleteEventArgs args) { } + private void ContentTypeService_Saved(IContentTypeService sender, SaveEventArgs args) { } + private void MediaTypeService_Saved(IMediaTypeService sender, SaveEventArgs args) { } + private void MemberTypeService_Saved(IMemberTypeService sender, SaveEventArgs args) { } + private void ContentTypeService_Deleted(IContentTypeService sender, DeleteEventArgs args) { } + private void MediaTypeService_Deleted(IMediaTypeService sender, DeleteEventArgs args) { } + private void MemberTypeService_Deleted(IMemberTypeService sender, DeleteEventArgs args) { } #endregion #region UserService & PermissionRepository - private static void PermissionRepository_AssignedPermissions(PermissionRepository sender, SaveEventArgs e) + private void PermissionRepository_AssignedPermissions(PermissionRepository sender, SaveEventArgs e) { var userIds = e.SavedEntities.Select(x => x.UserId).Distinct(); foreach (var id in userIds) - DistributedCache.Instance.RefreshUserPermissionsCache(id); + _distributedCache.RefreshUserPermissionsCache(id); } - private static void UserService_SavedUser(IUserService sender, SaveEventArgs e) + private void UserService_SavedUser(IUserService sender, SaveEventArgs e) { foreach (var entity in e.SavedEntities) - DistributedCache.Instance.RefreshUserCache(entity.Id); + _distributedCache.RefreshUserCache(entity.Id); } - private static void UserService_DeletedUser(IUserService sender, DeleteEventArgs e) + private void UserService_DeletedUser(IUserService sender, DeleteEventArgs e) { foreach (var entity in e.DeletedEntities) - DistributedCache.Instance.RemoveUserCache(entity.Id); + _distributedCache.RemoveUserCache(entity.Id); } #endregion @@ -514,10 +518,10 @@ namespace Umbraco.Web.Cache /// /// /// - private static void FileService_DeletedTemplate(IFileService sender, DeleteEventArgs e) + private void FileService_DeletedTemplate(IFileService sender, DeleteEventArgs e) { foreach (var entity in e.DeletedEntities) - DistributedCache.Instance.RemoveTemplateCache(entity.Id); + _distributedCache.RemoveTemplateCache(entity.Id); } /// @@ -525,95 +529,95 @@ namespace Umbraco.Web.Cache /// /// /// - private static void FileService_SavedTemplate(IFileService sender, SaveEventArgs e) + private void FileService_SavedTemplate(IFileService sender, SaveEventArgs e) { foreach (var entity in e.SavedEntities) - DistributedCache.Instance.RefreshTemplateCache(entity.Id); + _distributedCache.RefreshTemplateCache(entity.Id); } // fixme our weird events handling wants this for now - private static void FileService_DeletedStylesheet(IFileService sender, DeleteEventArgs e) { } - private static void FileService_SavedStylesheet(IFileService sender, SaveEventArgs e) { } + private void FileService_DeletedStylesheet(IFileService sender, DeleteEventArgs e) { } + private void FileService_SavedStylesheet(IFileService sender, SaveEventArgs e) { } #endregion #region MacroService - private static void MacroService_Deleted(IMacroService sender, DeleteEventArgs e) + private void MacroService_Deleted(IMacroService sender, DeleteEventArgs e) { foreach (var entity in e.DeletedEntities) - DistributedCache.Instance.RemoveMacroCache(entity); + _distributedCache.RemoveMacroCache(entity); } - private static void MacroService_Saved(IMacroService sender, SaveEventArgs e) + private void MacroService_Saved(IMacroService sender, SaveEventArgs e) { foreach (var entity in e.SavedEntities) - DistributedCache.Instance.RefreshMacroCache(entity); + _distributedCache.RefreshMacroCache(entity); } #endregion #region MediaService - private static void MediaService_Changed(IMediaService sender, TreeChange.EventArgs args) + private void MediaService_Changed(IMediaService sender, TreeChange.EventArgs args) { - DistributedCache.Instance.RefreshMediaCache(args.Changes.ToArray()); + _distributedCache.RefreshMediaCache(args.Changes.ToArray()); } // fixme our weird events handling wants this for now - private static void MediaService_Saved(IMediaService sender, SaveEventArgs e) { } - private static void MediaService_Deleted(IMediaService sender, DeleteEventArgs e) { } - private static void MediaService_Moved(IMediaService sender, MoveEventArgs e) { } - private static void MediaService_Trashed(IMediaService sender, MoveEventArgs e) { } - private static void MediaService_EmptiedRecycleBin(IMediaService sender, RecycleBinEventArgs e) { } + private void MediaService_Saved(IMediaService sender, SaveEventArgs e) { } + private void MediaService_Deleted(IMediaService sender, DeleteEventArgs e) { } + private void MediaService_Moved(IMediaService sender, MoveEventArgs e) { } + private void MediaService_Trashed(IMediaService sender, MoveEventArgs e) { } + private void MediaService_EmptiedRecycleBin(IMediaService sender, RecycleBinEventArgs e) { } #endregion #region MemberService - private static void MemberService_Deleted(IMemberService sender, DeleteEventArgs e) + private void MemberService_Deleted(IMemberService sender, DeleteEventArgs e) { - DistributedCache.Instance.RemoveMemberCache(e.DeletedEntities.ToArray()); + _distributedCache.RemoveMemberCache(e.DeletedEntities.ToArray()); } - private static void MemberService_Saved(IMemberService sender, SaveEventArgs e) + private void MemberService_Saved(IMemberService sender, SaveEventArgs e) { - DistributedCache.Instance.RefreshMemberCache(e.SavedEntities.ToArray()); + _distributedCache.RefreshMemberCache(e.SavedEntities.ToArray()); } #endregion #region MemberGroupService - private static void MemberGroupService_Deleted(IMemberGroupService sender, DeleteEventArgs e) + private void MemberGroupService_Deleted(IMemberGroupService sender, DeleteEventArgs e) { foreach (var m in e.DeletedEntities.ToArray()) { - DistributedCache.Instance.RemoveMemberGroupCache(m.Id); + _distributedCache.RemoveMemberGroupCache(m.Id); } } - private static void MemberGroupService_Saved(IMemberGroupService sender, SaveEventArgs e) + private void MemberGroupService_Saved(IMemberGroupService sender, SaveEventArgs e) { foreach (var m in e.SavedEntities.ToArray()) { - DistributedCache.Instance.RemoveMemberGroupCache(m.Id); + _distributedCache.RemoveMemberGroupCache(m.Id); } } #endregion #region RelationType - private static void RelationService_SavedRelationType(IRelationService sender, SaveEventArgs args) + private void RelationService_SavedRelationType(IRelationService sender, SaveEventArgs args) { - var dc = DistributedCache.Instance; + var dc = _distributedCache; foreach (var e in args.SavedEntities) dc.RefreshRelationTypeCache(e.Id); } - private static void RelationService_DeletedRelationType(IRelationService sender, DeleteEventArgs args) + private void RelationService_DeletedRelationType(IRelationService sender, DeleteEventArgs args) { - var dc = DistributedCache.Instance; + var dc = _distributedCache; foreach (var e in args.DeletedEntities) dc.RemoveRelationTypeCache(e.Id); } diff --git a/src/Umbraco.Web/Cache/DistributedCache.cs b/src/Umbraco.Web/Cache/DistributedCache.cs index 17e43f86d5..23d1067282 100644 --- a/src/Umbraco.Web/Cache/DistributedCache.cs +++ b/src/Umbraco.Web/Cache/DistributedCache.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Linq; using Umbraco.Core; -using Umbraco.Core.Sync; using Umbraco.Core.Cache; using Umbraco.Web.Composing; @@ -22,32 +21,6 @@ namespace Umbraco.Web.Cache /// public sealed class DistributedCache { - #region Constructor & Singleton - - // note - should inject into the application instead of using a singleton - private static readonly DistributedCache InstanceObject = new DistributedCache(); - - /// - /// Initializes a new instance of the class. - /// - private DistributedCache() - { } - - /// - /// Gets the static unique instance of the class. - /// - /// The static unique instance of the class. - /// Exists so that extension methods can be added to the distributed cache. - public static DistributedCache Instance - { - get - { - return InstanceObject; - } - } - - #endregion - #region Core notification methods /// diff --git a/src/Umbraco.Web/Composing/Current.cs b/src/Umbraco.Web/Composing/Current.cs index 9d1e3e4aad..4a78930aad 100644 --- a/src/Umbraco.Web/Composing/Current.cs +++ b/src/Umbraco.Web/Composing/Current.cs @@ -19,6 +19,7 @@ using Umbraco.Core.Services; using Umbraco.Core.Strings; using Umbraco.Core.Sync; using Umbraco.Core._Legacy.PackageActions; +using Umbraco.Web.Cache; using Umbraco.Web.Editors; using Umbraco.Web.HealthCheck; using Umbraco.Web.Media; @@ -94,6 +95,9 @@ namespace Umbraco.Web.Composing public static UmbracoContext UmbracoContext => UmbracoContextAccessor.UmbracoContext; + public static DistributedCache DistributedCache + => Container.GetInstance(); + public static IFacade Facade => Container.GetInstance().Facade; diff --git a/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs b/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs index 12b88340dc..ec34fcce51 100644 --- a/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs +++ b/src/Umbraco.Web/Install/InstallSteps/SetUmbracoVersionStep.cs @@ -43,7 +43,7 @@ namespace Umbraco.Web.Install.InstallSteps // Some upgrade scripts "may modify the database (cmsContentXml...) tables directly" - not sure // that is still true but the idea is that after an upgrade we want to reset the local facade, on // all LB nodes of course, so we need to use the distributed cache, and refresh everything. - DistributedCache.Instance.RefreshAllFacade(); + Current.DistributedCache.RefreshAllFacade(); // Update configurationStatus GlobalSettings.ConfigurationStatus = UmbracoVersion.SemanticVersion.ToSemanticString(); diff --git a/src/Umbraco.Web/WebRuntimeComponent.cs b/src/Umbraco.Web/WebRuntimeComponent.cs index 2e66e15e8b..ad1106e4cd 100644 --- a/src/Umbraco.Web/WebRuntimeComponent.cs +++ b/src/Umbraco.Web/WebRuntimeComponent.cs @@ -24,6 +24,7 @@ using Umbraco.Core.Profiling; using Umbraco.Core.PropertyEditors; using Umbraco.Core.PropertyEditors.ValueConverters; using Umbraco.Core.Services; +using Umbraco.Web.Cache; using Umbraco.Web.DI; using Umbraco.Web.Dictionary; using Umbraco.Web.Editors; @@ -82,6 +83,9 @@ namespace Umbraco.Web // fixme - FUCK! how can this even work, it's not a singleton! composition.Container.RegisterSingleton(); + // register distributed cache + composition.Container.RegisterSingleton(f => new DistributedCache()); + // replace some services composition.Container.RegisterSingleton(); composition.Container.RegisterSingleton(); diff --git a/src/Umbraco.Web/WebServices/NuCacheStatusController.cs b/src/Umbraco.Web/WebServices/NuCacheStatusController.cs index abbabb286e..c4d626569e 100644 --- a/src/Umbraco.Web/WebServices/NuCacheStatusController.cs +++ b/src/Umbraco.Web/WebServices/NuCacheStatusController.cs @@ -1,6 +1,7 @@ using System; using System.Web.Http; using Umbraco.Web.Cache; +using Umbraco.Web.Composing; using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.NuCache; using Umbraco.Web.WebApi; @@ -58,7 +59,7 @@ namespace Umbraco.Web.WebServices [HttpPost] public void ReloadCache() { - DistributedCache.Instance.RefreshAllFacade(); + Current.DistributedCache.RefreshAllFacade(); } } } diff --git a/src/Umbraco.Web/umbraco.presentation/template.cs b/src/Umbraco.Web/umbraco.presentation/template.cs index 143ad82633..4fd52025c7 100644 --- a/src/Umbraco.Web/umbraco.presentation/template.cs +++ b/src/Umbraco.Web/umbraco.presentation/template.cs @@ -553,7 +553,7 @@ where nodeId = @templateID", [Obsolete("Use ApplicationContext.Current.ApplicationCache.ClearCacheForTemplate instead")] public static void ClearCachedTemplate(int templateID) { - DistributedCache.Instance.RefreshTemplateCache(templateID); + Current.DistributedCache.RefreshTemplateCache(templateID); } public template(String templateContent)