2
2
using System . Threading . Tasks ;
3
3
using CmsEngine . Application . ViewModels ;
4
4
using CmsEngine . Core . Constants ;
5
+ using CmsEngine . Core . Exceptions ;
5
6
using CmsEngine . Data ;
6
7
using CmsEngine . Data . Entities ;
7
8
using Microsoft . AspNetCore . Http ;
@@ -14,6 +15,8 @@ public class Service : IService
14
15
{
15
16
private readonly IHttpContextAccessor _httpContextAccessor ;
16
17
private readonly IMemoryCache _memoryCache ;
18
+ private readonly string instanceHost ;
19
+ private readonly string instanceKey ;
17
20
18
21
protected readonly IUnitOfWork unitOfWork ;
19
22
protected readonly ILogger logger ;
@@ -22,7 +25,7 @@ public InstanceViewModel Instance
22
25
{
23
26
get
24
27
{
25
- return GetInstanceAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
28
+ return GetInstance ( ) ;
26
29
}
27
30
}
28
31
public UserViewModel CurrentUser
@@ -39,6 +42,9 @@ public Service(IUnitOfWork uow, IHttpContextAccessor hca, ILoggerFactory loggerF
39
42
_httpContextAccessor = hca ;
40
43
logger = loggerFactory . CreateLogger ( "Service" ) ;
41
44
_memoryCache = memoryCache ;
45
+
46
+ instanceHost = _httpContextAccessor . HttpContext . Request . Host . Host ;
47
+ instanceKey = $ "{ CmsEngineConstants . CacheKey . Instance } _{ instanceHost } ";
42
48
}
43
49
44
50
internal async Task < ApplicationUser > GetCurrentUserAsync ( )
@@ -56,7 +62,15 @@ internal async Task<ApplicationUser> GetCurrentUserAsync()
56
62
}
57
63
}
58
64
59
- private async Task < InstanceViewModel > GetInstanceAsync ( )
65
+ protected void SaveInstanceToCache ( object instance )
66
+ {
67
+ var timeSpan = TimeSpan . FromDays ( 7 ) ; //TODO: Perhaps set this in the config file. Or DB
68
+ logger . LogInformation ( "Adding '{0}' to cache with expiration date to {1}" , instanceKey , DateTime . Now . AddMilliseconds ( timeSpan . TotalMilliseconds ) . ToString ( ) ) ;
69
+ var cacheEntryOptions = new MemoryCacheEntryOptions ( ) . SetSlidingExpiration ( timeSpan ) ;
70
+ _memoryCache . Set ( instanceKey , instance , cacheEntryOptions ) ;
71
+ }
72
+
73
+ private InstanceViewModel GetInstance ( )
60
74
{
61
75
logger . LogInformation ( "GetInstanceAsync()" ) ;
62
76
@@ -65,65 +79,62 @@ private async Task<InstanceViewModel> GetInstanceAsync()
65
79
66
80
try
67
81
{
68
- logger . LogInformation ( "Loading Instance from cache" ) ;
69
- if ( ! _memoryCache . TryGetValue ( CmsEngineConstants . CacheKey . Instance , out instance ) )
82
+ logger . LogInformation ( "Loading '{0}' from cache" , instanceKey ) ;
83
+ if ( ! _memoryCache . TryGetValue ( instanceKey , out instance ) )
70
84
{
71
- logger . LogInformation ( "Empty cache for Instance . Loading instance from DB" ) ;
72
- website = await unitOfWork . Websites . GetWebsiteInstanceByHost ( _httpContextAccessor . HttpContext . Request . Host . Host ) ;
85
+ logger . LogInformation ( "Empty cache for '{0}' . Loading instance from DB" , instanceKey ) ;
86
+ website = unitOfWork . Websites . GetWebsiteInstanceByHost ( instanceHost ) ;
73
87
74
- if ( website ! = null )
88
+ if ( website = = null )
75
89
{
76
- instance = new InstanceViewModel
77
- {
78
- Id = website . Id ,
79
- Name = website . Name ,
80
- Description = website . Description ,
81
- Tagline = website . Tagline ,
82
- HeaderImage = website . HeaderImage ,
83
- Culture = website . Culture ,
84
- UrlFormat = website . UrlFormat ,
85
- DateFormat = website . DateFormat ,
86
- SiteUrl = website . SiteUrl ,
87
- ArticleLimit = website . ArticleLimit ,
88
- PageTitle = website . Name ,
89
- ContactDetails = new ContactDetailsViewModel
90
- {
91
- Address = website . Address ,
92
- Phone = website . Phone ,
93
- Email = website . Email ,
94
- } ,
95
- ApiDetails = new ApiDetailsViewModel
96
- {
97
- FacebookAppId = website . FacebookAppId ,
98
- FacebookApiVersion = website . FacebookApiVersion ,
99
- DisqusShortName = website . DisqusShortName
100
- } ,
101
- SocialMedia = new SocialMediaViewModel
102
- {
103
- Facebook = website . Facebook ,
104
- Twitter = website . Twitter ,
105
- Instagram = website . Instagram ,
106
- LinkedIn = website . LinkedIn
107
- } ,
108
- Google = new GoogleViewModel
109
- {
110
- GoogleAnalytics = website . GoogleAnalytics ,
111
- GoogleRecaptchaSiteKey = website . GoogleRecaptchaSiteKey ,
112
- GoogleRecaptchaSecretKey = website . GoogleRecaptchaSecretKey
113
- }
114
- } ;
115
-
116
- var timeSpan = TimeSpan . FromDays ( 7 ) ; //TODO: Perhaps set this in the config file. Or DB
117
-
118
- logger . LogInformation ( "Adding Instance to cache with expiration date to {0}" , DateTime . Now . AddMilliseconds ( timeSpan . TotalMilliseconds ) . ToString ( ) ) ;
119
- var cacheEntryOptions = new MemoryCacheEntryOptions ( ) . SetSlidingExpiration ( timeSpan ) ;
120
- _memoryCache . Set ( CmsEngineConstants . CacheKey . Instance , instance , cacheEntryOptions ) ;
90
+ throw new NotFoundException ( $ "Instance for '{ instanceHost } ' not found") ;
121
91
}
92
+
93
+ instance = new InstanceViewModel
94
+ {
95
+ Id = website . Id ,
96
+ Name = website . Name ,
97
+ Description = website . Description ,
98
+ Tagline = website . Tagline ,
99
+ HeaderImage = website . HeaderImage ,
100
+ Culture = website . Culture ,
101
+ UrlFormat = website . UrlFormat ,
102
+ DateFormat = website . DateFormat ,
103
+ SiteUrl = website . SiteUrl ,
104
+ ArticleLimit = website . ArticleLimit ,
105
+ PageTitle = website . Name ,
106
+ ContactDetails = new ContactDetailsViewModel
107
+ {
108
+ Address = website . Address ,
109
+ Phone = website . Phone ,
110
+ Email = website . Email ,
111
+ } ,
112
+ ApiDetails = new ApiDetailsViewModel
113
+ {
114
+ FacebookAppId = website . FacebookAppId ,
115
+ FacebookApiVersion = website . FacebookApiVersion ,
116
+ DisqusShortName = website . DisqusShortName
117
+ } ,
118
+ SocialMedia = new SocialMediaViewModel
119
+ {
120
+ Facebook = website . Facebook ,
121
+ Twitter = website . Twitter ,
122
+ Instagram = website . Instagram ,
123
+ LinkedIn = website . LinkedIn
124
+ } ,
125
+ Google = new GoogleViewModel
126
+ {
127
+ GoogleAnalytics = website . GoogleAnalytics ,
128
+ GoogleRecaptchaSiteKey = website . GoogleRecaptchaSiteKey ,
129
+ GoogleRecaptchaSecretKey = website . GoogleRecaptchaSecretKey
130
+ }
131
+ } ;
132
+
133
+ SaveInstanceToCache ( instance ) ;
122
134
}
123
135
}
124
136
catch ( Exception ex )
125
137
{
126
- logger . LogError ( ex , "Error when trying to load Instance" ) ;
127
138
throw ex ;
128
139
}
129
140
0 commit comments