Update: Configuration Optimisation and Caching

Loads entire configuration at start-up (rather than scope-based
loading). Deserialization occurs once, with the resulting value cached
and replayed if the requested type matches.
This commit is contained in:
Gary Sharp
2014-05-07 22:45:59 +10:00
parent 6b2cd47610
commit fb6067afc3
19 changed files with 455 additions and 282 deletions
@@ -1,9 +1,5 @@
using Disco.Data.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Disco.Data.Configuration.Modules
{
@@ -20,18 +16,18 @@ namespace Disco.Data.Configuration.Modules
{
get
{
return GetFromJson<Dictionary<string, List<string>>>(null);
return Get<Dictionary<string, List<string>>>(null);
}
set
{
SetAsJson(value);
Set(value);
}
}
public bool? SearchAllForestServers
{
get { return GetFromJson<bool?>(null); }
set { SetAsJson(value); }
get { return Get<bool?>(null); }
set { Set(value); }
}
}
}
@@ -1,8 +1,4 @@
using Disco.Data.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Disco.Data.Configuration.Modules
{
@@ -1,86 +0,0 @@
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//
//
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using Disco.Models.Repository;
//namespace Disco.Data.Configuration.Modules
//{
// public class DeviceProfileConfiguration : ConfigurationBase
// {
// private DeviceProfilesConfiguration deviceProfilesConfig;
// private DeviceProfile deviceProfile;
// public DeviceProfileConfiguration(ConfigurationContext Context, DeviceProfile DeviceProfile)
// : base(Context)
// {
// this.deviceProfilesConfig = Context.DeviceProfiles;
// this.deviceProfile = DeviceProfile;
// }
// public override string Scope
// {
// get
// {
// return string.Format("DeviceProfile:{0}", this.deviceProfile.Id);
// }
// }
// public string ComputerNameTemplate
// {
// get
// {
// return this.GetValue("ComputerNameTemplate", "DeviceProfile.ShortName + '-' + SerialNumber");
// }
// set
// {
// this.SetValue("ComputerNameTemplate", value);
// }
// }
// public enum DeviceProfileDistributionTypes : int
// {
// OneToMany = 0,
// OneToOne = 1
// }
// public DeviceProfileDistributionTypes DistributionType
// {
// get
// {
// return (DeviceProfileDistributionTypes)this.GetValue("DistributionType", (int)DeviceProfileDistributionTypes.OneToMany);
// }
// set
// {
// this.SetValue("DistributionType", (int)value);
// }
// }
// public string OrganisationalUnit
// {
// get
// {
// return this.GetValue<string>("OrganisationalUnit", null);
// }
// set
// {
// this.SetValue("OrganisationalUnit", value);
// }
// }
// public bool AllocateWirelessCertificate
// {
// get
// {
// return this.GetValue("AllocateWirelessCertificate", false);
// }
// set
// {
// this.SetValue("AllocateWirelessCertificate", value);
// }
// }
// }
//}
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Disco.Models.Repository;
using Disco.Data.Repository;
using Disco.Data.Repository;
using System;
namespace Disco.Data.Configuration.Modules
{
@@ -1,10 +1,7 @@
using Disco.Data.Repository;
using Disco.Models.BI.Job;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Disco.Models.BI.Job;
namespace Disco.Data.Configuration.Modules
{
@@ -46,14 +43,14 @@ namespace Disco.Data.Configuration.Modules
public LocationModes LocationMode
{
get { return GetFromEnum<LocationModes>(LocationModes.Unrestricted); }
set { SetAsEnum(value); }
get { return Get<LocationModes>(LocationModes.Unrestricted); }
set { Set(value); }
}
public List<string> LocationList
{
get { return GetFromJson<List<string>>(new List<string>()); }
set { SetAsJson(value); }
get { return Get<List<string>>(new List<string>()); }
set { Set(value); }
}
}
}
@@ -1,38 +1,22 @@
using System;
using Disco.Data.Repository;
using Disco.Models.BI.Config;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Disco.Models.BI.Config;
using Disco.Models.Repository;
using Newtonsoft.Json;
using Disco.Data.Repository;
namespace Disco.Data.Configuration.Modules
{
public class OrganisationAddressesConfiguration : ConfigurationBase
{
private const string scope = "OrganisationAddresses";
public OrganisationAddressesConfiguration(DiscoDataContext Database) : base(Database) { }
public override string Scope { get { return "OrganisationAddresses"; } }
public override string Scope { get { return scope; } }
public OrganisationAddress GetAddress(int Id)
{
var address = default(OrganisationAddress);
var addressString = this.Get<string>(null, Id.ToString());
if (addressString != null)
{
if (addressString.StartsWith("{"))
{
// Assume Json
address = JsonConvert.DeserializeObject<OrganisationAddress>(addressString);
}
else
{
// Assume Old Storage Method
address = OrganisationAddress.FromConfigurationEntry(Id, addressString);
}
}
return address;
return this.Get<OrganisationAddress>(null, Id.ToString());
}
public OrganisationAddress SetAddress(OrganisationAddress Address)
{
@@ -41,25 +25,21 @@ namespace Disco.Data.Configuration.Modules
Address.Id = NextOrganisationAddressId;
}
string addressString = JsonConvert.SerializeObject(Address);
this.Set(Address, Address.Id.ToString());
this.Set(addressString, Address.Id.ToString()); //Address.ToConfigurationEntry());
return Address;
}
public void RemoveAddress(int Id)
{
// Set Config Item to null = Remove Configuration Item
this.Set<string>(null, Id.ToString());
// Remove Configuration Item
this.RemoveItem(Id.ToString());
}
public List<OrganisationAddress> Addresses
{
get
{
return this.Items.Select(ca => ca.Value.StartsWith("{") ?
JsonConvert.DeserializeObject<OrganisationAddress>(ca.Value) :
OrganisationAddress.FromConfigurationEntry(int.Parse(ca.Key), ca.Value)
).ToList();
return this.ItemKeys.Select(key => this.Get<OrganisationAddress>(null, key)).ToList();
}
}
@@ -78,5 +58,19 @@ namespace Disco.Data.Configuration.Modules
}
}
internal static void MigrateDatabase(DiscoDataContext Database)
{
// Migrate all organisation addresses to JSON
if (Database.ConfigurationItems.Count(i => i.Scope == scope && !i.Value.StartsWith("{")) > 0)
{
var items = Database.ConfigurationItems.Where(i => i.Scope == scope && !i.Value.StartsWith("{")).ToList();
items.ForEach(i =>
{
i.Value = JsonConvert.SerializeObject(OrganisationAddress.FromConfigurationEntry(int.Parse(i.Key), i.Value));
});
Database.SaveChanges();
}
}
}
}