Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server-side caching of viewmodels #704

Merged
merged 20 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
using Microsoft.Owin.Security.DataProtection;
using Newtonsoft.Json.Linq;
using Microsoft.Owin.Infrastructure;
using DotVVM.Framework.Runtime.Caching;
using DotVVM.Framework.Hosting.Owin.Runtime.Caching;
using DotVVM.Framework.Routing;

namespace DotVVM.Framework.Tests.Runtime
{
Expand All @@ -37,6 +40,7 @@ public void TestInit()
services.AddTransient<IViewModelProtector, DefaultViewModelProtector>();
services.AddTransient<ICsrfProtector, DefaultCsrfProtector>();
services.AddSingleton<ICookieManager, ChunkingCookieManager>();
services.AddSingleton<IDotvvmCacheAdapter, OwinDotvvmCacheAdapter>();
});
configuration.Security.SigningKey = Convert.FromBase64String("Uiq1FXs016lC6QaWIREB7H2P/sn4WrxkvFkqaIKpB27E7RPuMipsORgSgnT+zJmUu8zXNSJ4BdL73JEMRDiF6A1ScRNwGyDxDAVL3nkpNlGrSoLNM1xHnVzSbocLFDrdEiZD2e3uKujguycvWSNxYzjgMjXNsaqvCtMu/qRaEGc=");
configuration.Security.EncryptionKey = Convert.FromBase64String("jNS9I3ZcxzsUSPYJSwzCOm/DEyKFNlBmDGo9wQ6nxKg=");
Expand All @@ -54,11 +58,11 @@ public void TestInit()


serializer = configuration.ServiceLocator.GetService<IViewModelSerializer>() as DefaultViewModelSerializer;
context = new DotvvmRequestContext()
{
context = new DotvvmRequestContext() {
Configuration = configuration,
HttpContext = contextMock.Object,
Presenter = configuration.RouteTable.GetDefaultPresenter(configuration.ServiceProvider),
Route = new DotvvmRoute("TestRoute", "test.dothtml", new { }, p => p.GetService<DotvvmPresenter>(), configuration)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ public class DotvvmExperimentalFeaturesConfiguration
{

// Add a property od DotvvmExperimentalFeatureFlag for each experimental feature here

[JsonProperty("lazyCsrfToken", DefaultValueHandling = DefaultValueHandling.Ignore)]
public DotvvmExperimentalFeatureFlag LazyCsrfToken { get; private set; } = new DotvvmExperimentalFeatureFlag();

[JsonProperty("serverSideViewModelCache", DefaultValueHandling = DefaultValueHandling.Ignore)]
public DotvvmExperimentalFeatureFlag ServerSideViewModelCache { get; private set; } = new DotvvmExperimentalFeatureFlag();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static IServiceCollection RegisterDotVVMServices(IServiceCollection servi
services.TryAddSingleton<IDotvvmViewBuilder, DefaultDotvvmViewBuilder>();
services.TryAddSingleton<IViewModelSerializer, DefaultViewModelSerializer>();
services.TryAddSingleton<IViewModelLoader, DefaultViewModelLoader>();
services.TryAddSingleton<IViewModelServerCache, DefaultViewModelServerCache>();
services.TryAddSingleton<IViewModelServerStore, InMemoryViewModelServerStore>();
services.TryAddSingleton<IStaticCommandServiceLoader, DefaultStaticCommandServiceLoader>();
services.TryAddSingleton<IViewModelValidationMetadataProvider, AttributeViewModelValidationMetadataProvider>();
services.TryAddSingleton<IValidationRuleTranslator, ViewModelValidationRuleTranslator>();
Expand Down
1 change: 1 addition & 0 deletions src/DotVVM.Framework/DotVVM.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.1" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
Expand Down
6 changes: 6 additions & 0 deletions src/DotVVM.Framework/Hosting/DotvvmPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ public async Task ProcessRequestCore(IDotvvmRequestContext context)

foreach (var f in requestFilters) await f.OnPageRenderedAsync(context);
}
catch (DotvvmInterruptRequestExecutionException ex) when (ex.InterruptReason == InterruptReason.CachedViewModelMissing)
{
// the client needs to repeat the postback and send the full viewmodel
context.SetCachedViewModelMissingResponse();
throw;
}
catch (DotvvmInterruptRequestExecutionException) { throw; }
catch (DotvvmHttpException) { throw; }
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ public static void RedirectToRoutePermanent(this IDotvvmRequestContext context,
public static void SetRedirectResponse(this IDotvvmRequestContext context, string url, int statusCode = (int)HttpStatusCode.Redirect, bool replaceInHistory = false, bool allowSpaRedirect = false) =>
context.Configuration.ServiceProvider.GetRequiredService<IHttpRedirectService>().WriteRedirectResponse(context.HttpContext, url, statusCode, replaceInHistory, allowSpaRedirect);

internal static void SetCachedViewModelMissingResponse(this IDotvvmRequestContext context)
{
context.HttpContext.Response.StatusCode = 200;
context.HttpContext.Response.ContentType = "application/json";
context.HttpContext.Response.Write(DefaultViewModelSerializer.GenerateMissingCachedViewModelResponse());
}

/// <summary>
/// Ends the request execution when the <see cref="ModelState"/> is not valid and displays the validation errors in <see cref="ValidationSummary"/> control.
Expand Down
3 changes: 2 additions & 1 deletion src/DotVVM.Framework/Hosting/InterruptReason.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum InterruptReason
Redirect,
RedirectPermanent,
ReturnFile,
ModelValidationFailed
ModelValidationFailed,
CachedViewModelMissing
}
}
4 changes: 4 additions & 0 deletions src/DotVVM.Framework/Resources/Scripts/DotVVM.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ interface IDotvvmExtensions {
}
interface IDotvvmViewModelInfo {
viewModel?: any;
viewModelCacheId?: string;
viewModelCache?: any;
renderedResources?: string[];
url?: string;
virtualDirectory?: string;
Expand Down Expand Up @@ -333,6 +335,8 @@ declare class DotVVM {
private addLeadingSlash;
private concatUrl;
patch(source: any, patch: any): any;
diff(source: any, modified: any): any;
readonly diffEqual: {};
private updateDynamicPathFragments;
private postJSON;
private getJSON;
Expand Down
Loading