Files
Umbraco-CMS/src/Umbraco.Web/PublishedCache/NuCache/PublishedShapshot.cs

113 lines
3.2 KiB
C#
Raw Normal View History

2016-05-27 14:26:28 +02:00
using System;
2016-06-09 18:45:16 +02:00
using Umbraco.Core;
2016-05-27 14:26:28 +02:00
using Umbraco.Core.Cache;
namespace Umbraco.Web.PublishedCache.NuCache
{
2017-10-31 12:48:24 +01:00
// implements published snapshot
internal class PublishedShapshot : IPublishedShapshot, IDisposable
2016-05-27 14:26:28 +02:00
{
2017-10-31 12:48:24 +01:00
private readonly PublishedSnapshotService _service;
2016-06-09 18:45:16 +02:00
private bool _defaultPreview;
2017-10-31 12:48:24 +01:00
private PublishedSnapshotElements _elements;
2016-05-27 14:26:28 +02:00
#region Constructors
2017-10-31 12:48:24 +01:00
public PublishedShapshot(PublishedSnapshotService service, bool defaultPreview)
2016-05-27 14:26:28 +02:00
{
_service = service;
_defaultPreview = defaultPreview;
2017-10-31 12:48:24 +01:00
SnapshotCache = new ObjectCacheRuntimeCacheProvider();
2016-05-27 14:26:28 +02:00
}
2017-10-31 12:48:24 +01:00
public class PublishedSnapshotElements : IDisposable
2016-05-27 14:26:28 +02:00
{
public ContentCache ContentCache;
public MediaCache MediaCache;
public MemberCache MemberCache;
public DomainCache DomainCache;
public ICacheProvider SnapshotCache;
2017-10-31 12:48:24 +01:00
public ICacheProvider ElementsCache;
2016-05-27 14:26:28 +02:00
public void Dispose()
{
ContentCache.Dispose();
MediaCache.Dispose();
}
}
2017-10-31 12:48:24 +01:00
private PublishedSnapshotElements Elements => _elements ?? (_elements = _service.GetElements(_defaultPreview));
2016-05-27 14:26:28 +02:00
public void Resync()
{
2017-10-31 12:48:24 +01:00
// no lock - published snapshots are single-thread
2016-05-30 19:54:36 +02:00
_elements?.Dispose();
2016-05-27 14:26:28 +02:00
_elements = null;
}
#endregion
#region Caches
2017-10-31 12:48:24 +01:00
public ICacheProvider SnapshotCache { get; }
2016-05-27 14:26:28 +02:00
2017-10-31 12:48:24 +01:00
public ICacheProvider ElementsCache => Elements.ElementsCache;
2016-05-27 14:26:28 +02:00
#endregion
2017-10-31 12:48:24 +01:00
#region IPublishedSnapshot
2016-05-27 14:26:28 +02:00
2016-05-30 19:54:36 +02:00
public IPublishedContentCache ContentCache => Elements.ContentCache;
2016-05-27 14:26:28 +02:00
2016-05-30 19:54:36 +02:00
public IPublishedMediaCache MediaCache => Elements.MediaCache;
2016-05-27 14:26:28 +02:00
2016-05-30 19:54:36 +02:00
public IPublishedMemberCache MemberCache => Elements.MemberCache;
2016-05-27 14:26:28 +02:00
2016-05-30 19:54:36 +02:00
public IDomainCache DomainCache => Elements.DomainCache;
2016-05-27 14:26:28 +02:00
2016-06-09 18:45:16 +02:00
public IDisposable ForcedPreview(bool preview, Action<bool> callback = null)
{
return new ForcedPreviewObject(this, preview, callback);
}
private class ForcedPreviewObject : DisposableObject
{
2017-10-31 12:48:24 +01:00
private readonly PublishedShapshot _publishedShapshot;
2016-06-09 18:45:16 +02:00
private readonly bool _origPreview;
private readonly Action<bool> _callback;
2017-10-31 12:48:24 +01:00
public ForcedPreviewObject(PublishedShapshot publishedShapshot, bool preview, Action<bool> callback)
2016-06-09 18:45:16 +02:00
{
2017-10-31 12:48:24 +01:00
_publishedShapshot = publishedShapshot;
2016-06-09 18:45:16 +02:00
_callback = callback;
// save and force
2017-10-31 12:48:24 +01:00
_origPreview = publishedShapshot._defaultPreview;
publishedShapshot._defaultPreview = preview;
2016-06-09 18:45:16 +02:00
}
protected override void DisposeResources()
{
// restore
2017-10-31 12:48:24 +01:00
_publishedShapshot._defaultPreview = _origPreview;
2016-06-09 18:45:16 +02:00
_callback?.Invoke(_origPreview);
}
}
2016-05-27 14:26:28 +02:00
#endregion
#region IDisposable
private bool _disposed;
public void Dispose()
{
if (_disposed) return;
_disposed = true;
2016-05-30 19:54:36 +02:00
_elements?.Dispose();
2016-05-27 14:26:28 +02:00
}
#endregion
}
}