Skip to content

Commit d4b1836

Browse files
committed
Refactoring of the caching system
1 parent 29d0a03 commit d4b1836

File tree

279 files changed

+1179
-1239
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

279 files changed

+1179
-1239
lines changed

src/Libraries/Nop.Core/Caching/CacheKey.cs

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System.Collections.Generic;
2-
using System.Globalization;
1+
using System;
2+
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
54

65
namespace Nop.Core.Caching
76
{
@@ -15,6 +14,19 @@ public partial class CacheKey
1514

1615
#region Ctor
1716

17+
public CacheKey(CacheKey cacheKey, Func<object, object> createCacheKeyParameters, params object[] keyObjects)
18+
{
19+
Init(cacheKey.Key, cacheKey.CacheTime, cacheKey.Prefixes.ToArray());
20+
21+
if(!keyObjects.Any())
22+
return;
23+
24+
Key = string.Format(_keyFormat, keyObjects.Select(createCacheKeyParameters).ToArray());
25+
26+
for (var i = 0; i < Prefixes.Count; i++)
27+
Prefixes[i] = string.Format(Prefixes[i], keyObjects.Select(createCacheKeyParameters).ToArray());
28+
}
29+
1830
public CacheKey(string cacheKey, int? cacheTime = null, params string[] prefixes)
1931
{
2032
Init(cacheKey, cacheTime, prefixes);
@@ -44,74 +56,20 @@ protected void Init(string cacheKey, int? cacheTime = null, params string[] pref
4456
if (cacheTime.HasValue)
4557
CacheTime = cacheTime.Value;
4658

47-
Prefixes.AddRange(prefixes);
48-
}
49-
50-
/// <summary>
51-
/// Creates the hash sum of identifiers list
52-
/// </summary>
53-
/// <param name="ids"></param>
54-
/// <returns></returns>
55-
private static string CreateIdsHash(IEnumerable<int> ids)
56-
{
57-
var identifiers = ids.ToList();
58-
59-
if (!identifiers.Any())
60-
return string.Empty;
61-
62-
return HashHelper.CreateHash(Encoding.UTF8.GetBytes(string.Join(", ", identifiers.OrderBy(id => id))), "SHA512");
63-
}
64-
65-
/// <summary>
66-
/// Converts an object to cache parameter
67-
/// </summary>
68-
/// <param name="parameter">Object to convert</param>
69-
/// <returns>Cache parameter</returns>
70-
private static object CreateCacheKeyParameters(object parameter)
71-
{
72-
return parameter switch
73-
{
74-
null => "null",
75-
IEnumerable<int> ids => CreateIdsHash(ids),
76-
IEnumerable<BaseEntity> entities => CreateIdsHash(entities.Select(e => e.Id)),
77-
BaseEntity entity => entity.Id,
78-
decimal param => param.ToString(CultureInfo.InvariantCulture),
79-
_ => parameter,
80-
};
59+
Prefixes.AddRange(prefixes.Where(prefix=> !string.IsNullOrEmpty(prefix)));
8160
}
8261

8362
#endregion
8463

85-
/// <summary>
86-
/// Fills the cache key
87-
/// </summary>
88-
/// <param name="keyObjects">Parameters to create cache key</param>
89-
/// <returns>Cache key</returns>
90-
public CacheKey FillCacheKey(params object[] keyObjects)
91-
{
92-
Key = keyObjects?.Any() ?? false
93-
? string.Format(_keyFormat, keyObjects.Select(CreateCacheKeyParameters).ToArray())
94-
: Key;
95-
96-
for (var i = 0; i < Prefixes.Count; i++)
97-
{
98-
Prefixes[i] = keyObjects?.Any() ?? false
99-
? string.Format(Prefixes[i], keyObjects.Select(CreateCacheKeyParameters).ToArray())
100-
: Prefixes[i];
101-
}
102-
103-
return this;
104-
}
105-
10664
/// <summary>
10765
/// Cache key
10866
/// </summary>
109-
public string Key { get; set; }
67+
public string Key { get; protected set; }
11068

11169
/// <summary>
11270
/// Prefixes to remove by prefix functionality
11371
/// </summary>
114-
public List<string> Prefixes { get; set; } = new List<string>();
72+
public List<string> Prefixes { get; protected set; } = new List<string>();
11573

11674
/// <summary>
11775
/// Cache time in minutes

src/Libraries/Nop.Core/Domain/Vendors/NopVendorDefaults.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Libraries/Nop.Services/Affiliates/AffiliateService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Nop.Data;
99
using Nop.Services.Caching.Extensions;
1010
using Nop.Services.Common;
11-
using Nop.Services.Defaults;
1211
using Nop.Services.Events;
1312
using Nop.Services.Seo;
1413

@@ -163,6 +162,7 @@ where a_o.Any()
163162
query = query.OrderByDescending(a => a.Id);
164163

165164
var affiliates = new PagedList<Affiliate>(query, pageIndex, pageSize);
165+
166166
return affiliates;
167167
}
168168

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Nop.Core.Domain.Affiliates;
2+
using Nop.Services.Caching;
23

3-
namespace Nop.Services.Caching.CacheEventConsumers.Affiliates
4+
namespace Nop.Services.Affiliates.Caching
45
{
56
/// <summary>
67
/// Represents an affiliate cache event consumer

src/Libraries/Nop.Services/Defaults/NopAffiliateDefaults.cs renamed to src/Libraries/Nop.Services/Affiliates/NopAffiliateDefaults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Nop.Services.Defaults
1+
namespace Nop.Services.Affiliates
22
{
33
/// <summary>
44
/// Represents default values related to affiliate services

src/Libraries/Nop.Services/Blogs/BlogService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Nop.Core.Domain.Catalog;
88
using Nop.Core.Domain.Stores;
99
using Nop.Data;
10-
using Nop.Services.Caching.CachingDefaults;
10+
using Nop.Services.Caching;
1111
using Nop.Services.Caching.Extensions;
1212
using Nop.Services.Events;
1313

@@ -171,7 +171,7 @@ public virtual IPagedList<BlogPost> GetAllBlogPostsByTag(int storeId = 0,
171171
/// <returns>Blog post tags</returns>
172172
public virtual IList<BlogPostTag> GetAllBlogPostTags(int storeId, int languageId, bool showHidden = false)
173173
{
174-
var cacheKey = NopBlogsCachingDefaults.BlogTagsModelCacheKey.FillCacheKey(languageId, storeId);
174+
var cacheKey = NopBlogsDefaults.BlogTagsModelCacheKey.FillCacheKey(languageId, storeId);
175175

176176
var blogPostTags = _cacheManager.Get(cacheKey, () =>
177177
{
@@ -395,7 +395,7 @@ public virtual int GetBlogCommentsCount(BlogPost blogPost, int storeId = 0, bool
395395
if (isApproved.HasValue)
396396
query = query.Where(comment => comment.IsApproved == isApproved.Value);
397397

398-
var cacheKey = NopBlogsCachingDefaults.BlogCommentsNumberCacheKey.FillCacheKey(blogPost, storeId, isApproved);
398+
var cacheKey = NopBlogsDefaults.BlogCommentsNumberCacheKey.FillCacheKey(blogPost, storeId, isApproved);
399399

400400
return _cacheManager.Get(cacheKey, () => query.Count());
401401
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Nop.Core.Domain.Blogs;
2-
using Nop.Services.Caching.CachingDefaults;
2+
using Nop.Services.Caching;
33

4-
namespace Nop.Services.Caching.CacheEventConsumers.Blogs
4+
namespace Nop.Services.Blogs.Caching
55
{
66
/// <summary>
77
/// Represents a blog comment cache event consumer
@@ -14,7 +14,7 @@ public partial class BlogCommentCacheEventConsumer : CacheEventConsumer<BlogComm
1414
/// <param name="entity">Entity</param>
1515
protected override void ClearCache(BlogComment entity)
1616
{
17-
RemoveByPrefix(string.Format(NopBlogsCachingDefaults.BlogCommentsNumberPrefixCacheKey, entity.BlogPostId));
17+
RemoveByPrefix(string.Format(NopBlogsDefaults.BlogCommentsNumberPrefixCacheKey, entity.BlogPostId));
1818
}
1919
}
2020
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Nop.Core.Domain.Blogs;
2-
using Nop.Services.Caching.CachingDefaults;
2+
using Nop.Services.Caching;
33

4-
namespace Nop.Services.Caching.CacheEventConsumers.Blogs
4+
namespace Nop.Services.Blogs.Caching
55
{
66
/// <summary>
77
/// Represents a blog post cache event consumer
@@ -14,7 +14,7 @@ public partial class BlogPostCacheEventConsumer : CacheEventConsumer<BlogPost>
1414
/// <param name="entity">Entity</param>
1515
protected override void ClearCache(BlogPost entity)
1616
{
17-
RemoveByPrefix(NopBlogsCachingDefaults.BlogTagsPrefixCacheKey);
17+
RemoveByPrefix(NopBlogsDefaults.BlogTagsPrefixCacheKey);
1818
}
1919
}
2020
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using Nop.Core.Caching;
22

3-
namespace Nop.Services.Caching.CachingDefaults
3+
namespace Nop.Services.Blogs
44
{
55
/// <summary>
66
/// Represents default values related to blogs services
77
/// </summary>
8-
public static partial class NopBlogsCachingDefaults
8+
public static partial class NopBlogsDefaults
99
{
10+
#region Caching defaults
11+
1012
/// <summary>
1113
/// Key for number of blog comments
1214
/// </summary>
@@ -38,5 +40,7 @@ public static partial class NopBlogsCachingDefaults
3840
/// Gets a key pattern to clear cache
3941
/// </summary>
4042
public static string BlogTagsPrefixCacheKey => "Nop.blog.tags";
43+
44+
#endregion
4145
}
4246
}

src/Libraries/Nop.Services/Caching/CacheEventConsumers/CacheEventConsumer.cs renamed to src/Libraries/Nop.Services/Caching/CacheEventConsumer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Nop.Core.Infrastructure;
55
using Nop.Services.Events;
66

7-
namespace Nop.Services.Caching.CacheEventConsumers
7+
namespace Nop.Services.Caching
88
{
99
public abstract partial class CacheEventConsumer<TEntity> : IConsumer<EntityInsertedEvent<TEntity>>,
1010
IConsumer<EntityUpdatedEvent<TEntity>>,

0 commit comments

Comments
 (0)