-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add As/Put method to ISite to allow caching #14372
Changes from 5 commits
41c8f0f
1f83860
55657d2
9eb7a12
5141f39
ee28846
2e3ab6d
5c804b5
76c9663
0164938
31d0255
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,88 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Routing; | ||
using Newtonsoft.Json.Linq; | ||
using OrchardCore.Documents; | ||
using OrchardCore.Entities; | ||
|
||
namespace OrchardCore.Settings | ||
{ | ||
// When updating class also update SiteSettingsDeploymentSource and SettingsStep. | ||
public class SiteSettings : DocumentEntity, ISite | ||
{ | ||
private JObject _properties = new(); | ||
|
||
public new JObject Properties | ||
{ | ||
get => _properties; | ||
set | ||
{ | ||
_properties = value ?? new JObject(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just not mutate I didn't have the time to try it on my side but are the Yes, I never took the time to manage an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why to not set it to a new object? Yes there are many way to a a accomplish the same result. The base class sets it to a new Object and usually we use Put and As without having to worry if Properties is null. It is best to always initialize it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem to init it to a new
But here I think we could just use this as done in
Not even sure that here we need a setter, so maybe
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we have to use a setting since we are overriding a property that define a setter. If we ignore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay for the setter. In my example we still clear the cache, Hmm, if the value is null you could do I would need to focus on it a little more but, If we So not sure we need to be able to clear this cache and then to override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, also not sure that we need a I talk about making a Document There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may introduce the support of this flag in the DocumentManager itself when I will have time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jtkech do we need to wait until adding support for the DocumentManager? I don't think we should block this PR for this. I think the PR as is provides a valid value. Maybe we take this one and if you add |
||
_cache.Clear(); | ||
} | ||
} | ||
|
||
public string BaseUrl { get; set; } | ||
|
||
public string Calendar { get; set; } | ||
|
||
public int MaxPagedCount { get; set; } | ||
|
||
public int MaxPageSize { get; set; } | ||
|
||
public int PageSize { get; set; } | ||
|
||
public string TimeZoneId { get; set; } | ||
|
||
public ResourceDebugMode ResourceDebugMode { get; set; } | ||
|
||
public string SiteName { get; set; } | ||
|
||
public string SiteSalt { get; set; } | ||
|
||
public string PageTitleFormat { get; set; } | ||
|
||
public string SuperUser { get; set; } | ||
|
||
public bool UseCdn { get; set; } | ||
|
||
public string CdnBaseUrl { get; set; } | ||
|
||
public RouteValueDictionary HomeRoute { get; set; } = new RouteValueDictionary(); | ||
|
||
public bool AppendVersion { get; set; } = true; | ||
|
||
public CacheMode CacheMode { get; set; } | ||
|
||
public T As<T>() where T : new() | ||
{ | ||
var name = typeof(T).Name; | ||
|
||
if (_cache.TryGetValue(name, out var obj) && obj is T value) | ||
{ | ||
return value; | ||
} | ||
|
||
var settings = this.As<T>(name); | ||
MikeAlhayek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
_cache[name] = settings; | ||
|
||
return settings; | ||
} | ||
|
||
public ISite Put<T>(T settings) where T : new() | ||
{ | ||
var name = typeof(T).Name; | ||
|
||
if (settings is not null) | ||
{ | ||
this.Put(name, settings); | ||
} | ||
|
||
_cache.Remove(name); | ||
|
||
return this; | ||
} | ||
|
||
private readonly Dictionary<string, object> _cache = new(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,43 @@ | ||
using Microsoft.AspNetCore.Routing; | ||
using OrchardCore.Entities; | ||
|
||
namespace OrchardCore.Settings | ||
namespace OrchardCore.Settings; | ||
|
||
public interface ISite : IEntity | ||
{ | ||
public interface ISite : IEntity | ||
{ | ||
string SiteName { get; set; } | ||
string PageTitleFormat { get; set; } | ||
string SiteSalt { get; set; } | ||
string SuperUser { get; set; } | ||
string Calendar { get; set; } | ||
string TimeZoneId { get; set; } | ||
ResourceDebugMode ResourceDebugMode { get; set; } | ||
bool UseCdn { get; set; } | ||
string CdnBaseUrl { get; set; } | ||
int PageSize { get; set; } | ||
int MaxPageSize { get; set; } | ||
int MaxPagedCount { get; set; } | ||
string BaseUrl { get; set; } | ||
RouteValueDictionary HomeRoute { get; set; } | ||
bool AppendVersion { get; set; } | ||
CacheMode CacheMode { get; set; } | ||
} | ||
string SiteName { get; set; } | ||
|
||
string PageTitleFormat { get; set; } | ||
|
||
string SiteSalt { get; set; } | ||
|
||
string SuperUser { get; set; } | ||
|
||
string Calendar { get; set; } | ||
|
||
string TimeZoneId { get; set; } | ||
|
||
ResourceDebugMode ResourceDebugMode { get; set; } | ||
|
||
bool UseCdn { get; set; } | ||
|
||
string CdnBaseUrl { get; set; } | ||
|
||
int PageSize { get; set; } | ||
|
||
int MaxPageSize { get; set; } | ||
|
||
int MaxPagedCount { get; set; } | ||
|
||
string BaseUrl { get; set; } | ||
|
||
RouteValueDictionary HomeRoute { get; set; } | ||
|
||
bool AppendVersion { get; set; } | ||
|
||
CacheMode CacheMode { get; set; } | ||
|
||
T As<T>() where T : new(); | ||
|
||
ISite Put<T>(T aspect) where T : new(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using OrchardCore.Settings; | ||
|
||
namespace OrchardCore.Tests.Utilities; | ||
|
||
public class SiteMockHelper | ||
{ | ||
public static Mock<ISite> GetSite<T>(T obj) where T : new() | ||
{ | ||
var properties = new JObject | ||
{ | ||
[obj.GetType().Name] = JObject.FromObject(obj) | ||
}; | ||
|
||
var mockSite = new Mock<ISite>(); | ||
mockSite.Setup(x => x.Properties) | ||
.Returns(properties); | ||
|
||
mockSite.Setup(x => x.As<T>()) | ||
.Returns(obj); | ||
|
||
return mockSite; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no a simple dictionary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The simple dictionary is below line 86
Here it is related to line 14 that defines a
new
Properties to override theEntity.Properties
.