-
Notifications
You must be signed in to change notification settings - Fork 28
/
MetaFieldsValueService.cs
65 lines (58 loc) · 2.59 KB
/
MetaFieldsValueService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Extensions;
using SeoToolkit.Umbraco.MetaFields.Core.Interfaces.Services;
using SeoToolkit.Umbraco.MetaFields.Core.Repositories.SeoValueRepository;
using SeoToolkit.Umbraco.MetaFields.Core.Constants;
using Microsoft.Extensions.Caching.Distributed;
using SeoToolkit.Umbraco.MetaFields.Core.Caching;
namespace SeoToolkit.Umbraco.MetaFields.Core.Services.SeoValueService
{
public class MetaFieldsValueService : IMetaFieldsValueService
{
private readonly IMetaFieldsValueRepository _repository;
private readonly IVariationContextAccessor _variationContextAccessor;
private readonly DistributedCache _distributedCache;
private readonly IAppPolicyCache _cache;
public MetaFieldsValueService(IMetaFieldsValueRepository repository, IVariationContextAccessor variationContextAccessor, AppCaches appCaches, DistributedCache distributedCache)
{
_repository = repository;
_variationContextAccessor = variationContextAccessor;
_distributedCache = distributedCache;
_cache = appCaches.RuntimeCache;
}
public Dictionary<string, object> GetUserValues(int nodeId, string culture = null)
{
var foundCulture = culture.IfNullOrWhiteSpace(GetCulture());
return _cache.GetCacheItem($"{CacheConstants.SeoValue}{nodeId}_{foundCulture}", () => _repository.GetAllValues(nodeId, foundCulture), TimeSpan.FromMinutes(30));
}
public void AddValues(int nodeId, Dictionary<string, object> values, string culture = null)
{
var foundCulture = culture.IfNullOrWhiteSpace(GetCulture());
foreach (var (key, value) in values)
{
if (_repository.Exists(nodeId, key, foundCulture))
_repository.Update(nodeId, key, foundCulture, value);
else
{
_repository.Add(nodeId, key, foundCulture, value);
}
}
ClearCache(nodeId);
}
public void Delete(int nodeId, string fieldAlias, string culture = null)
{
_repository.Delete(nodeId, fieldAlias, culture.IfNullOrWhiteSpace(GetCulture()));
}
private string GetCulture()
{
return _variationContextAccessor.VariationContext.Culture;
}
private void ClearCache(int nodeId)
{
_distributedCache.Refresh(SeoValueCacheRefresher.CacheGuid, nodeId);
}
}
}