mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-05 21:01:35 +08:00
caching sitecultureselector and sslsettingspart (#8289)
This commit is contained in:
parent
104d18aa06
commit
abfe7a022e
@ -3,12 +3,24 @@ using Orchard.Data;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Localization;
|
||||
using Orchard.SecureSocketsLayer.Models;
|
||||
using Orchard.Caching;
|
||||
|
||||
namespace Orchard.SecureSocketsLayer.Handlers {
|
||||
public class SslSettingsPartHandler : ContentHandler {
|
||||
public SslSettingsPartHandler() {
|
||||
private readonly ISignals _signals;
|
||||
|
||||
public SslSettingsPartHandler(ISignals signals) {
|
||||
|
||||
_signals = signals;
|
||||
|
||||
T = NullLocalizer.Instance;
|
||||
|
||||
Filters.Add(new ActivatingFilter<SslSettingsPart>("Site"));
|
||||
|
||||
// Evict cached content when updated, removed or destroyed.
|
||||
OnPublished<SslSettingsPart>((context, part) => Invalidate(part));
|
||||
OnRemoved<SslSettingsPart>((context, part) => Invalidate(part));
|
||||
OnDestroyed<SslSettingsPart>((context, part) => Invalidate(part));
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
@ -22,5 +34,10 @@ namespace Orchard.SecureSocketsLayer.Handlers {
|
||||
Position = "2"
|
||||
});
|
||||
}
|
||||
|
||||
private void Invalidate(SslSettingsPart content) {
|
||||
_signals.Trigger($"SslSettingsPart_{content.Id}");
|
||||
_signals.Trigger("SslSettingsPart_EvictAll");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Caching;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Owin;
|
||||
@ -8,11 +9,19 @@ using Owin;
|
||||
namespace Orchard.SecureSocketsLayer.Services {
|
||||
public class StrictTransportSecurityMiddlewareProvider : IOwinMiddlewareProvider {
|
||||
private readonly IWorkContextAccessor _wca;
|
||||
private readonly ICacheManager _cacheManager;
|
||||
private readonly ISignals _signals;
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
public StrictTransportSecurityMiddlewareProvider(IWorkContextAccessor wca) {
|
||||
public StrictTransportSecurityMiddlewareProvider(
|
||||
IWorkContextAccessor wca,
|
||||
ICacheManager cacheManager,
|
||||
ISignals signals) {
|
||||
|
||||
_wca = wca;
|
||||
_cacheManager = cacheManager;
|
||||
_signals = signals;
|
||||
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
@ -22,7 +31,13 @@ namespace Orchard.SecureSocketsLayer.Services {
|
||||
new OwinMiddlewareRegistration {
|
||||
Configure = app =>
|
||||
app.Use(async (context, next) => {
|
||||
var sslSettings = _wca.GetContext().CurrentSite.As<SslSettingsPart>();
|
||||
var cacheKey = "Orchard.SecureSocketsLayer.Services.StrictTransportSecurityMiddlewareProvider.GetOwinMiddlewares";
|
||||
var sslSettings = _cacheManager.Get(cacheKey, true, ctx =>{
|
||||
// check whether the cache should be invalidated
|
||||
ctx.Monitor(_signals.When("SslSettingsPart_EvictAll"));
|
||||
// cache this and save recomputing it each call
|
||||
return _wca.GetContext().CurrentSite.As<SslSettingsPart>();
|
||||
});
|
||||
|
||||
if (sslSettings.SendStrictTransportSecurityHeaders) {
|
||||
string responseValue = "max-age=" + sslSettings.StrictTransportSecurityMaxAge;
|
||||
|
@ -1,22 +1,36 @@
|
||||
using System;
|
||||
using System.Web;
|
||||
using Orchard.Caching;
|
||||
|
||||
namespace Orchard.Localization.Services {
|
||||
public class SiteCultureSelector : ICultureSelector {
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
private readonly ICacheManager _cacheManager;
|
||||
private readonly ISignals _signals;
|
||||
|
||||
public SiteCultureSelector(
|
||||
IWorkContextAccessor workContextAccessor,
|
||||
ICacheManager cacheManager,
|
||||
ISignals signals) {
|
||||
|
||||
public SiteCultureSelector(IWorkContextAccessor workContextAccessor) {
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_cacheManager = cacheManager;
|
||||
_signals = signals;
|
||||
}
|
||||
|
||||
public CultureSelectorResult GetCulture(HttpContextBase context) {
|
||||
string currentCultureName = _workContextAccessor.GetContext().CurrentSite.SiteCulture;
|
||||
var cacheKey = "Orchard.Localization.Services.SiteCultureSelector.GetCulture";
|
||||
return _cacheManager.Get(cacheKey, true, ctx => {
|
||||
// this is the same signal used in Orchard.Framework.DefaultCultureManager
|
||||
ctx.Monitor(_signals.When("culturesChanged"));
|
||||
|
||||
if (String.IsNullOrEmpty(currentCultureName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new CultureSelectorResult { Priority = -5, CultureName = currentCultureName };
|
||||
string currentCultureName = _workContextAccessor.GetContext().CurrentSite.SiteCulture;
|
||||
if (String.IsNullOrEmpty(currentCultureName)) {
|
||||
return null;
|
||||
}
|
||||
return new CultureSelectorResult { Priority = -5, CultureName = currentCultureName };
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user