Skip to content

Commit

Permalink
Cache user hangars for 15 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-dolkens committed Mar 10, 2016
1 parent f232fcc commit bdd6b6f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
5 changes: 3 additions & 2 deletions HoloXPLOR/HoloXPLOR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
<HintPath>..\packages\Dolkens.Framework.1.0.5648.17179\lib\net45\Dolkens.Framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dolkens.Framework.Caching, Version=0.1.5560.19205, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dolkens.Framework.Caching.0.1.5560.19205\lib\net45\Dolkens.Framework.Caching.dll</HintPath>
<Reference Include="Dolkens.Framework.Caching, Version=0.1.5617.32687, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dolkens.Framework.Caching.0.1.5617.32687\lib\net45\Dolkens.Framework.Caching.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dolkens.Framework.Caching.Memory, Version=0.1.5560.19476, Culture=neutral, processorArchitecture=MSIL">
Expand Down Expand Up @@ -87,6 +87,7 @@
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
Expand Down
38 changes: 26 additions & 12 deletions HoloXPLOR/Models/HoloTable/DetailModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,55 @@
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using Dolkens.Framework.Caching;

namespace HoloXPLOR.Models.HoloTable
{
public class DetailModel
{
public String CacheKey { get { return CacheUtils.BuildCacheKey("DetailModel.Player", this._currentXML.ToLowerInvariant()); } }

public DetailModel(String id, Guid? shipID = null)
{
this._currentXML = id;

String filename = HttpContext.Current.Server.MapPath(String.Format(@"~/App_Data/Inventory/{0}.xml", this.CurrentXML));
this.Player = CacheUtils.Cache.Get(this.CacheKey) as Inventory.Player;

// Special locatio for samples
if (Path.GetFileNameWithoutExtension(filename) == "sample")
if (this.Player == null)
{
filename = HttpContext.Current.Server.MapPath(String.Format(@"~/App_Data/{0}.xml", this.CurrentXML));
String filename = HttpContext.Current.Server.MapPath(String.Format(@"~/App_Data/Inventory/{0}.xml", this.CurrentXML));

// Special locatio for samples
if (Path.GetFileNameWithoutExtension(filename) == "sample")
{
filename = HttpContext.Current.Server.MapPath(String.Format(@"~/App_Data/{0}.xml", this.CurrentXML));
}

if (File.Exists(filename))
{
this.Player = System.IO.File.ReadAllText(filename).FromXML<Inventory.Player>();
}
else
{
throw new FileNotFoundException("Unable to load specified xml", String.Format("{0}.xml", id));
}
}

if (File.Exists(filename))
if (this.Player != null)
{
this.Player = System.IO.File.ReadAllText(filename).FromXML<Inventory.Player>();
this.ShipID = shipID ?? Guid.Empty;

if (shipID.HasValue && !this.Player.Ships.Where(s => s.ID == this.ShipID).Any())
{
throw new FileNotFoundException("Unable to load specified ship from xml", String.Format("{0}.xml", id));
}

var shipItemIDs = new HashSet<Guid>(this.Player.Ships.Where(s => s.Inventory != null).Where(s => s.Inventory.Items != null).SelectMany(s => s.Inventory.Items).Select(i => i.ID));

this.Player.Inventory = new Inventory.Inventory
{
Items = this.Player.Items.Where(i => !shipItemIDs.Contains(i.ID)).Select(i => new Inventory.InventoryItem { ID = i.ID }).ToArray()
};
}
else
{
throw new FileNotFoundException("Unable to load specified xml", String.Format("{0}.xml", id));

CacheUtils.Cache.Add(this.CacheKey, this.Player, DateTime.Now.AddMinutes(15));
}
}

Expand Down Expand Up @@ -84,6 +96,8 @@ public void Save()
xw.Close();
}
}

CacheUtils.Cache.Add(this.CacheKey, this.Player, DateTime.Now.AddMinutes(15));
}

public Byte[] GetBytes()
Expand Down
2 changes: 1 addition & 1 deletion HoloXPLOR/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="BitMiracle.LibJpeg.NET" version="1.3.311.0" targetFramework="net451" />
<package id="bootstrap" version="3.3.6" targetFramework="net451" />
<package id="Dolkens.Framework" version="1.0.5648.17179" targetFramework="net451" />
<package id="Dolkens.Framework.Caching" version="0.1.5560.19205" targetFramework="net451" />
<package id="Dolkens.Framework.Caching" version="0.1.5617.32687" targetFramework="net451" />
<package id="Dolkens.Framework.Caching.Memory" version="0.1.5560.19476" targetFramework="net451" />
<package id="elmah.corelibrary" version="1.2" targetFramework="net451" />
<package id="Elmah.Mvc" version="2.1.2" targetFramework="net451" />
Expand Down

0 comments on commit bdd6b6f

Please sign in to comment.