Files
Umbraco-CMS/src/Umbraco.Web/Cache/MediaCacheRefresher.cs

73 lines
2.0 KiB
C#
Raw Normal View History

using System;
using Umbraco.Core;
2013-03-12 03:00:42 +04:00
using Umbraco.Core.Cache;
using Umbraco.Core.Models;
using umbraco.interfaces;
namespace Umbraco.Web.Cache
{
/// <summary>
/// A cache refresher to ensure media cache is updated when members change
/// </summary>
/// <remarks>
/// This is not intended to be used directly in your code and it should be sealed but due to legacy code we cannot seal it.
/// </remarks>
public class MediaCacheRefresher : ICacheRefresher<IMedia>
{
public Guid UniqueIdentifier
{
get { return new Guid(DistributedCache.MediaCacheRefresherId); }
}
public string Name
{
get { return "Clears Media Cache from umbraco.library"; }
}
public void RefreshAll()
{
}
public void Refresh(int id)
{
ClearCache(ApplicationContext.Current.Services.MediaService.GetById(id));
}
public void Remove(int id)
{
ClearCache(ApplicationContext.Current.Services.MediaService.GetById(id));
}
public void Refresh(Guid id)
{
}
public void Refresh(IMedia instance)
{
ClearCache(instance);
}
public void Remove(IMedia instance)
{
ClearCache(instance);
}
private static void ClearCache(IMedia media)
{
if (media == null) return;
foreach (var idPart in media.Path.Split(','))
{
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(
string.Format("{0}_{1}_True", CacheKeys.MediaCacheKey, idPart));
// Also clear calls that only query this specific item!
if (idPart == media.Id.ToString())
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(
string.Format("{0}_{1}", CacheKeys.MediaCacheKey, media.Id));
}
}
}
}