mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Optimizing IAliasHolder updates
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Xml.Linq;
|
||||
using Orchard.Alias.Records;
|
||||
using Orchard.Data;
|
||||
@@ -13,8 +14,9 @@ namespace Orchard.Alias.Implementation.Storage {
|
||||
void Remove(string path);
|
||||
void Remove(string path, string aliasSource);
|
||||
void RemoveBySource(string aliasSource);
|
||||
IEnumerable<Tuple<string, string, IDictionary<string, string>, string>> List();
|
||||
IEnumerable<Tuple<string, string, IDictionary<string, string>, string>> List(string sourceStartsWith);
|
||||
IEnumerable<Tuple<string, string, IDictionary<string, string>, string, int>> List(Expression<Func<AliasRecord, bool>> predicate);
|
||||
IEnumerable<Tuple<string, string, IDictionary<string, string>, string, int>> List();
|
||||
IEnumerable<Tuple<string, string, IDictionary<string, string>, string, int>> List(string sourceStartsWith);
|
||||
}
|
||||
|
||||
public class AliasStorage : IAliasStorage {
|
||||
@@ -124,15 +126,25 @@ namespace Orchard.Alias.Implementation.Storage {
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Tuple<string, string, IDictionary<string, string>, string>> List() {
|
||||
return _aliasRepository.Table.OrderBy(a => a.Id).Select(ToDictionary).ToList();
|
||||
public IEnumerable<Tuple<string, string, IDictionary<string, string>, string, int>> List() {
|
||||
return List((Expression<Func<AliasRecord, bool>>) null);
|
||||
}
|
||||
|
||||
public IEnumerable<Tuple<string, string, IDictionary<string, string>, string>> List(string sourceStartsWith) {
|
||||
return _aliasRepository.Table.Where(a => a.Source.StartsWith(sourceStartsWith)).OrderBy(a => a.Id).Select(ToDictionary).ToList();
|
||||
public IEnumerable<Tuple<string, string, IDictionary<string, string>, string, int>> List(Expression<Func<AliasRecord, bool>> predicate) {
|
||||
var table = _aliasRepository.Table;
|
||||
|
||||
if (predicate != null) {
|
||||
table = table.Where(predicate);
|
||||
}
|
||||
|
||||
return table.OrderBy(a => a.Id).Select(ToDictionary).ToList();
|
||||
}
|
||||
|
||||
private static Tuple<string, string, IDictionary<string, string>, string> ToDictionary(AliasRecord aliasRecord) {
|
||||
public IEnumerable<Tuple<string, string, IDictionary<string, string>, string, int>> List(string sourceStartsWith) {
|
||||
return List(a => a.Source.StartsWith(sourceStartsWith));
|
||||
}
|
||||
|
||||
private static Tuple<string, string, IDictionary<string, string>, string, int> ToDictionary(AliasRecord aliasRecord) {
|
||||
IDictionary<string, string> routeValues = new Dictionary<string, string>();
|
||||
if (aliasRecord.Action.Area != null) {
|
||||
routeValues.Add("area", aliasRecord.Action.Area);
|
||||
@@ -148,7 +160,7 @@ namespace Orchard.Alias.Implementation.Storage {
|
||||
routeValues.Add(attr.Name.LocalName, attr.Value);
|
||||
}
|
||||
}
|
||||
return Tuple.Create(aliasRecord.Path, aliasRecord.Action.Area, routeValues, aliasRecord.Source);
|
||||
return Tuple.Create(aliasRecord.Path, aliasRecord.Action.Area, routeValues, aliasRecord.Source, aliasRecord.Id);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
namespace Orchard.Alias.Implementation.Updater {
|
||||
public class AliasUpdateCursor : IAliasUpdateCursor {
|
||||
public int Cursor { get; set; }
|
||||
}
|
||||
}
|
@@ -1,10 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using Orchard.Alias.Implementation.Holder;
|
||||
using Orchard.Alias.Implementation.Storage;
|
||||
using Orchard.Data;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Tasks;
|
||||
using Orchard.Logging;
|
||||
@@ -13,12 +10,14 @@ namespace Orchard.Alias.Implementation.Updater {
|
||||
public class AliasHolderUpdater : IOrchardShellEvents, IBackgroundTask {
|
||||
private readonly IAliasHolder _aliasHolder;
|
||||
private readonly IAliasStorage _storage;
|
||||
private readonly IAliasUpdateCursor _cursor;
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
public AliasHolderUpdater(IAliasHolder aliasHolder, IAliasStorage storage) {
|
||||
public AliasHolderUpdater(IAliasHolder aliasHolder, IAliasStorage storage, IAliasUpdateCursor cursor) {
|
||||
_aliasHolder = aliasHolder;
|
||||
_storage = storage;
|
||||
_cursor = cursor;
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
|
||||
@@ -31,7 +30,14 @@ namespace Orchard.Alias.Implementation.Updater {
|
||||
|
||||
private void Refresh() {
|
||||
try {
|
||||
var aliases = _storage.List();
|
||||
// only retreive aliases which have not been processed yet
|
||||
var aliases = _storage.List(x => x.Id > _cursor.Cursor).ToArray();
|
||||
|
||||
// update the last processed id
|
||||
if (aliases.Any()) {
|
||||
_cursor.Cursor = aliases.Last().Item5;
|
||||
}
|
||||
|
||||
_aliasHolder.SetAliases(aliases.Select(alias => new AliasInfo { Path = alias.Item1, Area = alias.Item2, RouteValues = alias.Item3 }));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
|
@@ -0,0 +1,5 @@
|
||||
namespace Orchard.Alias.Implementation.Updater {
|
||||
public interface IAliasUpdateCursor : ISingletonDependency {
|
||||
int Cursor { get; set; }
|
||||
}
|
||||
}
|
@@ -21,6 +21,10 @@
|
||||
</UpgradeBackupLocation>
|
||||
<TargetFrameworkProfile />
|
||||
<UseIISExpress>false</UseIISExpress>
|
||||
<IISExpressSSLPort />
|
||||
<IISExpressAnonymousAuthentication />
|
||||
<IISExpressWindowsAuthentication />
|
||||
<IISExpressUseClassicPipelineMode />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@@ -67,6 +71,8 @@
|
||||
<Content Include="Views\Admin\Index.cshtml" />
|
||||
<Content Include="Web.config" />
|
||||
<Content Include="Views\Web.config" />
|
||||
<Compile Include="Implementation\Updater\AliasUpdateCursor.cs" />
|
||||
<Compile Include="Implementation\Updater\IAliasUpdateCursor.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Content Include="Module.txt" />
|
||||
</ItemGroup>
|
||||
|
Reference in New Issue
Block a user