using System;
using Umbraco.Core.Cache;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Persistence.Repositories.Implement;
namespace Umbraco.Web.Cache
{
///
/// Handles User group cache invalidation/refreshing
///
///
/// This also needs to clear the user cache since IReadOnlyUserGroup's are attached to IUser objects
///
public sealed class UserGroupCacheRefresher : CacheRefresherBase
{
public UserGroupCacheRefresher(AppCaches appCaches)
: base(appCaches)
{ }
#region Define
protected override UserGroupCacheRefresher This => this;
public static readonly Guid UniqueId = Guid.Parse("45178038-B232-4FE8-AA1A-F2B949C44762");
public override Guid RefresherUniqueId => UniqueId;
public override string Name => "User Group Cache Refresher";
#endregion
#region Refresher
public override void RefreshAll()
{
ClearAllIsolatedCacheByEntityType();
var userGroupCache = AppCaches.IsolatedCaches.Get();
if (userGroupCache)
{
userGroupCache.Result.ClearByKey(CacheKeys.UserGroupGetByAliasCacheKeyPrefix);
}
//We'll need to clear all user cache too
ClearAllIsolatedCacheByEntityType();
base.RefreshAll();
}
public override void Refresh(int id)
{
Remove(id);
base.Refresh(id);
}
public override void Remove(int id)
{
var userGroupCache = AppCaches.IsolatedCaches.Get();
if (userGroupCache)
{
userGroupCache.Result.Clear(RepositoryCacheKeys.GetKey(id));
userGroupCache.Result.ClearByKey(CacheKeys.UserGroupGetByAliasCacheKeyPrefix);
}
//we don't know what user's belong to this group without doing a look up so we'll need to just clear them all
ClearAllIsolatedCacheByEntityType();
base.Remove(id);
}
#endregion
}
}