Files
Umbraco-CMS/src/Umbraco.PublishedCache.NuCache/PublishedSnapshot.cs

115 lines
3.3 KiB
C#
Raw Normal View History

using System;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.PublishedCache;
2016-05-27 14:26:28 +02:00
namespace Umbraco.Cms.Infrastructure.PublishedCache
2016-05-27 14:26:28 +02:00
{
2017-10-31 12:48:24 +01:00
// implements published snapshot
2018-04-27 11:38:50 +10:00
internal class PublishedSnapshot : IPublishedSnapshot, 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
2018-04-27 11:38:50 +10:00
public PublishedSnapshot(PublishedSnapshotService service, bool defaultPreview)
2016-05-27 14:26:28 +02:00
{
_service = service;
_defaultPreview = defaultPreview;
}
2017-10-31 12:48:24 +01:00
public class PublishedSnapshotElements : IDisposable
2016-05-27 14:26:28 +02:00
{
#pragma warning disable IDE1006 // Naming Styles
2016-05-27 14:26:28 +02:00
public ContentCache ContentCache;
public MediaCache MediaCache;
public MemberCache MemberCache;
public DomainCache DomainCache;
2019-01-17 11:01:23 +01:00
public IAppCache SnapshotCache;
public IAppCache ElementsCache;
#pragma warning restore IDE1006 // Naming Styles
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
2019-01-17 11:01:23 +01:00
public IAppCache SnapshotCache => Elements.SnapshotCache;
2016-05-27 14:26:28 +02:00
2019-01-17 11:01:23 +01:00
public IAppCache 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
2017-10-31 12:50:30 +01:00
public IPublishedContentCache Content => Elements.ContentCache;
2016-05-27 14:26:28 +02:00
2017-10-31 12:50:30 +01:00
public IPublishedMediaCache Media => Elements.MediaCache;
2016-05-27 14:26:28 +02:00
2017-10-31 12:50:30 +01:00
public IPublishedMemberCache Members => Elements.MemberCache;
2016-05-27 14:26:28 +02:00
2017-10-31 12:50:30 +01:00
public IDomainCache Domains => 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 : DisposableObjectSlim
2016-06-09 18:45:16 +02:00
{
2018-04-27 11:38:50 +10:00
private readonly PublishedSnapshot _publishedSnapshot;
2016-06-09 18:45:16 +02:00
private readonly bool _origPreview;
private readonly Action<bool> _callback;
2018-04-27 11:38:50 +10:00
public ForcedPreviewObject(PublishedSnapshot publishedShapshot, bool preview, Action<bool> callback)
2016-06-09 18:45:16 +02:00
{
2018-04-27 11:38:50 +10:00
_publishedSnapshot = 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
2018-04-27 11:38:50 +10:00
_publishedSnapshot._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
}
}