// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.Linq; using System.Web.Caching; using Tools.CrashReporter.CrashReportWebSite.DataModels; using Tools.CrashReporter.CrashReportWebSite.DataModels.Repositories; using Tools.CrashReporter.CrashReportWebSite.ViewModels; namespace Tools.CrashReporter.CrashReportWebSite.Models { /// /// A class to handle caching of database results on the web server. /// public class CachedDataService : IDisposable { private CrashRepository Crashes; private BuggRepository Buggs; private Cache CacheInstance; private const string CacheKeyPrefix = "_CachedDataService_"; private const string CallstackKeyPrefix = "_CallStack_"; private const string FunctionCallKeyPrefix = "_FunctionCallStack_"; private const string UserNameKeyPrefix = "_UserName_"; /// /// Link the Http context cache to a Crash repository. /// /// The current Http context cache. /// The repository to associate the cache with. public CachedDataService( Cache inCache, CrashRepository inCrashRepository ) { CacheInstance = inCache; Crashes = inCrashRepository; } /// /// Link the Http context cache to a Bugg repository. /// /// The current Http context cache. /// The repository to associate the cache with. public CachedDataService( Cache inCache, BuggRepository inBuggRepository ) { CacheInstance = inCache; Buggs = inBuggRepository; } /// /// Implementing Dispose. /// public void Dispose() { Dispose( true ); GC.SuppressFinalize( this ); } /// /// Disposes the resources. /// /// true if the Dispose call is from user code, and not system code. protected virtual void Dispose( bool Disposing ) { //Crashes.Dispose(); //Buggs.Dispose(); } /// /// Retrieve a cached (and pre-parsed) callstack container from the cache, or parse the raw callstack, and add to the cache. /// /// The Crash to retrieve the parsed callstack for. /// A parsed callstack. public CallStackContainer GetCallStackFast(CrashDataModel currentCrash) { using( var logTimer = new FAutoScopedLogTimer( this.GetType().ToString() + "(CrashId=" + currentCrash.Id + ")" ) ) { var key = CacheKeyPrefix + CallstackKeyPrefix + currentCrash.Id; var callStack = (CallStackContainer)CacheInstance[key]; if (callStack != null) return callStack; callStack = new CallStackContainer( currentCrash.CrashType, currentCrash.RawCallStack, currentCrash.PlatformName ); callStack.bDisplayFunctionNames = true; CacheInstance.Insert( key, callStack ); return callStack; } } } }