Merge remote-tracking branch 'origin/temp8' into temp8-implement-GetByContentType-nucache

This commit is contained in:
Shannon
2019-02-14 09:52:09 +11:00
34 changed files with 351 additions and 259 deletions

View File

@@ -48,6 +48,21 @@ namespace Umbraco.Web.PublishedCache
#endregion
#region Rebuild
/// <summary>
/// Rebuilds internal caches (but does not reload).
/// </summary>
/// <remarks>
/// <para>Forces the snapshot service to rebuild its internal caches. For instance, some caches
/// may rely on a database table to store pre-serialized version of documents.</para>
/// <para>This does *not* reload the caches. Caches need to be reloaded, for instance via
/// <see cref="DistributedCache" /> RefreshAllPublishedSnapshot method.</para>
/// </remarks>
void Rebuild();
#endregion
#region Preview
/* Later on we can imagine that EnterPreview would handle a "level" that would be either

View File

@@ -27,6 +27,7 @@ using Umbraco.Web.Cache;
using Umbraco.Web.Install;
using Umbraco.Web.PublishedCache.NuCache.DataSource;
using Umbraco.Web.Routing;
using File = System.IO.File;
namespace Umbraco.Web.PublishedCache.NuCache
{
@@ -142,31 +143,7 @@ namespace Umbraco.Web.PublishedCache.NuCache
if (registered)
{
string path;
var tempLocation = globalSettings.LocalTempStorageLocation;
switch (tempLocation)
{
case LocalTempStorage.AspNetTemp:
path = Path.Combine(HttpRuntime.CodegenDir, "UmbracoData", "NuCache");
break;
case LocalTempStorage.EnvironmentTemp:
// TODO: why has this to be repeated everywhere?!
// include the appdomain hash is just a safety check, for example if a website is moved from worker A to worker B and then back
// to worker A again, in theory the %temp% folder should already be empty but we really want to make sure that its not
// utilizing an old path - assuming we cannot have SHA1 collisions on AppDomainAppId
var appDomainHash = HttpRuntime.AppDomainAppId.GenerateHash();
path = Path.Combine(Environment.ExpandEnvironmentVariables("%temp%"), "UmbracoData", appDomainHash, "NuCache");
break;
//case LocalTempStorage.Default:
//case LocalTempStorage.Unknown:
default:
path = IOHelper.MapPath("~/App_Data/TEMP/NuCache");
break;
}
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var path = GetLocalFilesPath();
var localContentDbPath = Path.Combine(path, "NuCache.Content.db");
var localMediaDbPath = Path.Combine(path, "NuCache.Media.db");
_localDbExists = System.IO.File.Exists(localContentDbPath) && System.IO.File.Exists(localMediaDbPath);
@@ -295,13 +272,69 @@ namespace Umbraco.Web.PublishedCache.NuCache
#endregion
#region Local files
private string GetLocalFilesPath()
{
string path;
var tempLocation = _globalSettings.LocalTempStorageLocation;
switch (tempLocation)
{
case LocalTempStorage.AspNetTemp:
path = Path.Combine(HttpRuntime.CodegenDir, "UmbracoData", "NuCache");
break;
case LocalTempStorage.EnvironmentTemp:
// TODO: why has this to be repeated everywhere?!
// include the appdomain hash is just a safety check, for example if a website is moved from worker A to worker B and then back
// to worker A again, in theory the %temp% folder should already be empty but we really want to make sure that its not
// utilizing an old path - assuming we cannot have SHA1 collisions on AppDomainAppId
var appDomainHash = HttpRuntime.AppDomainAppId.GenerateHash();
path = Path.Combine(Environment.ExpandEnvironmentVariables("%temp%"), "UmbracoData", appDomainHash, "NuCache");
break;
//case LocalTempStorage.Default:
//case LocalTempStorage.Unknown:
default:
path = IOHelper.MapPath("~/App_Data/TEMP/NuCache");
break;
}
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
return path;
}
private void DeleteLocalFilesForContent()
{
if (_isReady && _localContentDb != null)
throw new InvalidOperationException("Cannot delete local files while the cache uses them.");
var path = GetLocalFilesPath();
var localContentDbPath = Path.Combine(path, "NuCache.Content.db");
if (File.Exists(localContentDbPath))
File.Delete(localContentDbPath);
}
private void DeleteLocalFilesForMedia()
{
if (_isReady && _localMediaDb != null)
throw new InvalidOperationException("Cannot delete local files while the cache uses them.");
var path = GetLocalFilesPath();
var localMediaDbPath = Path.Combine(path, "NuCache.Media.db");
if (File.Exists(localMediaDbPath))
File.Delete(localMediaDbPath);
}
#endregion
#region Environment
public override bool EnsureEnvironment(out IEnumerable<string> errors)
{
// must have app_data and be able to write files into it
var ok = FilePermissionHelper.TryCreateDirectory(SystemDirectories.Data);
errors = ok ? Enumerable.Empty<string>() : new[] { "NuCache local DB files." };
var ok = FilePermissionHelper.TryCreateDirectory(GetLocalFilesPath());
errors = ok ? Enumerable.Empty<string>() : new[] { "NuCache local files." };
return ok;
}
@@ -560,10 +593,11 @@ namespace Umbraco.Web.PublishedCache.NuCache
public override void Notify(ContentCacheRefresher.JsonPayload[] payloads, out bool draftChanged, out bool publishedChanged)
{
// no cache, nothing we can do
// no cache, trash everything
if (_isReady == false)
{
draftChanged = publishedChanged = false;
DeleteLocalFilesForContent();
draftChanged = publishedChanged = true;
return;
}
@@ -655,10 +689,11 @@ namespace Umbraco.Web.PublishedCache.NuCache
public override void Notify(MediaCacheRefresher.JsonPayload[] payloads, out bool anythingChanged)
{
// no cache, nothing we can do
// no cache, trash everything
if (_isReady == false)
{
anythingChanged = false;
DeleteLocalFilesForMedia();
anythingChanged = true;
return;
}
@@ -1277,6 +1312,21 @@ namespace Umbraco.Web.PublishedCache.NuCache
#region Rebuild Database PreCache
public override void Rebuild()
{
_logger.Debug<PublishedSnapshotService>("Rebuilding...");
using (var scope = _scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped))
{
scope.ReadLock(Constants.Locks.ContentTree);
scope.ReadLock(Constants.Locks.MediaTree);
scope.ReadLock(Constants.Locks.MemberTree);
RebuildContentDbCacheLocked(scope, 5000, null);
RebuildMediaDbCacheLocked(scope, 5000, null);
RebuildMemberDbCacheLocked(scope, 5000, null);
scope.Complete();
}
}
public void RebuildContentDbCache(int groupSize = 5000, IEnumerable<int> contentTypeIds = null)
{
using (var scope = _scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped))

View File

@@ -33,6 +33,9 @@ namespace Umbraco.Web.PublishedCache
public abstract void Notify(DataTypeCacheRefresher.JsonPayload[] payloads);
public abstract void Notify(DomainCacheRefresher.JsonPayload[] payloads);
public virtual void Rebuild()
{ }
public virtual void Dispose()
{ }
}