- Implementation of a topology descriptor cache for tenant topologies based on host level xml file (tests were checked in previously).

--HG--
branch : dev
This commit is contained in:
Suha Can
2010-04-14 18:14:56 -07:00
parent 8d59e1d6ae
commit 0cf2b1c0e6
2 changed files with 62 additions and 6 deletions

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using Orchard.Environment.Configuration;
using Orchard.Environment.Topology.Models;
using Orchard.Localization;
@@ -7,7 +9,6 @@ using Orchard.Logging;
namespace Orchard.Environment.Topology {
public class DefaultTopologyDescriptorCache : ITopologyDescriptorCache {
readonly IDictionary<string, ShellTopologyDescriptor> _cache= new Dictionary<string, ShellTopologyDescriptor>();
private readonly IAppDataFolder _appDataFolder;
private const string TopologyCacheFileName = "cache.dat";
@@ -25,20 +26,72 @@ namespace Orchard.Environment.Topology {
public ShellTopologyDescriptor Fetch(string name) {
VerifyCacheFile();
ShellTopologyDescriptor value;
return _cache.TryGetValue(name, out value) ? value : null;
var text = _appDataFolder.ReadFile(TopologyCacheFileName);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(text);
XmlNode rootNode = xmlDocument.DocumentElement;
foreach (XmlNode tenantNode in rootNode.ChildNodes) {
if (String.Equals(tenantNode.Name, name, StringComparison.OrdinalIgnoreCase)) {
var serializer = new DataContractSerializer(typeof(ShellTopologyDescriptor));
var reader = new StringReader(tenantNode.InnerText);
using (var xmlReader = XmlReader.Create(reader)) {
return (ShellTopologyDescriptor) serializer.ReadObject(xmlReader, true);
}
}
}
return null;
}
public void Store(string name, ShellTopologyDescriptor descriptor) {
VerifyCacheFile();
_cache[name] = descriptor;
var text = _appDataFolder.ReadFile(TopologyCacheFileName);
bool tenantCacheUpdated = false;
var saveWriter = new StringWriter();
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(text);
XmlNode rootNode = xmlDocument.DocumentElement;
foreach (XmlNode tenantNode in rootNode.ChildNodes) {
if (String.Equals(tenantNode.Name, name, StringComparison.OrdinalIgnoreCase)) {
var serializer = new DataContractSerializer(typeof (ShellTopologyDescriptor));
var writer = new StringWriter();
using (var xmlWriter = XmlWriter.Create(writer)) {
serializer.WriteObject(xmlWriter, descriptor);
}
tenantNode.InnerText = writer.ToString();
tenantCacheUpdated = true;
break;
}
}
if (!tenantCacheUpdated) {
XmlElement newTenant = xmlDocument.CreateElement(name);
var serializer = new DataContractSerializer(typeof(ShellTopologyDescriptor));
var writer = new StringWriter();
using (var xmlWriter = XmlWriter.Create(writer)) {
serializer.WriteObject(xmlWriter, descriptor);
}
newTenant.InnerText = writer.ToString();
rootNode.AppendChild(newTenant);
}
xmlDocument.Save(saveWriter);
_appDataFolder.CreateFile(TopologyCacheFileName, saveWriter.ToString());
}
#endregion
private void VerifyCacheFile() {
if (!_appDataFolder.FileExists(TopologyCacheFileName)) {
_appDataFolder.CreateFile(TopologyCacheFileName, String.Empty);
var writer = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(writer)) {
if (xmlWriter != null) {
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Tenants");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
}
_appDataFolder.CreateFile(TopologyCacheFileName, writer.ToString());
}
}
}

View File

@@ -80,6 +80,9 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Runtime.Serialization">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
<Reference Include="System.Transactions" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Abstractions">