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

change approach to reset cache #629

Merged
merged 6 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<Product>VirtoCommerce</Product>
<Copyright>Copyright © VirtoCommerce 2011-2020</Copyright>
<Copyright>Copyright © VirtoCommerce 2011-2022</Copyright>
<Authors>VirtoCommerce</Authors>
</PropertyGroup>
<PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions VirtoCommerce.Storefront.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
# Visual Studio Version 17
VisualStudioVersion = 17.0.32112.339
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VirtoCommerce.Storefront", "VirtoCommerce.Storefront\VirtoCommerce.Storefront.csproj", "{84C14E0A-0F36-4BB5-9C00-7E8FB72891C2}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using VirtoCommerce.Storefront.Model;
using VirtoCommerce.Storefront.Model.Common;
using VirtoCommerce.Storefront.Model.StaticContent;
using VirtoCommerce.Storefront.Models;

namespace VirtoCommerce.Storefront.Controllers.Api
{
Expand All @@ -20,19 +21,44 @@ public ApiStaticContentController(IWorkContextAccessor workContextAccessor, ISto
{
}

// POST: storefrontapi/content/reset-cache
[HttpPost("reset-cache")]
public ActionResult ResetCache([FromBody] ResetCacheEventModel webHookEvent)
{
if (TryResetCacheInternal(webHookEvent?.EventBody?.FirstOrDefault()?.Type))
{
return Ok("OK");
}

return Ok("Failed");
}

// POST: storefrontapi/content/reset-cache/theme
[HttpPost("reset-cache/{region}")]
public ActionResult ResetCache([FromRoute] string region)
{
if (TryResetCacheInternal(region))
{
return Ok("OK");
}

return Ok("Failed");
}

private static bool TryResetCacheInternal(string region)
{
switch (region)
{
case "theme": ThemeEngineCacheRegion.ExpireRegion();
break;
case "theme":
case "themes":
ThemeEngineCacheRegion.ExpireRegion();
return true;
case "pages":
case "blogs":
StaticContentCacheRegion.ExpireRegion();
break;
return true;
}
return Ok();
return false;
}

// POST: storefrontapi/content/pages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected void AddCurrentUser(HttpRequestMessage request)
/// <param name="request"></param>
protected void AddUserIp(HttpRequestMessage request)
{
var userIp = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
var userIp = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress.ToString();

if (!string.IsNullOrEmpty(userIp))
{
Expand Down
29 changes: 29 additions & 0 deletions VirtoCommerce.Storefront/Models/ResetCacheModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VirtoCommerce.Storefront.Models
{
/// <summary>
/// Cache Event Model For Web Hook from Virto Commmerce Platform
/// </summary>
public class ResetCacheEventModel
{
public string EventId { get; set; }

public ResetCacheEventBodyModel[] EventBody { get; set; }
}

public class ResetCacheEventBodyModel
{
public string ObjectType { get; set; }

public string Id { get; set; }

public string Path { get; set; }

public string Type { get; set; }
}
}
10 changes: 5 additions & 5 deletions VirtoCommerce.Storefront/VirtoCommerce.Storefront.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<UserSecretsId>0cd403c4-2cd0-42b3-987a-02900f4a683e</UserSecretsId>
<Description>The storefront implementation of the Virto Commerce platform.</Description>
<PackageLicenseUrl>https://virtocommerce.com/open-source-license</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/VirtoCommerce/vc-storefront-core</PackageProjectUrl>
<RepositoryUrl>https://github.com/VirtoCommerce/vc-storefront-core</RepositoryUrl>
<PackageProjectUrl>https://github.com/VirtoCommerce/vc-storefront</PackageProjectUrl>
<RepositoryUrl>https://github.com/VirtoCommerce/vc-storefront</RepositoryUrl>

<PackageIconUrl>https://virtocommerce.com/themes/assets/logo.jpg</PackageIconUrl>
<PackageReleaseNotes></PackageReleaseNotes>
Expand Down Expand Up @@ -47,10 +47,10 @@
<PackageReference Include="FluentValidation.AspNetCore" Version="10.4.0" />

<PackageReference Include="Markdig" Version="0.30.2" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.20.0" />
<PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.20.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.SnapshotCollector" Version="1.4.3" />
<PackageReference Include="Microsoft.ApplicationInsights.Web" Version="2.20.0" />
<PackageReference Include="Microsoft.ApplicationInsights.Web" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Facebook" Version="6.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="6.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.4" />
Expand Down